Skip to content

Instantly share code, notes, and snippets.

@DrTom
Created November 27, 2012 14:10
Show Gist options
  • Save DrTom/4154399 to your computer and use it in GitHub Desktop.
Save DrTom/4154399 to your computer and use it in GitHub Desktop.
require 'builder'
require 'execjs'
require 'open3'
require 'tilt'
require (File.dirname __FILE__) + "/lib/pandoc_helper"
require (File.dirname __FILE__) + "/lib/showdown_helper"
require (File.dirname __FILE__) + "/lib/posts_helper"
### PANDOC Markdown Rendering
class ::PandocMarkdownRenderTemplate < ::Tilt::Template
def prepare
end
def evaluate(scope,locals,&block)
@output = PandocHelper.render_markdown data
end
end
Tilt.register PandocMarkdownRenderTemplate, 'mkd'
Tilt.register PandocMarkdownRenderTemplate, 'markdown'
Tilt.register PandocMarkdownRenderTemplate, 'md'
Tilt.prefer PandocMarkdownRenderTemplate
set :markdown_engine, PandocMarkdownRenderTemplate
###
# Compass
###
require 'ninesixty'
# Susy grids in Compass
# First: gem install compass-susy-plugin
# require 'susy'
# Change Compass configuration
compass_config do |config|
# config.output_style = :compact
#
config.preferred_syntax = :sass
end
###
# Haml
###
# CodeRay syntax highlighting in Haml
# First: gem install haml-coderay
# require 'haml-coderay'
# CoffeeScript filters in Haml
# First: gem install coffee-filter
require 'coffee-filter'
# Automatic image dimensions on image_tag helper
# activate :automatic_image_sizes
###
# Page command
###
page "/posts/feed.xml", :layout => false
# Per-page layout changes:
#
# With no layout
# page "/path/to/file.html", :layout => false
#
# With alternative layout
# page "/path/to/file.html", :layout => :otherlayout
#
# A path which all have the same layout
# with_layout :admin do
# page "/admin/*"
# end
# Proxy (fake) files
# page "/this-page-has-no-template.html", :proxy => "/template-file.html" do
# @which_fake_page = "Rendering a fake page with a variable"
# end
###
# Helpers
###
# Methods defined in the helpers block are available in templates
# helpers do
# def some_helper
# "Helping"
# end
# end
# Change the CSS directory
# set :css_dir, "alternative_css_directory"
# Change the JS directory
# set :js_dir, "alternative_js_directory"
# Change the images directory
# set :images_dir, "alternative_image_directory"
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
activate :minify_css
# Minify Javascript on build
activate :minify_javascript
#activate :asset_hash
# Enable cache buster
#activate :cache_buster
# Use relative URLs
# activate :relative_assets
# Compress PNGs after build
# First: gem install middleman-smusher
# require "middleman-smusher"
# activate :smusher
# Or use a different image path
# set :http_path, "/Content/images/"
end
- @post = PostsHelper.load_post (File.dirname __FILE__)
!= partial "post"
module ::PandocHelper
def self.render_markdown input
puts "RENDERING MARKDOWN WITH PANDOC"
sin,sout,serr,wait = Open3.popen3("pandoc -f markdown -")
sin.write input
sin.close
sout.read
end
end
module ::PostsHelper
POST_MATCHER = /\/\d\d\d\d\/\d\d\/\d\d\/(\w|\d|-)+$/
TITLE_MATCHER = /((^\/|\w|\d|-)+)$/
DATE_MATCHER = /\/(\d\d\d\d)\/(\d\d)\/(\d\d)\/(\w|\d|-)+$/
def self.load_post start_dir
puts "LOADING POST #{start_dir}"
post = {}
post[:meta] = load_metadata start_dir
post[:content] = render_markdown File.read start_dir + "/_post_content.mkd"
if File.exist? start_dir + "/_post_appendix.mkd"
post[:appendix] = render_markdown File.read start_dir + "/_post_appendix.mkd"
end
post
end
def self.load_metadata start_dir
TITLE_MATCHER =~ start_dir
meta = YAML.load_file start_dir + "/_post_meta.yml"
meta["title"] ||= $1.gsub(/_/x, " ")
DATE_MATCHER =~ start_dir
published = Time.parse "#{$1}-#{$2}-#{$3}"
meta["published"] = published
#require 'pry'; binding-pry
if meta["updated"]
meta["updated"] = Time.parse meta["updated"].to_s
end
POST_MATCHER =~ start_dir
meta["path"] ||= $& +"/"
meta["tags"] = meta["tags"].map{|s| s.downcase}.sort.join ', ' if meta["tags"] and meta["tags"].kind_of? Array
meta
end
def self.load_index posts_dir
posts = []
(Dir.glob (posts_dir+"/**/*")).each do |f|
if f =~ POST_MATCHER
meta_data = load_metadata f
posts.unshift meta_data
end
end
posts
end
def self.render_markdown data
::PandocHelper.render_markdown data
end
def self.build_feed posts
xml = ::Builder::XmlMarkup.new(:indent => 2)
xml.instruct!
xml.feed :xmlns => "http://www.w3.org/2005/Atom" do |feed|
feed.author do |author|
author.name "Dr. Thomas Schank"
author.uri "http://dr.th.schank.ch/"
end
feed.title "Dr. Tom's Posts"
feed.subtitle "Don't reinvent the wheel unless you want to know how to make good wheels."
feed.id "http://drtom.schank.ch/posts/"
feed.updated Time.now.iso8601
posts.each do |post|
feed.entry do |entry|
entry.title post["title"]
entry.published post["published"]
entry.updated post["updated"] if post["updated"]
entry.summary post["abstract"]
fullpath="/posts"+post["path"]
entry.id fullpath
entry.link :href=>fullpath
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment