Skip to content

Instantly share code, notes, and snippets.

@ralph
Created October 20, 2011 11:39
Show Gist options
  • Save ralph/1300939 to your computer and use it in GitHub Desktop.
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.
#!/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)
@ralph
Copy link
Author

ralph commented Oct 20, 2011

:)

@roidrage
Copy link

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
])

@ralph
Copy link
Author

ralph commented Oct 20, 2011

It's nice and all, but I'd rather have the list of valid languages be generated from Albino or pygmentize or something.

@roidrage
Copy link

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.

@ralph
Copy link
Author

ralph commented Oct 20, 2011

All right, all right, updated for great enhanced and bigger Redcartishness :)

@mnapoli
Copy link

mnapoli commented Oct 15, 2012

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