Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 40 lines (31 sloc) 1.13 KB
#!/usr/bin/env ruby
# Jekyll preprocessor for Marked app
# Converts Jekyll posts to GitHub Markdown
# See http://brettterpstra.com/2013/01/04/previewing-jekyll-posts-with-marked/
BLOG_ROOT = File.expand_path("../..", ENV["MARKED_PATH"])
SEPARATOR = "---"
def process(content)
if content.start_with?(SEPARATOR)
require "yaml"
sections = content.split(SEPARATOR)
frontmatter = YAML::load(sections[1])
title = frontmatter["title"]
content = "# #{title}" + sections[2..-1].join(SEPARATOR)
# Convert Jekyll syntax highlighting to GitHub Markdown
content.gsub!(/\{% highlight (.*?) %\}/, "```\\1")
content.gsub!(/\{% endhighlight %\}/, "```")
# Expand Jekyll variables to their names as we don't know their values
content.gsub!(/\{\{\s+(.*?)\s+\}\}/, "\\1")
# Fix image links
content.gsub!(/\/static\/images\//, "#{BLOG_ROOT}\\0")
end
content
end
begin
Encoding.default_external = Encoding::UTF_8
puts process(STDIN.read)
rescue => e
# Since Marked does not report any preprocessor errors inside the app, show
# them as rendered output.
puts "Preprocessor failed with error \"#{e.message}\""
end