module StarlightHelpers::Content

Public Instance Methods

render_text(content, markup = 'text/plain', options = {}) click to toggle source
# File lib/starlight_helpers/content.rb, line 15
def render_text(content, markup = 'text/plain', options = {})
        textile_opts = []
        markdown_opts = {
                tables: true, fenced_code_blocks: true, autolink: true,
                disable_indented_code_blocks: true, strikethrough: true,
                underline: true, highlight: true, footnotes: true
        }
        if options[:not_trusted]
                textile_opts << :lite_mode << :filter_html
                markdown_opts = {filter_html: true, no_styles: true}
        end
  
        if markup == 'text/markdown'
                renderer = Redcarpet::Render::HTML.new(markdown_opts)
                Redcarpet::Markdown.new(renderer).render(content)
        elsif markup == 'text/textile'
                RedCloth.new(content, textile_opts).to_html
        elsif markup == 'text/html' && options[:not_trusted] != true
                # nothing
                content
        # for text/plain and other
        else
                content.gsub(/["&'<>]/,
                        '"' => '&quot;',
                        '&' => '&amp;',
                        "'" => '&apos;',
                        '<' => '&lt;',
                        '>' => '&gt;'
                ).gsub("\n", '<br>')
        end
end