Module: Metanorma::Standoc::Blocks

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

Instance Method Summary collapse

Instance Method Details

#admonition(node) ⇒ Object



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

def admonition(node)
  ret = admonition_alternatives(node) and return ret
  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
end

#admonition_alternatives(node) ⇒ Object



107
108
109
110
111
112
# File 'lib/metanorma/standoc/blocks_notes.rb', line 107

def admonition_alternatives(node)
  in_terms? && node.attr("name") == "note" and return termnote(node)
  node.attr("name") == "note" and return note(node)
  node.attr("name") == "todo" and return todo(node)
  nil
end

#admonition_attrs(node) ⇒ Object



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

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



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

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



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

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

#boilerplate_note(node) ⇒ Object



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

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



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

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

#example_attrs(node) ⇒ Object



134
135
136
# File 'lib/metanorma/standoc/blocks.rb', line 134

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

#example_by_role(node, role) ⇒ Object



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

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



138
139
140
141
142
143
144
145
# File 'lib/metanorma/standoc/blocks.rb', line 138

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
end

#example_to_requirement(node, role) ⇒ Object



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

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



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

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

#figure_example(node) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/metanorma/standoc/blocks_image.rb', line 23

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
end

#figure_title(node, out) ⇒ Object



32
33
34
35
# File 'lib/metanorma/standoc/blocks_image.rb', line 32

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

#form(node) ⇒ Object



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

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

#form_attrs(node) ⇒ Object



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

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



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

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



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

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



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

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



43
44
45
46
47
48
49
50
# File 'lib/metanorma/standoc/blocks_image.rb', line 43

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



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

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

#listing(node) ⇒ Object



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

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



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

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



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

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



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

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

#note(node) ⇒ Object



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

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
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



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

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



147
148
149
150
151
152
# File 'lib/metanorma/standoc/blocks.rb', line 147

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”?



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

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
end

#pass(node) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/metanorma/standoc/blocks.rb', line 211

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)
      p << @c.encode(node.content, :basic, :hexadecimal)
    end
  end
end

#pseudocode_example(node) ⇒ Object

prevent A’s and other subs inappropriate for pseudocode



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

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
end

#quote(node) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/metanorma/standoc/blocks.rb', line 180

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

#quote_attribution(node, out) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/metanorma/standoc/blocks.rb', line 169

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



164
165
166
167
# File 'lib/metanorma/standoc/blocks.rb', line 164

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
# File 'lib/metanorma/standoc/reqt.rb', line 35

def requirement_validate(docxml)
  docxml.xpath("//requirement | //recommendation | //permission")
    .each do |r|
    @reqt_models.model(r["model"]).validate(r, @log)
  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


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

def sidebar(node)
  noko do |xml|
    xml.review **sidebar_attrs(node) do |r|
      wrap_in_para(node, r)
    end
  end
end


20
21
22
23
24
25
26
27
28
# 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"),
      type: node.attr("type") || nil,
    ),
  )
end

#stem(node) ⇒ Object

NOTE: html escaping is performed by Nokogiri



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

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

#svgmap_attrs(node) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/metanorma/standoc/blocks_image.rb', line 4

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



12
13
14
15
16
17
18
19
20
21
# File 'lib/metanorma/standoc/blocks_image.rb', line 12

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
end

#term_example(node) ⇒ Object



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

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
end

#termnote(node) ⇒ Object



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

def termnote(node)
  noko do |xml|
    xml.termnote **termnote_attrs(node) do |ex|
      wrap_in_para(node, ex)
    end
  end
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



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

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

#todo_attrs(node) ⇒ Object



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

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:, type: "todo"))
end