Class: Polites::HtmlFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/polites/html_formatter.rb

Overview

Takes an AST as produced by Parser and generates HTML output from it. This output is not guaranteed to be the exact same as Polites would produce itself, but it should be structurally similar.

Constant Summary collapse

NL =
"\n"

Instance Method Summary collapse

Constructor Details

#initializeHtmlFormatter

Returns a new instance of HtmlFormatter.



19
20
21
22
23
# File 'lib/polites/html_formatter.rb', line 19

def initialize
  @footnotes = []
  @indenter = ListIndenter.new
  @code_grouper = CodeGrouper.new
end

Instance Method Details

#call(obj, indent: false, join: '') ⇒ String

Apply formatting to the given AST.

Parameters:

  • obj (Object)
  • indent (Bool) (defaults to: false)

    whether to apply indentation when formatting. Used internally.

  • join (String) (defaults to: '')

    how to join multiple elements together. Used internally.

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/polites/html_formatter.rb', line 31

def call(obj, indent: false, join: '')
  case obj
  when Sheet
    content = call(obj.content, indent: true, join: NL)
    content << NL << tag(:ol, NL + footnotes + NL, id: 'footnotes') << NL if @footnotes.any?
    content
  when ListIndenter::List
    nl = obj.children.first.level.positive? ? NL : ''
    tag_name = case obj.children.first
               when Block::OrderedList then 'ol'
               when Block::UnorderedList then 'ul'
               end
    nl + tag(tag_name, NL + call(obj.children, join: NL) + NL) + nl
  when Array
    coll = indent ? @code_grouper.call(@indenter.call(obj)) : obj
    coll.map { |c| call(c) }.join(join)
  when Block::UnorderedList, Block::OrderedList
    tag(:li, call(obj.children))
  when Block::Paragraph
    tag(:p, call(obj.children))
  when Block::Heading1
    tag(:h1, call(obj.children))
  when Block::Heading2
    tag(:h2, call(obj.children))
  when Block::Heading3
    tag(:h3, call(obj.children))
  when Block::Heading4
    tag(:h4, call(obj.children))
  when Block::Heading5
    tag(:h5, call(obj.children))
  when Block::Heading6
    tag(:h6, call(obj.children))
  when Block::Blockquote
    tag(:blockquote, tag(:p, call(obj.children)))
  when Block::CodeBlock
    tag(:pre, tag(:code, CGI.escape_html(call(obj.children)), class: "language-#{obj.syntax}"))
  when Block::Divider
    tag(:hr)
  when Span::Strong
    tag(:strong, call(obj.children))
  when Span::Emph
    tag(:em, call(obj.children))
  when Span::Delete
    tag(:del, call(obj.children))
  when Span::Mark
    tag(:mark, call(obj.children))
  when Span::Code
    tag(:code, call(obj.children))
  when Span::Annotation
    call(obj.children)
  when Span::Footnote
    @footnotes << obj.text
    n = @footnotes.count
    tag(:sup, tag(:a, n, id: "ffn#{n}", href: "#fn#{n}", class: 'footnote'))
  when Span::Image
    figcaption = obj.description ? tag(:figcaption, obj.description) : ''
    tag(:figure, tag(:img, nil, src: image_src(obj), title: obj.title, alt: obj.description, width: obj.width, height: obj.height) + figcaption)
  when Span::Link
    tag(:a, call(obj.children), href: obj.url, title: obj.title)
  when Text
    obj.text
  else
    raise FormattingError, "Unknown obj #{obj.inspect}"
  end
end