public
Created

  • Download Gist
replacer.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.