Skip to content

Commit

Permalink
Merge pull request #7 from cespare/no_hack
Browse files Browse the repository at this point in the history
Replace code blocks by subclassing the Redcarpet renderer.
  • Loading branch information
dmac committed Jul 24, 2012
2 parents 5bbdfb6 + 4e73916 commit 5cfba0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
21 changes: 6 additions & 15 deletions bin/markdown_doctor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "rubygems"
require "redcarpet"
require "nokogiri"
require "pygments.rb"

HEADER = <<EOF
Expand Down Expand Up @@ -245,23 +244,15 @@ FOOTER = <<EOF
</div>
EOF

def pygmentize_code_blocks(html)
doc = Nokogiri::HTML(html)
code_blocks = doc.css("pre code[class]").map(&:parent)

code_blocks.each do |code|
language = code.child.attribute("class").value
pygmentized_html = Pygments.highlight(code.child.text, :lexer => language, :encoding => "utf-8",
:nowrap => true, :stripnl => false, :stripall => false)
new_node = Nokogiri::HTML.fragment(pygmentized_html)
code.replace(new_node)
class DoctoredRedcarpet < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, :lexer => language, :encoding => "utf-8", :nowrap => true, :stripnl => false,
:stripall => false)
end

doc.to_html
end

text = ARGF.read
markdown_options = { :fenced_code_blocks => true, :tables => true, :space_after_headers => true }
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, markdown_options)
output = HEADER + pygmentize_code_blocks(markdown.render(text)) + FOOTER
markdown = Redcarpet::Markdown.new(DoctoredRedcarpet, markdown_options)
output = HEADER + markdown.render(text) + FOOTER
puts output
1 change: 0 additions & 1 deletion markdown_doctor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Gem::Specification.new do |s|
s.executables = %w(markdown_doctor)
s.files = `git ls-files`.split("\n")

s.add_dependency("nokogiri", "~> 1.5.0")
s.add_dependency("pygments.rb", "~> 0.2.4")
s.add_dependency("redcarpet", "~> 2.1.0")
end

0 comments on commit 5cfba0e

Please sign in to comment.