Last active
December 9, 2015 21:58
-
-
Save kizzx2/4333771 to your computer and use it in GitHub Desktop.
Markdown emails with syntax highlighting on each <pre> block
This file contains hidden or 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
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful for creating rich HTML emails in Thunderbird by Insert > HTML