Skip to content

Instantly share code, notes, and snippets.

@kizzx2
Last active December 9, 2015 21:58
Show Gist options
  • Select an option

  • Save kizzx2/4333771 to your computer and use it in GitHub Desktop.

Select an option

Save kizzx2/4333771 to your computer and use it in GitHub Desktop.
Markdown emails with syntax highlighting on each <pre> block
#!/usr/bin/env ruby
# encoding: UTF-8
require 'nokogiri'
require 'open3'
html = Open3.popen2('redcarpet') do |stdin, stdout|
stdin.write(STDIN.read)
stdin.close
stdout.read
end
doc = Nokogiri::HTML(html)
doc.xpath("//p").each do |p|
p['style'] = %(font-family: Calibri, Helvetica, Arial, sans-serif)
end
doc.xpath("//code").each do |code|
code['style'] = %(background: #eee; color: #d14; ) +
%(font-family: Consolas, 'Courier New', monospace)
end
doc.xpath("//blockquote").each do |blockquote|
blockquote["type"] = "cite"
end
doc.xpath("//pre[@lang]").each do |pre|
lang = pre.attr('lang')
Open3.popen2("pygmentize -l #{lang} -f html -O noclasses -O style=pastie") do |stdin, stdout|
stdin.write(pre.inner_text)
stdin.close
pre.replace(stdout.read)
end
end
puts doc.to_html
@kizzx2
Copy link
Copy Markdown
Author

kizzx2 commented Dec 19, 2012

Useful for creating rich HTML emails in Thunderbird by Insert > HTML

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