require 'redcarpet' require 'rouge' require 'rouge/plugins/redcarpet' require 'action_view' module Playbook module RedcarpetHelper def markdown(text) options = { filter_html: false, hard_wrap: true, link_attributes: { rel: 'nofollow', target: "_blank" }, space_after_headers: true, fenced_code_blocks: true, no_styles: false, safe_links_only: true } extensions = { autolink: true, superscript: true, fenced_code_blocks: true, tables: true, disable_indented_code_blocks: false, strikethrough: true, underline: true, highlight: true, footnotes: true, with_toc_data: true, } renderer = HTMLBlockCode.new(options) markdown = Redcarpet::Markdown.new(renderer, extensions) markdown.render(text).html_safe end def rouge(text, language) formatter = Rouge::Formatters::HTML.new(css_class: 'highlight') lexer = Rouge::Lexer.find(language) formatter.format(lexer.lex(text)) end def rouge_markdown(text) render_options = { filter_html: true, hard_wrap: true, link_attributes: { rel: 'nofollow' } } renderer = HTML.new(render_options) extensions = { autolink: true, fenced_code_blocks: true, lax_spacing: true, no_intra_emphasis: true, strikethrough: true, superscript: true } markdown = Redcarpet::Markdown.new(renderer, extensions) markdown.render(text) end end class HTML < Redcarpet::Render::HTML include Rouge::Plugins::Redcarpet end class HTMLBlockCode < Redcarpet::Render::HTML include ActionView::Helpers::AssetTagHelper def header(title, level) if level == 7 @headers ||= [] permalink = title.gsub(/\W+/, "-") if @headers.include?(permalink) permalink += "_1" loop do break if !@headers.include?(permalink) permalink.gsub!(/\_(\d+)$/, "_#{$1.to_i + 1}") end end @headers << permalink %(\n#{title}\n) else %(\n#{title}\n) end end def image(link, title, alt_text) image_tag(link, title: title, alt: alt_text, class: "imageloader lazyload") end def preprocess(full_document) full_document.gsub(/\[component (.*)\]/) do @string = $1 @default_height = '160' @attr = ['', @default_height] # Set src from attributes @string.gsub(/src="(.*?)"/) do @attr[0] = $1 end # Set height from attributes @string.gsub(/height="(.*?)"/) do if $1 @attr[1] = $1 else @attr[1] = @default_height end end %(\n
View component
\n) end end def list(contents, list_type) @contents = contents @list_items = contents.split("\n") if @list_items[0].include? "[do]" or @list_items[0].include? "[dont]" @element_items = [] @list_items.each do |item, index| item.gsub(/\
  • (.*)\<\/li>/) do @element_items.push($1); end end # Doing both because we could have either/both # clean up @dont_items, @trash_items_dont = @element_items.partition { |x, i| x.include? "[dont]" } @do_items, @trash_items_do = @element_items.partition { |x, i| x.include? "[do]" } @do_list = [] @do_items.each do |item, index| @do_list.push("
  • #{item.sub('[do] ', '')}
  • ") end @do_list = "" @dont_list = [] @dont_items.each do |item, index| @dont_list.push("
  • #{item.sub('[dont] ', '')}
  • ") end @dont_list = "" "
    #{@do_list}
    #{@dont_list}
    " else @contents end end end end