1require 'redcarpet/render_strip'
 
2
 
3module MarkdownHelper
 
4  HTML_DEFAULTS = {
 
5    escape_html: false, filter_html: false,
 
6    hard_wrap: true, xhtml: true, safe_links_only: true,
 
7    no_styles: true, no_images: true, no_links: false,
 
8    with_toc_data: false, prettify: false, link_attributes: {}
 
 9  }
 
 
11  PARSER_DEFAULTS = {
 
12    no_intra_emphasis: true, tables: false, fenced_code_blocks: false,
 
13    autolink: true, disable_indented_code_blocks: false, strikethrough: true,
 
14    lax_spacing: false, space_after_headers: true, superscript: true,
 
15    underline: false, highlight: false, quote: false, footnotes: false
 
16  }
 
  • Complexity 1 » saikuro
18  def markdown_to_html(markup, options = {})
 
19    markdown_parser(html_renderer(options), options).render(markup).html_safe
 
20  end
 
  • Complexity 1 » saikuro
22  def markdown_to_text(markup, options = {})
 
23    markdown_parser(text_renderer, options).render(markup).html_safe
 
24  end
 
 
26  private
 
  • Complexity 1 » saikuro
28  def html_renderer(options)
 
29    Redcarpet::Render::HTML.new(options_for_renderer(options))
 
30  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
32  def text_renderer
 
33    Redcarpet::Render::StripDown.new
 
34  end
 
  • Complexity 1 » saikuro
36  def markdown_parser(renderer, options)
 
37    Redcarpet::Markdown.new(renderer, options_for_parser(options))
 
38  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
40  def options_for_parser(options)
 
41    PARSER_DEFAULTS.merge(options.slice(*PARSER_DEFAULTS.keys))
 
42  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
44  def options_for_renderer(options)
 
45    HTML_DEFAULTS.merge(options.slice(*HTML_DEFAULTS.keys))
 
46  end
 
47end