Module: Metanorma::Standoc::Blocks

Included in:
Converter
Defined in:
lib/metanorma/standoc/reqt.rb,
lib/metanorma/standoc/blocks.rb,
lib/metanorma/standoc/blocks_notes.rb

Instance Method Summary collapse

Instance Method Details

#admonition(node) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/metanorma/standoc/blocks_notes.rb', line 98

def admonition(node)
  return termnote(node) if in_terms? && node.attr("name") == "note"
  return note(node) if node.attr("name") == "note"
  return todo(node) if node.attr("name") == "todo"

  noko do |xml|
    xml.admonition **admonition_attrs(node) do |a|
      node.title.nil? or a.name { |name| name << node.title }
      wrap_in_para(node, a)
    end
  end.join("\n")
end

#admonition_attrs(node) ⇒ Object



79
80
81
82
83
# File 'lib/metanorma/standoc/blocks_notes.rb', line 79

def admonition_attrs(node)
  attr_code(keep_attrs(node).merge(id_attr(node)
    .merge(admonition_core_attrs(node)
    .merge(type: admonition_name(node)))))
end

#admonition_core_attrs(node) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/metanorma/standoc/blocks_notes.rb', line 85

def admonition_core_attrs(node)
  { notag: node.attr("notag") == "true" ? "true" : nil,
    coverpage: node.attr("coverpage") == "true" ? "true" : nil,
    beforeclauses: node.attr("beforeclauses") == "true" ? "true" : nil,
    unnumbered: node.attr("unnumbered") ||
      (node.attr("notag") == "true") || nil }
end

#admonition_name(node) ⇒ Object



93
94
95
96
# File 'lib/metanorma/standoc/blocks_notes.rb', line 93

def admonition_name(node)
  ret = node.attr("type") || node.attr("name")
  ret&.downcase
end

#boilerplate_note(node) ⇒ Object



74
75
76
77
# File 'lib/metanorma/standoc/blocks_notes.rb', line 74

def boilerplate_note(node)
  node.set_attr("type", "boilerplate")
  note(node)
end

#default_requirement_modelObject



12
13
14
# File 'lib/metanorma/standoc/reqt.rb', line 12

def default_requirement_model
  :default
end

#example(node) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/metanorma/standoc/blocks.rb', line 95

def example(node)
  (in_terms? || node.option?("termexample")) and return term_example(node)
  role = node.role || node.attr("style")
  ret = example_to_requirement(node, role) ||
    example_by_role(node, role) and return ret
  reqt_subpart?(role) and return requirement_subpart(node)
  example_proper(node)
end

#example_attrs(node) ⇒ Object



152
153
154
# File 'lib/metanorma/standoc/blocks.rb', line 152

def example_attrs(node)
  attr_code(id_unnum_attrs(node).merge(keep_attrs(node)))
end

#example_by_role(node, role) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/metanorma/standoc/blocks.rb', line 104

def example_by_role(node, role)
  case role
  when "pseudocode" then pseudocode_example(node)
  when "svgmap" then svgmap_example(node)
  when "form" then form(node)
  when "definition" then termdefinition(node)
  when "figure" then figure_example(node)
  end
end

#example_proper(node) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/metanorma/standoc/blocks.rb', line 156

def example_proper(node)
  noko do |xml|
    xml.example **example_attrs(node) do |ex|
      node.title.nil? or ex.name { |name| name << node.title }
      wrap_in_para(node, ex)
    end
  end.join("\n")
end

#example_to_requirement(node, role) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/metanorma/standoc/blocks.rb', line 114

def example_to_requirement(node, role)
  @reqt_models.requirement_roles.key?(role&.to_sym) or return
  # need to call here for proper recursion ordering
  select_requirement_model(node)
  requirement(node,
              @reqt_models.requirement_roles[role.to_sym], role)
end

#figure_attrs(node) ⇒ Object



179
180
181
182
# File 'lib/metanorma/standoc/blocks.rb', line 179

def figure_attrs(node)
  attr_code(id_unnum_attrs(node).merge(keep_attrs(node))
    .merge(class: node.attr("class")))
end

#figure_example(node) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/metanorma/standoc/blocks.rb', line 165

