Class: Docsmith::Rendering::HtmlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/docsmith/rendering/html_renderer.rb

Overview

Renders a DocumentVersion's content as an HTML string. Markdown is shown pre-formatted (no external gem dependency). JSON is pretty-printed inside a pre block. html content passes through Docsmith.configuration.html_sanitizer. Subclass and override #render to plug in a markdown gem (e.g. redcarpet).

Instance Method Summary collapse

Instance Method Details

#render(version, **options) ⇒ String

Returns HTML representation of the version content.

Parameters:

Returns:

  • (String)

    HTML representation of the version content



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/docsmith/rendering/html_renderer.rb', line 17

def render(version, **options)
  content      = version.content.to_s
  content_type = version.content_type.to_s

  case content_type
  when "html"
    sanitize_html(content)
  when "markdown"
    "<pre class=\"docsmith-markdown\">#{CGI.escapeHTML(content)}</pre>"
  when "json"
    pretty = JSON.pretty_generate(JSON.parse(content))
    "<pre class=\"docsmith-json\">#{CGI.escapeHTML(pretty)}</pre>"
  else
    "<pre>#{CGI.escapeHTML(content)}</pre>"
  end
rescue JSON::ParserError
  "<pre>#{CGI.escapeHTML(content)}</pre>"
end