Module: IsoDoc::Function::Blocks

Included in:
Common
Defined in:
lib/isodoc/function/reqt.rb,
lib/isodoc/function/blocks.rb,
lib/isodoc/function/blocks_example_note.rb

Constant Summary collapse

EXAMPLE_TBL_ATTR =
{ class: "example_label", style: "width:82.8pt;padding:0 0 0 0;\
margin-left:0pt;vertical-align:top;" }.freeze
EXAMPLE_TD_ATTR =
{ style: "vertical-align:top;padding:0;", class: "example" }.freeze

Instance Method Summary collapse

Instance Method Details

#admonition_attrs(node) ⇒ Object



186
187
188
189
# File 'lib/isodoc/function/blocks_example_note.rb', line 186

def admonition_attrs(node)
  attr_code(id: node["id"], class: admonition_class(node),
            style: keep_style(node), coverpage: node["coverpage"])
end

#admonition_class(node) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/isodoc/function/blocks_example_note.rb', line 158

def admonition_class(node)
  # metanorma/metanorma-standoc#1197: admonition @class is unused by the
  # type-driven labelling/numbering, so it is repurposed for additive
  # custom HTML classes appended after the built-in Admonition classes.
  ["Admonition", admonition_subclass(node), node["class"]]
    .compact.join(" ").strip
end

#admonition_name(node, _type) ⇒ Object



182
183
184
# File 'lib/isodoc/function/blocks_example_note.rb', line 182

def admonition_name(node, _type)
  node&.at(ns("./fmt-name"))
end

#admonition_name_in_first_para(node, div) ⇒ Object

code to allow name and first paragraph to be rendered in same block



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/isodoc/function/blocks_example_note.rb', line 207

def admonition_name_in_first_para(node, div)
  name = node.at(ns("./fmt-name"))
  para = node.at(ns("./p"))
  div.p do |p|
    if name = admonition_name(node, node["type"])&.remove
      name.children.each { |n| parse(n, p) }
      admonition_name_para_delim(p) # TODO to Presentation XML
    end
    para.children.each { |n| parse(n, p) }
  end
  para.xpath("./following-sibling::*").each { |n| parse(n, div) }
end

#admonition_name_para_delim(para) ⇒ Object



220
221
222
# File 'lib/isodoc/function/blocks_example_note.rb', line 220

def admonition_name_para_delim(para)
  insert_tab(para, 1)
end

#admonition_name_parse(node, div, name) ⇒ Object



151
152
153
154
155
156
# File 'lib/isodoc/function/blocks_example_note.rb', line 151

def admonition_name_parse(node, div, name)
  div.p class: "AdmonitionTitle #{admonition_subclass(node)}".strip,
        style: "text-align:center;" do |p|
    children_parse(name, p)
  end
end

#admonition_p_parse(node, div) ⇒ Object

code to allow name and first paragraph to be rendered in same block



202
203
204
# File 'lib/isodoc/function/blocks_example_note.rb', line 202

def admonition_p_parse(node, div)
  admonition_parse1(node, div)
end

#admonition_parse(node, out) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/isodoc/function/blocks_example_note.rb', line 191

def admonition_parse(node, out)
  out.div(**admonition_attrs(node)) do |div|
    if starts_with_para?(node)
      admonition_p_parse(node, div)
    else
      admonition_parse1(node, div)
    end
  end
end

#admonition_parse1(node, div) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/isodoc/function/blocks_example_note.rb', line 224

def admonition_parse1(node, div)
  name = admonition_name(node, node["type"])
  if name
    admonition_name_parse(node, div, name)
  end
  node.children.each { |n| parse(n, div) unless n.name == "fmt-name" }
end

#admonition_subclass(node) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/isodoc/function/blocks_example_note.rb', line 166

def admonition_subclass(node)
  case node["type"]
  when "important" then "AdmonitionImportant"
  when "warning" then "AdmonitionWarning"
  when "caution" then "AdmonitionCaution"
  when "todo" then "AdmonitionTodo"
  when "editor" then "AdmonitionEditor"
  when "editorial" then "AdmonitionEditorial"
  when "tip" then "AdmonitionTip"
  when "statement" then "AdmonitionStatement"
  when "box" then "AdmonitionBox"
  when "safety-precaution" then "AdmonitionSafetyPrecaution"
  else "AdmonitionOther"
  end
end

#annotation_parse(node, out) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/isodoc/function/blocks.rb', line 100

def annotation_parse(node, out)
  dl = node.at(ns("./dl")) or return
  @sourcecode = false
  out.div class: "annotation" do |div|
    parse(dl, div)
  end
end

#attribution_parse(node, out) ⇒ Object



184
185
186
187
188
# File 'lib/isodoc/function/blocks.rb', line 184

def attribution_parse(node, out)
  out.div class: "QuoteAttribution" do |div|
    children_parse(node, div)
  end
end

#block_body_first_elem(node) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/isodoc/function/blocks_example_note.rb', line 77

def block_body_first_elem(node)
  node.elements.each do |n|
    %w(title fmt-title fmt-xref-label fmt-name name)
      .include?(n.name) and next
    return n
  end
  nil
end

#columnbreak_parse(node, out) ⇒ Object



240
# File 'lib/isodoc/function/blocks.rb', line 240

def columnbreak_parse(node, out); end

#cross_align_parse(node, out) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/isodoc/function/blocks.rb', line 228

def cross_align_parse(node, out)
  out.table do |t|
    t.tbody do |b|
      node.xpath(ns("./align-cell")).each do |c|
        b.td do |td|
          c.children.each { |n| parse(n, td) }
        end
      end
    end
  end
end

#div_parse(node, out) ⇒ Object



42
43
44
45
46
# File 'lib/isodoc/function/reqt.rb', line 42

def div_parse(node, out)
  out.div(**reqt_attrs(node, node["type"])) do |div|
    children_parse(node, div)
  end
end

#example_collapsible_parse(node, out) ⇒ Object

HTML5 collapsible example: label in

, body in the example div



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/isodoc/function/blocks_example_note.rb', line 63

def example_collapsible_parse(node, out)
  out.details(open: "open") do |det|
    name = node.at(ns("./fmt-name"))
    det.summary do |s|
      name and children_parse(name, s)
    end
    det.div(**example_div_attr(node)) do |div|
      node.children.each do |n|
        parse(n, div) unless n.name == "fmt-name"
      end
    end
  end
end

#example_div_attr(node) ⇒ Object



15
16
17
18
# File 'lib/isodoc/function/blocks_example_note.rb', line 15

def example_div_attr(node)
  attr_code(id: node["id"], class: merge_html_class("example", node),
            style: keep_style(node))
end

#example_div_parse(node, out) ⇒ Object

used if we are boxing examples



21
22
23
24
25
26
27
28
# File 'lib/isodoc/function/blocks_example_note.rb', line 21

def example_div_parse(node, out)
  out.div(**example_div_attr(node)) do |div|
    example_label(node, div, node.at(ns("./fmt-name")))
    node.children.each do |n|
      parse(n, div) unless n.name == "fmt-name"
    end
  end
end

#example_label(_node, div, name) ⇒ Object



4
5
6
7
8
9
# File 'lib/isodoc/function/blocks_example_note.rb', line 4

def example_label(_node, div, name)
  name.nil? and return
  div.p class: "example-title" do |_p|
    children_parse(name, div)
  end
end

#example_parse(node, out) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/isodoc/function/blocks_example_note.rb', line 54

def example_parse(node, out)
  if node["collapsible"] == "true"
    example_collapsible_parse(node, out)
  else
    example_div_parse(node, out)
  end
end

#example_table_attr(node) ⇒ Object



30
31
32
33
34
# File 'lib/isodoc/function/blocks_example_note.rb', line 30

def example_table_attr(node)
  attr_code(id: node["id"], class: "example",
            style: "border-collapse:collapse;border-spacing:0;" \
                   "#{keep_style(node)}")
end

#example_table_parse(node, out) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/isodoc/function/blocks_example_note.rb', line 39

def example_table_parse(node, out)
  out.table(**example_table_attr(node)) do |t|
    t.tr do |tr|
      tr.td(**EXAMPLE_TBL_ATTR) do |td|
        example_label(node, td, node.at(ns("./fmt-name")))
      end
      tr.td(**EXAMPLE_TD_ATTR) do |td|
        node.children.each do |n|
          parse(n, td) unless n.name == "fmt-name"
        end
      end
    end
  end
end

#figure_attrs(node) ⇒ Object



13
14
15
16
# File 'lib/isodoc/function/blocks.rb', line 13

def figure_attrs(node)
  attr_code(id: node["id"], class: merge_html_class("figure", node),
            style: keep_style(node))
end

#figure_name_parse(_node, div, name) ⇒ Object



6
7
8
9
10
11
# File 'lib/isodoc/function/blocks.rb', line 6

def figure_name_parse(_node, div, name)
  name.nil? and return
  div.figcaption do |p|
    children_parse(name, p)
  end
end

#figure_parse(node, out) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/isodoc/function/blocks.rb', line 18

def figure_parse(node, out)
  # metanorma/metanorma-standoc#1197: "pseudocode" may be one of several
  # space-separated classes; dispatch on its presence, not equality, so a
  # custom class alongside it is preserved.
  node["class"]&.split&.include?("pseudocode") ||
    node["type"] == "pseudocode" and
    return pseudocode_parse(node, out)
  @in_figure = true
  figure_parse1(node, out)
  @in_figure = false
end

#figure_parse1(node, out) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/isodoc/function/blocks.rb', line 30

def figure_parse1(node, out)
  out.figure(**figure_attrs(node)) do |div|
    node.children.each do |n|
      parse(n, div) unless n.name == "fmt-name"
    end
    figure_name_parse(node, div, node.at(ns("./fmt-name")))
  end
end

#fmt_fn_body_parse(node, out) ⇒ Object

we dont't want figure dl/dd/fmt-fn-body to be processed as actual footnotes



244
245
246
247
248
249
250
251
252
# File 'lib/isodoc/function/blocks.rb', line 244

def fmt_fn_body_parse(node, out)
  node.ancestors("table, figure").empty? and
    node.at(ns(".//fmt-fn-label"))&.remove
  tag = node.parent.name == "fmt-footnote-container" ? "aside" : "div"
  id = node["is_table"] ? node["reference"] : node["id"]
  out.send tag, id: "fn:#{id}", class: "footnote" do |div|
    children_parse(node, div)
  end
end

#formula_attrs(node) ⇒ Object



120
121
122
# File 'lib/isodoc/function/blocks.rb', line 120

def formula_attrs(node)
  attr_code(id: node["id"], style: keep_style(node))
end

#formula_parse(node, out) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/isodoc/function/blocks.rb', line 124

def formula_parse(node, out)
  out.div(**formula_attrs(node)) do |div|
    formula_parse1(node, div)
    node.children.each do |n|
      %w(stem fmt-name fmt-stem).include? n.name and next
      parse(n, div)
    end
  end
end

#formula_parse1(node, out) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/isodoc/function/blocks.rb', line 108

def formula_parse1(node, out)
  out.div(**attr_code(class: "formula")) do |div|
    div.p do |_p|
      parse(node.at(ns("./fmt-stem")), div)
      if lbl = node&.at(ns("./fmt-name"))&.text
        insert_tab(div, 1)
        div << lbl
      end
    end
  end
end

#keep_style(node) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/isodoc/function/blocks_example_note.rb', line 119

def keep_style(node)
  ret = ""
  node["style"] and ret += "#{node['style']};"
  node["keep-with-next"] == "true" and
    ret += "page-break-after: avoid;"
  node["keep-lines-together"] == "true" and
    ret += "page-break-inside: avoid;"
  return nil if ret.empty?

  ret
end

#key_name_parse(node, div) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/isodoc/function/blocks.rb', line 147

def key_name_parse(node, div)
  a = keep_style(node)
  a&.include?("page-break-after:") or
    a = "page-break-after: avoid;#{a}"
  div.p style: a do |p|
    p.b do |s|
      children_parse(node, s)
    end
  end
end

#key_parse(node, out) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/isodoc/function/blocks.rb', line 134

def key_parse(node, out)
  klass = "key #{node['class']}".strip
  out.div(**attr_code(class: klass)) do |div|
    node.children.each do |n|
      if n.name == "name"
        key_name_parse(n, div)
        "<p keep-with-next='true'><strong>#{@i18n.key}</strong></p>"
      else parse(n, div)
      end
    end
  end
end

#note_attrs(node) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/isodoc/function/blocks_example_note.rb', line 131

def note_attrs(node)
  classes = node["type"]&.split(",") || []
  classes << "Note"
  node["class"] and classes << node["class"]
  attr_code(id: node["id"], class: classes.join(" "),
            style: keep_style(node), coverpage: node["coverpage"])
end

#note_p_classObject



93
94
95
# File 'lib/isodoc/function/blocks_example_note.rb', line 93

def note_p_class
  nil
end

#note_p_parse(node, div) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/isodoc/function/blocks_example_note.rb', line 97

def note_p_parse(node, div)
  name = node.at(ns("./fmt-name"))
  para = node.at(ns("./p")) || node.at(ns("./semx/p"))
  div.p(**attr_code(class: note_p_class)) do |p|
    name and p.span class: "note_label" do |s|
      name.children.each { |n| parse(n, s) }
    end
    children_parse(para, p)
  end
  para.xpath("./following-sibling::*").each { |n| parse(n, div) }
end

#note_parse(node, out) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/isodoc/function/blocks_example_note.rb', line 139

def note_parse(node, out)
  @note = true
  out.div(**note_attrs(node)) do |div|
    if starts_with_para?(node)
      note_p_parse(node, div)
    else
      note_parse1(node, div)
    end
  end
  @note = false
end

#note_parse1(node, div) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/isodoc/function/blocks_example_note.rb', line 109

def note_parse1(node, div)
  name = node.at(ns("./fmt-name")) and
    div.p(**attr_code(class: note_p_class)) do |p|
      p.span class: "note_label" do |s|
        name.remove.children.each { |n| parse(n, s) }
      end
    end
  children_parse(node, div)
end

#para_attrs(node) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/isodoc/function/blocks.rb', line 166

def para_attrs(node)
  attrs = { class: para_class(node), id: node["id"] }
  s = node["align"].nil? ? "" : "text-align:#{node['align']};"
  s = "#{s}#{keep_style(node)}"
  attrs[:style] = s unless s.empty?
  attrs
end

#para_class(node) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/isodoc/function/blocks.rb', line 158

def para_class(node)
  classtype = nil
  classtype = "MsoCommentText" if in_comment
  node["type"] == "floating-title" and
    classtype = "h#{node['depth'] || '1'}"
  merge_html_class(classtype, node)
end

#para_parse(node, out) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/isodoc/function/blocks.rb', line 174

def para_parse(node, out)
  out.p(**attr_code(para_attrs(node))) do |p|
    para_prefix(node, p)
    node.children.each { |n| parse(n, p) unless n.name == "note" }
  end
  node.xpath(ns("./note")).each { |n| parse(n, out) }
end

#para_prefix(node, out) ⇒ Object



182
# File 'lib/isodoc/function/blocks.rb', line 182

def para_prefix(node, out); end

#passthrough_parse(node, out) ⇒ Object



204
205
206
207
208
# File 'lib/isodoc/function/blocks.rb', line 204

def passthrough_parse(node, out)
  node["formats"] &&
    !(node["formats"].split.include? @format.to_s) and return
  out.passthrough node.text
end

#permission_parse(node, out) ⇒ Object



36
37
38
39
40
# File 'lib/isodoc/function/reqt.rb', line 36

def permission_parse(node, out)
  out.div(**reqt_attrs(node, "permission")) do |t|
    recommendation_parse1(node, t)
  end
end

#pre_parse(node, out) ⇒ Object



96
97
98
# File 'lib/isodoc/function/blocks.rb', line 96

def pre_parse(node, out)
  out.pre(node.text, **attr_code(id: node["id"]))
end

#pseudocode_attrs(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/isodoc/function/blocks.rb', line 39

def pseudocode_attrs(node)
  classes = node["class"]&.split || []
  classes.include?("pseudocode") or classes.unshift("pseudocode")
  attr_code(id: node["id"], class: classes.join(" "),
            style: keep_style(node))
end

#pseudocode_parse(node, out) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/isodoc/function/blocks.rb', line 50

def pseudocode_parse(node, out)
  @in_figure = true
  name = node.at(ns("./fmt-name"))
  s = node.at(ns("./fmt-figure")) || node
  out.send pseudocode_tag, **pseudocode_attrs(node) do |div|
    s.children.each { |n| parse(n, div) unless n.name == "fmt-name" }
    sourcecode_name_parse(node, div, name)
  end
  @in_figure = false
end

#pseudocode_tagObject



46
47
48
# File 'lib/isodoc/function/blocks.rb', line 46

def pseudocode_tag
  "div"
end

#quote_attrs(node) ⇒ Object



197
198
199
200
201
202
# File 'lib/isodoc/function/blocks.rb', line 197

def quote_attrs(node)
  ret = para_attrs(node).merge(class: merge_html_class("Quote", node))
  node["type"] == "newcontent" and
    ret[:class] += " AmendNewcontent"
  ret
end

#quote_parse(node, out) ⇒ Object



190
191
192
193
194
195
# File 'lib/isodoc/function/blocks.rb', line 190

def quote_parse(node, out)
  attrs = quote_attrs(node)
  out.div(**attr_code(attrs)) do |p|
    node.children.each { |n| parse(n, p) unless n.name == "source" }
  end
end

#recommendation_name(name, out) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/isodoc/function/reqt.rb', line 4

def recommendation_name(name, out)
  return if name.nil?

  out.p class: "RecommendationTitle" do |p|
    name.children.each { |n| parse(n, p) }
  end
end

#recommendation_parse(node, out) ⇒ Object



16
17
18
19
20
# File 'lib/isodoc/function/reqt.rb', line 16

def recommendation_parse(node, out)
  out.div(**reqt_attrs(node, "recommend")) do |t|
    recommendation_parse1(node, t)
  end
end

#recommendation_parse1(node, out) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/isodoc/function/reqt.rb', line 22

def recommendation_parse1(node, out)
  p = node.at(ns("./fmt-provision")) or return
  recommendation_name(node.at(ns("./fmt-name")), out)
  p.children.each do |n|
    parse(n, out)
  end
end

#reqt_attrs(node, klass) ⇒ Object



12
13
14
# File 'lib/isodoc/function/reqt.rb', line 12

def reqt_attrs(node, klass)
  attr_code(class: klass, id: node["id"], style: keep_style(node))
end

#requirement_parse(node, out) ⇒ Object



30
31
32
33
34
# File 'lib/isodoc/function/reqt.rb', line 30

def requirement_parse(node, out)
  out.div(**reqt_attrs(node, "require")) do |t|
    recommendation_parse1(node, t)
  end
end

#source_parse(node, out) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/isodoc/function/blocks.rb', line 220

def source_parse(node, out)
  out.div class: "BlockSource" do |d|
    d.p do |p|
      children_parse(node, p)
    end
  end
end

#sourcecode_attrs(node) ⇒ Object



68
69
70
71
# File 'lib/isodoc/function/blocks.rb', line 68

def sourcecode_attrs(node)
  attr_code(id: node["id"], class: merge_html_class("Sourcecode", node),
            style: keep_style(node))
end

#sourcecode_name_parse(_node, div, name) ⇒ Object



61
62
63
64
65
66
# File 'lib/isodoc/function/blocks.rb', line 61

def sourcecode_name_parse(_node, div, name)
  name.nil? and return
  div.p class: "SourceTitle", style: "text-align:center;" do |p|
    name.children.each { |n| parse(n, p) }
  end
end

#sourcecode_parse(node, out) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/isodoc/function/blocks.rb', line 73

def sourcecode_parse(node, out)
  name = node.at(ns("./fmt-name"))
  n = node.at(ns("./fmt-sourcecode"))
  s = n || node
  out.p(**sourcecode_attrs(node)) do |div|
    sourcecode_parse1(s, div)
  end
  annotation_parse(s, out)
  sourcecode_name_parse(node, out, name)
end

#sourcecode_parse1(node, div) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/isodoc/function/blocks.rb', line 84

def sourcecode_parse1(node, div)
  @sourcecode = "pre"
  node.at(ns(".//table[@class = 'rouge-line-table']")) ||
    node.at("./ancestor::xmlns:table[@class = 'rouge-line-table']") and
    @sourcecode = "table"
  node.children.each do |n|
    %w(fmt-name dl).include?(n.name) and next
    parse(n, div)
  end
  @sourcecode = false
end

#starts_with_para?(node) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/isodoc/function/blocks_example_note.rb', line 86

def starts_with_para?(node)
  elem = block_body_first_elem(node) or return false
  elem.name == "p" || block_body_first_elem(elem)&.name == "p" ||
    (elem.elements.first&.name == "semx" &&
      block_body_first_elem(elem.elements.first)&.name == "p")
end

#svg_parse(node, out) ⇒ Object



210
211
212
# File 'lib/isodoc/function/blocks.rb', line 210

def svg_parse(node, out)
  out.parent.add_child(node)
end

#toc_parse(node, out) ⇒ Object



214
215
216
217
218
# File 'lib/isodoc/function/blocks.rb', line 214

def toc_parse(node, out)
  out.div class: "toc" do |div|
    children_parse(node, div)
  end
end