def figure_example(node)
  noko do |xml|
    xml.figure **figure_attrs(node) do |ex|
      node.title.nil? or ex.name { |name| name << node.title }
      wrap_in_para(node, ex)
    end
  end.join("\n")
end

#figure_title(node, out) ⇒ Object



174
175
176
177
# File 'lib/metanorma/standoc/blocks.rb', line 174

def figure_title(node, out)
  node.title.nil? and return
  out.name { |name| name << node.title }
end

#form(node) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/metanorma/standoc/blocks.rb', line 52

def form(node)
  noko do |xml|
    xml.form **form_attrs(node) do |f|
      f << node.content
    end
  end
end

#form_attrs(node) ⇒ Object



46
47
48
49
50
# File 'lib/metanorma/standoc/blocks.rb', line 46

def form_attrs(node)
  attr_code(id_attr(node)
    .merge(class: node.attr("class"),
           name: node.attr("name"), action: node.attr("action")))
end

#formula_attrs(node) ⇒ Object



22
23
24
25
26
27
# File 'lib/metanorma/standoc/blocks.rb', line 22

def formula_attrs(node)
  attr_code(id_unnum_attrs(node)
    .merge(keep_attrs(node).merge(
             inequality: node.option?("inequality") ? "true" : nil,
           )))
end

#id_attr(node = nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/metanorma/standoc/blocks.rb', line 7

def id_attr(node = nil)
  { id: Metanorma::Utils::anchor_or_uuid(node),
    tag: node&.attr("tag"),
    columns: node&.attr("columns"),
    "multilingual-rendering": node&.attr("multilingual-rendering") }
end

#id_unnum_attrs(node) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/metanorma/standoc/blocks.rb', line 14

def id_unnum_attrs(node)
  attr_code(id_attr(node).merge(
              unnumbered: node.option?("unnumbered") ? "true" : nil,
              number: node.attr("number"),
              subsequence: node.attr("subsequence"),
            ))
end

#image(node) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/metanorma/standoc/blocks.rb', line 184

def image(node)
  noko do |xml|
    xml.figure **figure_attrs(node) do |f|
      figure_title(node, f)
      f.image **image_attributes(node)
    end
  end
end

#keep_attrs(node) ⇒ Object



29
30
31
32
# File 'lib/metanorma/standoc/blocks.rb', line 29

def keep_attrs(node)
  { "keep-with-next": node.attr("keep-with-next"),
    "keep-lines-together": node.attr("keep-lines-together") }
end

#listing(node) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/metanorma/standoc/blocks.rb', line 246

def listing(node)
  fragment = ::Nokogiri::XML::Builder.new do |xml|
    xml.sourcecode **listing_attrs(node) do |s|
      figure_title(node, s)
      s << node.content
    end
  end
  fragment.to_xml(encoding: "US-ASCII", save_with:
                  Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
end

#listing_attrs(node) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/metanorma/standoc/blocks.rb', line 235

def listing_attrs(node)
  linenums = node.option?("linenums") || node.attributes[3] ||
    @source_linenums
  attr_code(id_attr(node).merge(keep_attrs(node)
            .merge(lang: node.attr("language"),
                   linenums: linenums ? "true" : nil,
                   unnumbered: node.option?("unnumbered") ? "true" : nil,
                   number: node.attr("number"),
                   filename: node.attr("filename"))))
end

#literal(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/metanorma/standoc/blocks.rb', line 64

def literal(node)
  noko do |xml|
    xml.figure **literal_attrs(node) do |f|
      figure_title(node, f)
      f.pre node.lines.join("\n"),
            **attr_code(id: Metanorma::Utils::anchor_or_uuid,
                        alt: node.attr("alt"))
    end
  end
end

#literal_attrs(node) ⇒ Object



60
61
62
# File 'lib/metanorma/standoc/blocks.rb', line 60

def literal_attrs(node)
  attr_code(id_attr(node).merge(keep_attrs(node)))
end

#note(node) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/metanorma/standoc/blocks_notes.rb', line 64

def note(node)
  return termnote(node) if node.option?("termnote")

  noko do |xml|
    xml.note **note_attrs(node) do |c|
      wrap_in_para(node, c)
    end
  end.join("\n")
end

#note_attrs(node) ⇒ Object



15
16
17
18
# File 'lib/metanorma/standoc/blocks_notes.rb', line 15

def note_attrs(node)
  attr_code(termnote_attrs(node).merge(admonition_core_attrs(node)
    .merge(type: node.attr("type"))))
end

#open(node) ⇒ Object

We append each contained block to its parent



35
36
37
38
39
40
41
42
43
44
# File 'lib/metanorma/standoc/blocks.rb', line 35

def open(node)
  role = node.role || node.attr("style")
  reqt_subpart?(role) and return requirement_subpart(node)
  role == "form" and return form(node)
  role == "definition" and return termdefinition(node)
  role == "boilerplate" and return boilerplate_note(node)
  result = []
  node.blocks.each { |b| result << send(b.context, b) }
  result
end

#para_attrs(node) ⇒ Object



193
194
195
196
197
198
# File 'lib/metanorma/standoc/blocks.rb', line 193

def para_attrs(node)
  attr_code(id_attr(node).merge(keep_attrs(node)
    .merge(align: node.attr("align"),
           variant_title: node.role == "variant-title" ? true : nil,
           type: node.attr("type"))))
end

#paragraph(node) ⇒ Object

term sources occasionally turning up as “source source”?



201
202
203
204
205
206
207
208
# File 'lib/metanorma/standoc/blocks.rb', line 201

def paragraph(node)
  node.role&.sub(/ .*$/, "") == "source" and return termsource(node)
  noko do |xml|
    xml.p **para_attrs(node) do |xml_t|
      xml_t << node.content
    end
  end.join("\n")
end

#pass(node) ⇒ Object



257
258
259
260
261
262
263
264
# File 'lib/metanorma/standoc/blocks.rb', line 257

def pass(node)
  noko do |xml|
    xml.passthrough **attr_code(formats:
                                node.attr("format") || "metanorma") do |p|
      p << @c.encode(@c.decode(node.content), :basic, :hexadecimal)
    end
  end
end

#pseudocode_example(node) ⇒ Object

prevent A’s and other subs inappropriate for pseudocode



142
143
144
145
146
147
148
149
150
# File 'lib/metanorma/standoc/blocks.rb', line 142

def pseudocode_example(node)
  node.blocks.each { |b| b.remove_sub(:replacements) }
  noko do |xml|
    xml.figure **example_attrs(node).merge(class: "pseudocode") do |ex|
      figure_title(node, ex)
      wrap_in_para(node, ex)
    end
  end.join("\n")
end

#quote(node) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/metanorma/standoc/blocks.rb', line 226

def quote(node)
  noko do |xml|
    xml.quote **quote_attrs(node) do |q|
      quote_attribution(node, q)
      wrap_in_para(node, q)
    end
  end.join("\n")
end

#quote_attribution(node, out) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/metanorma/standoc/blocks.rb', line 215

def quote_attribution(node, out)
  if node.attr("citetitle")
    m = /^(?<cite>[^,]+)(?:,(?<text>.*$))?$/m.match node.attr("citetitle")
    out.source **attr_code(target: m[:cite], type: "inline") do |s|
      s <<  m[:text]
    end
  end
  node.attr("attribution") and
    out.author { |a| a << node.attr("attribution") }
end

#quote_attrs(node) ⇒ Object



210
211
212
213
# File 'lib/metanorma/standoc/blocks.rb', line 210

def quote_attrs(node)
  attr_code(id_attr(node).merge(keep_attrs(node))
    .merge(align: node.attr("align")))
end

#reqt_subpart?(name) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/metanorma/standoc/reqt.rb', line 4

def reqt_subpart?(name)
  @reqt_model&.reqt_subpart?(name)
end

#requirement(node, obligation, type) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/metanorma/standoc/reqt.rb', line 23

def requirement(node, obligation, type)
  nested = @reqt_model
  !node.attr("type") &&
    !%w(requirement recommendation permission).include?(type) and
    node.set_attr("type", type)
  attrs = keep_attrs(node).merge(id_unnum_attrs(node))
    .merge(model: @reqt_model_name)
  ret = @reqt_model.requirement(node, obligation, attrs)
  @reqt_model = nil unless nested
  ret
end

#requirement_subpart(node) ⇒ Object



8
9
10
# File 'lib/metanorma/standoc/reqt.rb', line 8

def requirement_subpart(node)
  @reqt_model.requirement_subpart(node, keep_attrs(node))
end

#requirement_validate(docxml) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/metanorma/standoc/reqt.rb', line 35

def requirement_validate(docxml)
  docxml.xpath("//requirement | //recommendation | //permission")
    .each_with_object([]) do |r, m|
    ret = @reqt_models.model(r["model"]).validate(r, @log)
    ret.each { |x| m << x }
  end
end

#select_requirement_model(node) ⇒ Object



16
17
18
19
20
21
# File 'lib/metanorma/standoc/reqt.rb', line 16

def select_requirement_model(node)
  return if @reqt_model

  @reqt_model_name = node.attr("model") || @default_requirement_model
  @reqt_model = @reqt_models.model(@reqt_model_name)
end


29
30
31
32
33
34
35
36
37
# File 'lib/metanorma/standoc/blocks_notes.rb', line 29

def sidebar(node)
  return unless draft?

  noko do |xml|
    xml.review **sidebar_attrs(node) do |r|
      wrap_in_para(node, r)
    end
  end.join("\n")
end


20
21
22
23
24
25
26
27
# File 'lib/metanorma/standoc/blocks_notes.rb', line 20

def sidebar_attrs(node)
  todo_attrs(node).merge(
    attr_code(
      from: node.attr("from"),
      to: node.attr("to") || node.attr("from"),
    ),
  )
end

#stem(node) ⇒ Object

NOTE: html escaping is performed by Nokogiri



76
77
78
79
80
81
82
# File 'lib/metanorma/standoc/blocks.rb', line 76

def stem(node)
  noko do |xml|
    xml.formula **formula_attrs(node) do |s|
      stem_parse(node.lines.join("\n"), s, node.style.to_sym)
    end
  end
end

#svgmap_attrs(node) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/metanorma/standoc/blocks.rb', line 122

def svgmap_attrs(node)
  attr_code(id_attr(node)
    .merge(id: node.id, number: node.attr("number"),
           unnumbered: node.option?("unnumbered") ? "true" : nil,
           subsequence: node.attr("subsequence"))
  .merge(keep_attrs(node)))
end

#svgmap_example(node) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/metanorma/standoc/blocks.rb', line 130

def svgmap_example(node)
  noko do |xml|
    xml.svgmap **attr_code(svgmap_attrs(node).merge(
                             src: node.attr("src"), alt: node.attr("alt"),
                           )) do |ex|
      figure_title(node, ex)
      ex << node.content
    end
  end.join("\n")
end

#term_example(node) ⇒ Object



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

def term_example(node)
  noko do |xml|
    xml.termexample **attr_code(id_attr(node)
      .merge(
        keepasterm: node.option?("termexample") || nil,
      )) do |ex|
      wrap_in_para(node, ex)
    end
  end.join("\n")
end

#termnote(node) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/metanorma/standoc/blocks_notes.rb', line 56

def termnote(node)
  noko do |xml|
    xml.termnote **termnote_attrs(node) do |ex|
      wrap_in_para(node, ex)
    end
  end.join("\n")
end

#termnote_attrs(node) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/metanorma/standoc/blocks_notes.rb', line 4

def termnote_attrs(node)
  attr_code(id_attr(node).merge(keep_attrs(node)
    .merge(
      unnumbered: node.attr("unnumbered"),
      number: node.attr("number"),
      subsequence: node.attr("subsequence"),
      "keep-separate": node.attr("keep-separate"),
      keepasterm: node.option?("termnote") ? "true" : nil,
    )))
end

#todo(node) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/metanorma/standoc/blocks_notes.rb', line 48

def todo(node)
  noko do |xml|
    xml.review **todo_attrs(node) do |r|
      wrap_in_para(node, r)
    end
  end.join("\n")
end

#todo_attrs(node) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/metanorma/standoc/blocks_notes.rb', line 39

def todo_attrs(node)
  date = node.attr("date") || Date.today.iso8601.gsub(/\+.*$/, "")
  date += "T00:00:00Z" unless date.include?("T")
  attr_code(id_attr(node)
    .merge(reviewer: node.attr("reviewer") || node.attr("source") ||
           "(Unknown)",
           date: date))
end