Module: GovukMarkdown::Renderer::Mixin

Included in:
GovukMarkdown::Renderer
Defined in:
lib/govuk_markdown/renderer/mixin.rb

Overview

Renderer::Mixin overrides methods in Redcarpet::Render::HTML. See GovukMarkdown::Renderer for an example of how to include it.

Instance Method Summary collapse

Instance Method Details

#header(text, header_level) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/govuk_markdown/renderer/mixin.rb', line 42

def header(text, header_level)
  valid_header_sizes = %w[xl l m s].freeze

  start_size = valid_header_sizes.include?(@headings_start_with) ? @headings_start_with : "xl"

  start_size_index = valid_header_sizes.find_index(start_size)

  header_size = valid_header_sizes[start_size_index + header_level - 1] || "s"

  id_attribute = @options[:with_toc_data] ? " id=\"#{text.parameterize}\"" : ""
  "    <h\#{header_level}\#{id_attribute} class=\"govuk-heading-\#{header_size}\">\#{text}</h\#{header_level}>\n  HTML\nend\n"

#hrule ⇒ Object



82
83
84
85
86
# File 'lib/govuk_markdown/renderer/mixin.rb', line 82

def hrule
  "    <hr class=\"govuk-section-break govuk-section-break--xl govuk-section-break--visible\">\n  HTML\nend\n"

#list(contents, list_type) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/govuk_markdown/renderer/mixin.rb', line 63

def list(contents, list_type)
  case list_type
  when :unordered
    "      <ul class=\"govuk-list govuk-list--bullet\">\n        \#{contents}\n      </ul>\n    HTML\n  when :ordered\n    <<~HTML\n      <ol class=\"govuk-list govuk-list--number\">\n        \#{contents}\n      </ol>\n    HTML\n  else\n    raise \"Unexpected type \#{list_type.inspect}\"\n  end\nend\n"

#paragraph(text) ⇒ Object



57
58
59
60
61
# File 'lib/govuk_markdown/renderer/mixin.rb', line 57

def paragraph(text)
  "    <p class=\"govuk-body-m\">\#{text}</p>\n  HTML\nend\n"

#preprocess(document) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/govuk_markdown/renderer/mixin.rb', line 88

def preprocess(document)
  Preprocessor
    .new(document)
    .inject_inset_text
    .inject_details
    .strip_front_matter(@strip_front_matter)
    .output
end

#table(header, body) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/govuk_markdown/renderer/mixin.rb', line 5

def table(header, body)
  "    <table class='govuk-table'>\n      <thead class='govuk-table__head'>\n        \#{header}\n      </thead>\n      <tbody class='govuk-table__body'>\n        \#{body}\n      </tbody>\n    </table>\n  HTML\nend\n"

#table_cell(content, _alignment, header) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/govuk_markdown/renderer/mixin.rb', line 26

def table_cell(content, _alignment, header)
  if header
    "      <th class='govuk-table__header'>\n        \#{content}\n      </th>\n    HTML\n  else\n    <<~HTML\n      <td class='govuk-table__cell'>\n        \#{content}\n      </td>\n    HTML\n  end\nend\n"

#table_row(content) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/govuk_markdown/renderer/mixin.rb', line 18

def table_row(content)
  "    <tr class='govuk-table__row'>\n      \#{content}\n    </tr>\n  HTML\nend\n"