Skip to content

Instantly share code, notes, and snippets.

@krismeister
Last active August 29, 2016 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krismeister/6e33e7c0f4a1f8e4a8708779f253e1ca to your computer and use it in GitHub Desktop.
Save krismeister/6e33e7c0f4a1f8e4a8708779f253e1ca to your computer and use it in GitHub Desktop.
Extend the Redcarpet HTML Renderer to output language-XXX class
class PreCodeLanguageMarkdown < Redcarpet::Render::HTML
def block_code(code, language)
[
'<pre>',
"<code class='language-#{language}'>#{html_escape(code)}</code>",
'</pre>'
].join()
end
private
# Doesn't seem dangerous, keep an eye on it.
def html_escape(string)
string.gsub(/['&\"<>\/]/, {
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
'"' => '&quot;',
"'" => '&#x27;',
"/" => '&#x2F;',
})
end
end
@krismeister
Copy link
Author

Put this file in Rail's ./lib

Require the file where you need to render:

 require_dependency('PreCodeLanguageMarkdown')

Then be sure to add the file to the watch list, so when you change it , your development env will reload it:

config.watchable_dirs['lib'] = [:rb]

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