lib/devcenter-parser/github_parser.rb in devcenter-parser-2.2.2 vs lib/devcenter-parser/github_parser.rb in devcenter-parser-2.2.3rc1
- old
+ new
@@ -1,9 +1,36 @@
module GitHubParser
extend self
class HTMLWithPantsRenderer < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
+
+ def block_code(code, language)
+ if language
+ language.sub!(/\Alang(uage)?-/, '')
+ %{<pre class="language-#{language}"><code class="language-#{language}">#{html_escape(code)}</code></pre>}
+ else
+ %{<pre><code>#{html_escape(code)}</code></pre>}
+ end
+ end
+
+ private
+
+ # This is a method copy/pasted from redcarpet/lib/redcarpet.rb
+ # It's a private instance method on the Render::Safe class which inherits
+ # from the HTML renderer.
+ # I'm not sure why our tests expect escaped code while we inherit from the
+ # HTML renderer and not the Safe renderer though.
+ def html_escape(string)
+ string.gsub(/['&\"<>\/]/, {
+ '&' => '&',
+ '<' => '<',
+ '>' => '>',
+ '"' => '"',
+ "'" => ''',
+ "/" => '/',
+ })
+ end
end
def self.parse(markdown)
html = github_parser.render(markdown.to_s)
doc = Nokogiri::HTML::DocumentFragment.parse(html)