Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created March 18, 2012 17:51
Show Gist options
  • Select an option

  • Save peterhellberg/2078566 to your computer and use it in GitHub Desktop.

Select an option

Save peterhellberg/2078566 to your computer and use it in GitHub Desktop.

Revisions

  1. peterhellberg created this gist Mar 18, 2012.
    34 changes: 34 additions & 0 deletions gfm.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/env ruby
    require 'rubygems'
    require 'redcarpet'
    require 'pygments.rb'

    class HTMLwithPygments < Redcarpet::Render::HTML
    def block_code(code, language)
    Pygments.highlight(code, :lexer => language.to_sym, :options => {
    :encoding => 'utf-8'
    })
    end
    end

    def from_markdown(text)
    markdown = Redcarpet::Markdown.new(HTMLwithPygments,
    :fenced_code_blocks => true,
    :no_intra_emphasis => true,
    :autolink => true,
    :strikethrough => true,
    :lax_html_blocks => true,
    :superscript => true,
    :hard_wrap => false,
    :tables => true,
    :xhtml => false)

    text.gsub!(/\{\{( *)?"(.*?)"\}\}/, '\1\2')
    text.gsub!(/^\{% highlight (.+?) ?%\}(.*?)^\{% endhighlight %\}/m) do |match|
    Pygments.highlight($2, :lexer => $1, :options => {:encoding => 'utf-8'})
    end

    markdown.render(text)
    end

    puts from_markdown(ARGF.read)