Skip to content

Instantly share code, notes, and snippets.

@lfborjas
Created April 19, 2011 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfborjas/926783 to your computer and use it in GitHub Desktop.
Save lfborjas/926783 to your computer and use it in GitHub Desktop.
require 'hpricot'
require 'coderay'
require 'rdiscount'
#open the file in markdown
f = File.open "tmp.markdown"
#generate html with rdiscount
md = RDiscount.new f.read
#parse it with hpricot and be magical
d = Hpricot md.to_html
d.search "pre" do |code|
lang = ""
lines = code.innerText.split "\n"
lines.each do |line|
if line[0..2] == "@@@"
lang = line.gsub "@@@", ''
break
end
end
#ok, we have the lang, now, color it
newhtml = Hpricot CodeRay.scan(lines[1..-1].join("\n"), lang.to_sym).div :line_numbers => :inline
d.replace_child code, newhtml
end
fn = File.new "tmp_coded.html", 'w'
fn.write d.to_s
fn.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment