Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created March 18, 2012 17:51
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save peterhellberg/2078566 to your computer and use it in GitHub Desktop.
Save peterhellberg/2078566 to your computer and use it in GitHub Desktop.
GitHub Flavored Markdown parser for use with Jekyll and Marked.app
#!/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)
@peterhellberg
Copy link
Author

Screenshot

@henrik
Copy link

henrik commented Mar 18, 2012

Neat. Unless I read it wrong, the ? in text.gsub!(/\{\{( *)? is redundant.

@peterhellberg
Copy link
Author

Oh, yeah that’s right :)

@cboettig
Copy link

Very cool. Will this work with the version of redcarpet on gh-pages? (1.17 I think?) I use this plugin to get support for fenced codeblocks on jekyll, but it is not compatible with the version on github so I must run jekyll locally first.

@peterhellberg
Copy link
Author

@cboettig I’m not sure. I generally deploy to one of my private servers. But you are welcome to try :)

@lviggiano
Copy link

I'm not a ruby programmer. How do I set it up to work with Jekyll?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment