Created
April 19, 2011 03:43
-
-
Save lfborjas/926783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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