Created
June 8, 2015 02:56
-
-
Save sapslaj/c806d363621497e3f4ec to your computer and use it in GitHub Desktop.
How to refactor a Markdown conversion class to a struct
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
| Markdown = Struct.new(:markdown_text) do | |
| class << self | |
| def convert(text) | |
| Markdown.new(text).to_html | |
| end | |
| end | |
| def to_html(renderer_options = {hard_wrap: true, filter_html: true}) | |
| renderer_options = {hard_wrap: true, filter_html: true}.merge renderer_options | |
| redcarpet_options = { | |
| autolink: true, | |
| no_intra_emphasis: true, | |
| fenced_code_blocks: true, | |
| lax_html_blocks: true, | |
| strikethrough: true, | |
| superscript: true, | |
| tables: true | |
| } | |
| Rails.cache.fetch ["markdown", Digest::SHA1.hexdigest(markdown_text)].join('-') do | |
| Redcarpet::Markdown.new(renderer.new(renderer_options), redcarpet_options).render(markdown_text) | |
| end | |
| end | |
| private | |
| def renderer | |
| Class.new(Redcarpet::Render::HTML) do | |
| def block_code(code, language) | |
| sha = Digest::SHA1.hexdigest(code) | |
| Rails.cache.fetch ["code", language, sha].join('-') do | |
| lexer_aliases = Pygments.lexers.map { |l| l.second[:aliases] }.flatten | |
| pygments_options = {} | |
| pygments_options[:lexer] = code if lexer_aliases.include? language | |
| Pygments.highlight(code, pygments_options) | |
| end | |
| end | |
| end | |
| end | |
| end |
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
| module Markdown | |
| extend self | |
| def convert(text, options={}) | |
| options[:engine] ||= :redcarpet_with_pygments | |
| sha = Digest::SHA1.hexdigest(text) | |
| Rails.cache.fetch ["markdown", options[:engine].to_s, sha].join('-') do | |
| send("engine_#{options[:engine]}".intern, text) | |
| end | |
| end | |
| def engine_kramdown_with_pygments(text) | |
| pygs = Class.new(Kramdown::Converter::Html) | |
| pygs.send :define_method, :convert_codeblock do |el, indent| | |
| attr = el.attr.dup | |
| lang = extract_code_language!(attr) | |
| if lang | |
| add_code_tags( | |
| Pygments.highlight(el.value, | |
| :lexer => lang, | |
| :options => { :encoding => 'utf-8' }) | |
| ) | |
| else | |
| "<pre><code>#{el.value}</code></pre>" | |
| end | |
| end | |
| pygs.send :define_method, :add_code_tags do |code| | |
| code = code.sub(/<pre>/,'<pre><code>') | |
| code = code.sub(/<\/pre>/,"</code></pre>") | |
| end | |
| self.const_set 'Pygs', pygs | |
| options = { | |
| auto_ids: false, | |
| input: 'gfm' | |
| } | |
| Kramdown::Document.new(text, options).to_pygs | |
| end | |
| def engine_redcarpet_with_pygments(text) | |
| html_with_pygments = Class.new(Redcarpet::Render::HTML) | |
| html_with_pygments.send :define_method, :block_code do |code, language| | |
| sha = Digest::SHA1.hexdigest(code) | |
| Rails.cache.fetch ["code", language, sha].join('-') do | |
| lexer_aliases = Pygments.lexers.map { |l| l.second[:aliases] }.flatten | |
| pygments_options = {} | |
| pygments_options[:lexer] = code if lexer_aliases.include? language | |
| Pygments.highlight(code, pygments_options) | |
| end | |
| end | |
| self.const_set 'HTMLwithPygments', html_with_pygments | |
| renderer = HTMLwithPygments.new(hard_wrap: true, filter_html: true) | |
| options = { | |
| autolink: true, | |
| no_intra_emphasis: true, | |
| fenced_code_blocks: true, | |
| lax_html_blocks: true, | |
| strikethrough: true, | |
| superscript: true, | |
| tables: true # TODO: Fix table formatting | |
| } | |
| Redcarpet::Markdown.new(renderer, options).render(text) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment