Created
October 20, 2011 11:39
-
-
Save ralph/1300939 to your computer and use it in GitHub Desktop.
Markdown Processor for Redcarpet version 2, for usage on the command line, in Marked etc. Supports syntax highlighting via Pygments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Processor for Github flavored markdown, inspired by: | |
# https://github.com/alampros/Docter/blob/master/bin/github-flavored-markdown.rb | |
# | |
# Current version of this script can be found here: | |
# https://gist.github.com/1300939 | |
# | |
# Adapted for Redcarpet version 2 by Ralph von der Heyden | |
# http://github.com/ralph | |
# http://twitter.com/ralph | |
# http://www.rvdh.de | |
# | |
# You will need the following gems: | |
# * redcarpet version 2 | |
# * albino | |
# | |
# You also need the pygments library for syntax highlighting: | |
# sudo easy_install pygments | |
# Make sure the pygmentize bin is in your $PATH, or link it like that: | |
# sudo ln -s /usr/local/bin/pygmentize /usr/bin | |
# | |
# Make sure to chmod +x this script. | |
# | |
# Usage: | |
# github-flavored-markdown.rb document.md | |
require 'rubygems' | |
require 'redcarpet' | |
require 'albino' | |
class SyntaxRenderer < Redcarpet::Render::HTML | |
def block_code(code, language) | |
if language && !language.empty? | |
Albino.colorize(code, language) | |
else | |
"<pre><code>#{code}</code></pre>" | |
end | |
end | |
end | |
def markdown(text) | |
renderer = SyntaxRenderer.new(optionize [ | |
:with_toc_data, | |
:hard_wrap, | |
:xhtml | |
]) | |
markdown = Redcarpet::Markdown.new(renderer, optionize([ | |
:fenced_code_blocks, | |
:no_intra_emphasis, | |
:tables, | |
:autolink, | |
:strikethrough, | |
:space_after_headers | |
])) | |
markdown.render(text) | |
end | |
def optionize(options) | |
options.each_with_object({}) { |option, memo| memo[option] = true } | |
end | |
puts markdown(ARGF.read) |
More Redcarpet-ish way to do it, removes the need to use Nokogiri entirely:
class SyntaxRenderer < Redcarpet::Render::HTML
def block_code(code, language)
if %w{ruby javascript}.include?(language)
Albino.colorize(code, language)
else
"<pre><code>#{code}</code></pre>"
end
end
end
renderer = SyntaxRenderer.new(optionize [
:with_toc_data,
:hard_wrap,
:gh_blockcode,
:xhtml
])
It's nice and all, but I'd rather have the list of valid languages be generated from Albino or pygmentize or something.
Not really the point, the language part is just taken from my code, you can remove the check easily, and it'll still be more Redcartet-ish.
All right, all right, updated for great enhanced and bigger Redcartishness :)
I have ruby 1.8.7 and had to replace:
options.each_with_object({}) { |option, memo| memo[option] = true }
with
options.inject({}) { |memo, option| memo[option] = true; memo }
because each_with_object
was undefined.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:)