Class: Clickwrap::DocumentRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/clickwrap/document_renderer.rb

Overview

Safe, dependency-free rendering for the text formats most legal documents use. It intentionally favors faithful, readable text over clever Markdown interpretation: source characters are escaped, then placed in a

element. Applications that want richer Markdown may supply a renderer, but
its HTML still passes through #sanitize_html before Clickwrap stores it.

Constant Summary collapse

NAME =
"clickwrap_safe_document_renderer"
VERSION =
"1"
SANITIZER_NAME =
"rails_safe_list_sanitizer"
SANITIZER_VERSION =
"1"
SAFE_TAGS =
%w[
  a abbr b blockquote br cite code dd del details div dl dt em h1 h2 h3 h4 h5 h6
  hr i ins kbd li mark ol p pre q s samp small span strong sub summary sup table
  tbody td tfoot th thead tr u ul var
].freeze
SAFE_ATTRIBUTES =
%w[href title lang dir class id colspan rowspan scope].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sanitize_html(html) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/clickwrap/document_renderer.rb', line 52

def self.sanitize_html(html)
  safe_list_sanitizer_class.new.sanitize(
    html.to_s,
    tags: SAFE_TAGS,
    attributes: SAFE_ATTRIBUTES
  ).to_s
end

Instance Method Details

#call(bytes, definition) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clickwrap/document_renderer.rb', line 31

def call(bytes, definition)
  text = utf8_text!(bytes, definition)

  html = case definition.media_type
         when "text/html" then text
         when "text/markdown", "text/plain", "application/json"
           %(<pre class="clickwrap-document-source">#{ERB::Util.html_escape(text)}</pre>)
         else
           return nil
         end

  {
    bytes: self.class.sanitize_html(html),
    media_type: "text/html; charset=utf-8",
    renderer_name: NAME,
    renderer_version: VERSION,
    sanitizer_name: SANITIZER_NAME,
    sanitizer_version: SANITIZER_VERSION
  }
end