Class: Metanorma::Html::Renderers::SectionRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/html/renderers/section_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(coordinator) ⇒ SectionRenderer

Returns a new instance of SectionRenderer.



7
8
9
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 7

def initialize(coordinator)
  @coordinator = coordinator
end

Instance Method Details

#collect_ordered_children(section) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 68

def collect_ordered_children(section)
  children = []

  coordinator.walk_ordered(section) do |type, obj|
    next if %i[text tab].include?(type)

    children << obj
  end

  supplementary_attrs = %i[terms definitions]
  supplementary_attrs.each do |attr|
    val = safe_attr(section, attr)
    next if val.nil?

    Array(val).each do |v|
      children << v unless children.include?(v)
    end
  end

  children.compact!
  sort_by_displayorder(children)
end

#preface_clauses_filtered(preface, toc_filters) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 160

def preface_clauses_filtered(preface, toc_filters)
  clauses = safe_attr(preface, :clause) || safe_attr(preface, :content)
  clauses = Array(clauses)
  return clauses if toc_filters.empty?

  clauses.reject do |cl|
    cl_type = safe_attr(cl, :type)
    cl_type && toc_filters.include?(cl_type)
  end
end

#render_basic_section(section, level: 1, **_opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 11

def render_basic_section(section, level: 1, **_opts)
  attrs = element_attrs(id: safe_attr(section, :id))
  title_html = render_section_title(section, level)
  content = section.blocks&.filter_map do |block|
    coordinator.render(block)
  end&.join
  notes_html = section.notes&.filter_map do |note|
    coordinator.render_note(note)
  end&.join
  render_liquid("_section.html.liquid", {
                  "attrs" => attrs,
                  "title" => title_html,
                  "content" => content,
                  "notes" => notes_html,
                })
end

#render_content_section(section, level: 1, **_opts) ⇒ Object



42
43
44
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 42

def render_content_section(section, level: 1, **_opts)
  render_hierarchical_section(section, level: level, **_opts)
end

#render_hierarchical_section(section, level: 1, **_opts) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 28

def render_hierarchical_section(section, level: 1, **_opts)
  attrs = element_attrs(id: safe_attr(section, :id))
  title_html = render_section_title(section, level)
  content = render_section_content(section, level)
  subsections_html = section.subsections&.filter_map do |sub|
    coordinator.render(sub, level: level + 1)
  end&.join
  render_liquid("_section.html.liquid", {
                  "attrs" => attrs,
                  "title" => title_html,
                  "content" => "#{content}#{subsections_html}",
                })
end

#render_ordered_content(section, level = 1) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 91

def render_ordered_content(section, level = 1)
  children = collect_ordered_children(section)
  parts = []
  children.each do |node|
    next if node.is_a?(String)
    next if coordinator.is_title_element?(node, section)

    parts << (coordinator.render(node, level: level + 1) || "")
  end
  parts.join
end

#render_ordered_preface(preface, order, toc_filters) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 125

def render_ordered_preface(preface, order, toc_filters)
  parts = []
  order.each do |section_name|
    case section_name
    when "clause"
      clauses = preface_clauses_filtered(preface, toc_filters)
      clauses&.each do |cl|
        parts << (coordinator.render(cl, level: 1) || "")
      end
    else
      value = safe_attr(preface, section_name.to_sym)
      parts << (coordinator.render(value) || "") if value
    end
  end
  parts.join
end

#render_preface(preface, **_opts) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 114

def render_preface(preface, **_opts)
  order = coordinator.theme.preface_order
  toc_filters = coordinator.theme.toc_filter_types

  if coordinator.theme.preface_wrap
    render_wrapped_preface(preface, order, toc_filters)
  else
    render_ordered_preface(preface, order, toc_filters)
  end
end

#render_section_content(section, _level) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 57

def render_section_content(section, _level)
  parts = []
  section.blocks&.each do |block|
    parts << (coordinator.render(block) || "")
  end
  section.notes&.each do |note|
    parts << (coordinator.render_note(note) || "")
  end
  parts.join
end

#render_section_title(section, level) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 46

def render_section_title(section, level)
  titles = section.title
  return nil unless titles
  return nil if titles.is_a?(Array) && titles.empty?

  h = "h#{[[level, 6].min, 1].max}"
  title_text = coordinator.extract_title_text(titles)
  render_liquid("_heading.html.liquid", "tag" => h, "class_attr" => "",
                                        "content" => escape_html(title_text))
end

#render_wrapped_preface(preface, order, toc_filters) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 142

def render_wrapped_preface(preface, order, toc_filters)
  clauses = preface_clauses_filtered(preface, toc_filters)
  other_sections = order.reject { |n| n == "clause" }
  has_others = other_sections.any? do |name|
    val = safe_attr(preface, name.to_sym)
    val && !Array(val).empty?
  end

  return nil if clauses.empty? && !has_others

  coordinator.register_toc_entry(id: "preface", level: 1,
                                 text: "Preface")
  content = clauses.filter_map do |cl|
    coordinator.render(cl, level: 2)
  end.join
  render_liquid("_wrapped_preface.html.liquid", content: content)
end

#sort_by_displayorder(children) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/metanorma/html/renderers/section_renderer.rb', line 103

def sort_by_displayorder(children)
  children.sort_by do |node|
    order = if node.is_a?(Lutaml::Model::Serializable) &&
        node.class.attributes.key?(:displayorder)
              node.displayorder
            end
    order &&= order.to_i
    order || Float::INFINITY
  end
end