Class: GovukTechDocs::TechDocsHTMLRenderer

Inherits:
Middleman::Renderers::MiddlemanRedcarpetHTML
  • Object
show all
Includes:
Redcarpet::Render::SmartyPants
Defined in:
lib/govuk_tech_docs/tech_docs_html_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TechDocsHTMLRenderer

Returns a new instance of TechDocsHTMLRenderer.



7
8
9
10
11
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 7

def initialize(options = {})
  @local_options = options.dup
  @app = @local_options[:context].app
  super
end

Instance Method Details

#block_code(text, lang) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 78

def block_code(text, lang)
  if defined?(super)
    # Post-processing the block_code HTML to implement tabbable code blocks.
    #
    # Middleman monkey patches the Middleman::Renderers::MiddlemanRedcarpetHTML
    # to include Middleman::Syntax::RedcarpetCodeRenderer. This defines its own
    # version of `block_code(text, lang)` which we can call with `super`.

    fragment = Nokogiri::HTML::DocumentFragment.parse(super)
    fragment.traverse do |element|
      if element.name == "pre" && element["tabindex"].nil?
        element["tabindex"] = "0"
      end
    end
    fragment.to_html
  else
    # If syntax highlighting with redcarpet isn't enabled, super will not
    # be `defined?`, so we can jump straight to rendering HTML ourselves.

    fragment = Nokogiri::HTML::DocumentFragment.parse("")
    pre = Nokogiri::XML::Node.new "pre", fragment
    pre["tabindex"] = "0"
    code = Nokogiri::XML::Node.new "code", fragment
    code["class"] = lang
    code.content = text
    pre.add_child code
    pre.to_html
  end
end

#header(text, level) ⇒ Object



17
18
19
20
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 17

def header(text, level)
  anchor = UniqueIdentifierGenerator.instance.create(text, level)
  %(<h#{level} id="#{anchor}">#{text}</h#{level}>\n)
end

#image(link, *args) ⇒ Object



22
23
24
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 22

def image(link, *args)
  %(<a href="#{link}" rel="noopener noreferrer">#{super}</a>)
end

#paragraph(text) ⇒ Object



13
14
15
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 13

def paragraph(text)
  @app.api("<p>#{text.strip}</p>\n")
end

#table(header, body) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 26

def table(header, body)
  %(<div class="table-container">
    <table>
      #{header}#{body}
    </table>
  </div>)
end

#table_row(body) ⇒ Object



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
# File 'lib/govuk_tech_docs/tech_docs_html_renderer.rb', line 34

def table_row(body)
  # Post-processing the table_cell HTML to implement row headings.
  #
  # Doing this in table_row instead of table_cell is a hack.
  #
  # Ideally, we'd use the table_cell callback like:
  #
  # def table_cell(content, alignment, header)
  #   if header
  #     "<th>#{content}</th>"
  #   elsif content.start_with? "# "
  #     "<th scope="row">#{content.sub(/^# /, "")}</th>"
  #   else
  #     "<td>#{content}</td>"
  #   end
  # end
  #
  # Sadly, Redcarpet's table_cell callback doesn't allow you to distinguish
  # table cells and table headings until https://github.com/vmg/redcarpet/commit/27dfb2a738a23aadd286ac9e7ecd61c4545d29de
  # (which is not yet released). This means we can't use the table_cell callback
  # without breaking column headers, so we're having to hack it in table_row.

  fragment = Nokogiri::HTML::DocumentFragment.parse(body)
  fragment.children.each do |cell|
    next unless cell.name == "td"
    next if cell.children.empty?

    first_child = cell.children.first
    next unless first_child.text?

    leading_text = first_child.content
    next unless leading_text.start_with?("#")

    cell.name = "th"
    cell["scope"] = "row"
    first_child.content = leading_text.sub(/# */, "")
  end

  tr = Nokogiri::XML::Node.new "tr", fragment
  tr.children = fragment.children

  tr.to_html
end