Module: Metanorma::Standoc::Section

Included in:
Converter
Defined in:
lib/metanorma/standoc/terms.rb,
lib/metanorma/standoc/section.rb,
lib/metanorma/standoc/ref_sect.rb

Constant Summary collapse

TERM_REFERENCE_RE_STR =
<<~REGEXP.freeze
  ^(?<xref><(xref|concept)[^>]+>(.*?</(xref|concept)>)?)
         (,\s(?<text>.*))?
  $
REGEXP
TERM_REFERENCE_RE =
Regexp.new(TERM_REFERENCE_RE_STR.gsub(/\s/, "").gsub(/_/, "\\s"),
Regexp::IGNORECASE | Regexp::MULTILINE)
PREFACE_CLAUSE_NAMES =
%w(abstract foreword introduction metanorma-extension termdocsource
misc-container acknowledgements).freeze
MAIN_CLAUSE_NAMES =
["normative references", "terms and definitions", "scope",
"symbols and abbreviated terms", "clause", "bibliography"].freeze

Instance Method Summary collapse

Instance Method Details

#abstract_parse(attrs, xml, node) ⇒ Object



180
181
182
183
184
# File 'lib/metanorma/standoc/section.rb', line 180

def abstract_parse(attrs, xml, node)
  xml.abstract **attr_code(attrs) do |xml_section|
    xml_section << node.content
  end
end

#acknowledgements_parse(attrs, xml, node) ⇒ Object



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

def acknowledgements_parse(attrs, xml, node)
  xml.acknowledgements **attr_code(attrs) do |xml_section|
    xml_section.title { |t| (t << node.title) || @i18n.acknowledgements }
    content = node.content
    xml_section << content
  end
end

#add_term_source(node, xml_t, seen_xref, match) ⇒ Object



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

def add_term_source(node, xml_t, seen_xref, match)
  attrs = {}
  body = seen_xref.children[0]
  unless body.name == "concept"
    attrs = termsource_origin_attrs(node, seen_xref)
    body = body.children
  end
  xml_t.origin **attr_code(attrs) do |o|
    o << body.to_xml
  end
  add_term_source_mod(xml_t, match)
end

#add_term_source_mod(xml_t, match) ⇒ Object



137
138
139
140
141
# File 'lib/metanorma/standoc/terms.rb', line 137

def add_term_source_mod(xml_t, match)
  match[:text] && xml_t.modification do |mod|
    mod.p { |p| p << match[:text].sub(/^\s+/, "") }
  end
end

#annex_parse(attrs, xml, node) ⇒ Object



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

def annex_parse(attrs, xml, node)
  attrs[:"inline-header"] = node.option? "inline-header"
  set_obligation(attrs, node)
  xml.annex **attr_code(attrs) do |xml_section|
    xml_section.title { |name| name << node.title }
    xml_section << node.content
  end
end

#bibitem_parse(attrs, xml, node) ⇒ Object



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

def bibitem_parse(attrs, xml, node)
  norm_ref = @norm_ref
  biblio = @biblio
  @biblio = false
  @norm_ref = false
  ret = clause_parse(attrs, xml, node)
  @biblio = biblio
  @norm_ref = norm_ref
  ret
end

#biblio_prep(attrs, xml, node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/metanorma/standoc/ref_sect.rb', line 45

def biblio_prep(attrs, xml, node)
  if node.option? "bibitem"
    bibitem_parse(attrs, xml, node)
  else
    node.attr("style") == "bibliography" or
      @log.add("AsciiDoc Input", node,
               "Section not marked up as [bibliography]!")
    nil
  end
end

#bibliography_parse(attrs, xml, node) ⇒ Object



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

def bibliography_parse(attrs, xml, node)
  x = biblio_prep(attrs, xml, node) and return x
  @biblio = true
  attrs = attrs.merge(normative: node.attr("normative") || false)
  xml.references **attr_code(attrs) do |xml_section|
    xml_section.title { |t| t << node.title }
    xml_section << node.content
  end
  @biblio = false
end

#clause_parse(attrs, xml, node) ⇒ Object



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

def clause_parse(attrs, xml, node)
  attrs[:"inline-header"] = node.option? "inline-header"
  attrs[:bibitem] = true if node.option? "bibitem"
  attrs[:level] = node.attr("level")
  set_obligation(attrs, node)
  xml.send :clause, **attr_code(attrs) do |xml_section|
    xml_section.title { |n| n << node.title } unless node.title.nil?
    xml_section << node.content
  end
end

#emend_biblio(xml, code, title, usrlbl) ⇒ Object



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

def emend_biblio(xml, code, title, usrlbl)
  emend_biblio_id(xml, code)
  emend_biblio_title(xml, code, title)
  emend_biblio_usrlbl(xml, usrlbl)
end

#emend_biblio_id(xml, code) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/metanorma/standoc/ref_sect.rb', line 62

def emend_biblio_id(xml, code)
  unless xml.at("/bibitem/docidentifier[not(@type = 'DOI')][text()]") ||
      /^doi:/.match?(code)
    @log.add("Bibliography", nil,
             "ERROR: No document identifier retrieved for #{code}")
    xml.root << "<docidentifier>#{code}</docidentifier>"
  end
end

#emend_biblio_title(xml, code, title) ⇒ Object

supply title if missing; add title with spans in it as formattedref, to emend bibitem with later



73
74
75
76
77
78
79
80
81
82
# File 'lib/metanorma/standoc/ref_sect.rb', line 73

def emend_biblio_title(xml, code, title)
  fmt = /<span class=|<fn/.match?(title)
  unless xml.at("/bibitem/title[text()]")
    @log.add("Bibliography", nil,
             "ERROR: No title retrieved for #{code}")
    !fmt and
    xml.root << "<title>#{title || '(MISSING TITLE)'}</title>"
  end
  fmt and xml.root << "<formattedref>#{title}</formattedref>"
end

#emend_biblio_usrlbl(xml, usrlbl) ⇒ Object



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

def emend_biblio_usrlbl(xml, usrlbl)
  usrlbl or return
  xml.at("/bibitem/docidentifier").next =
    "<docidentifier type='metanorma'>#{mn_code(usrlbl)}</docidentifier>"
end

#extract_termsource_refs(text, node) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/metanorma/standoc/terms.rb', line 152

def extract_termsource_refs(text, node)
  matched = TERM_REFERENCE_RE.match text
  matched.nil? and @log.add("AsciiDoc Input", node,
                            "term reference not in expected format:" \
                            "#{text}", severity: 1)
  matched
end

#floating_title(node) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/metanorma/standoc/section.rb', line 240

def floating_title(node)
  noko do |xml|
    xml.floating_title **floating_title_attrs(node) do |xml_t|
      xml_t << node.title
    end
  end.join("\n")
end

#floating_title_attrs(node) ⇒ Object



234
235
236
237
238
# File 'lib/metanorma/standoc/section.rb', line 234

def floating_title_attrs(node)
  attr_code(id_attr(node)
    .merge(align: node.attr("align"), depth: node.level,
           type: "floating-title"))
end

#foreword_parse(attrs, xml, node) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/metanorma/standoc/section.rb', line 218

def foreword_parse(attrs, xml, node)
  xml.foreword **attr_code(attrs) do |xml_section|
    xml_section.title { |t| t << node.title }
    content = node.content
    xml_section << content
  end
end

#in_biblio?Boolean

Returns:

  • (Boolean)


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

def in_biblio?
  @biblio
end

#in_norm_ref?Boolean

Returns:

  • (Boolean)


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

def in_norm_ref?
  @norm_ref
end

#in_terms?Boolean

Returns:

  • (Boolean)


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

def in_terms?
  @term_def
end

#indexsect_parse(attrs, xml, node) ⇒ Object



173
174
175
176
177
178
# File 'lib/metanorma/standoc/section.rb', line 173

def indexsect_parse(attrs, xml, node)
  xml.indexsect **attr_code(attrs) do |xml_section|
    xml_section.title { |name| name << node.title }
    xml_section << node.content
  end
end

#introduction_parse(attrs, xml, node) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/metanorma/standoc/section.rb', line 210

def introduction_parse(attrs, xml, node)
  xml.introduction **attr_code(attrs) do |xml_section|
    xml_section.title { |t| t << @i18n.introduction }
    content = node.content
    xml_section << content
  end
end

#misccontainer_parse(_attrs, xml, node) ⇒ Object



167
168
169
170
171
# File 'lib/metanorma/standoc/section.rb', line 167

def misccontainer_parse(_attrs, xml, node)
  xml.send :"misc-container-clause" do |xml_section|
    xml_section << node.content
  end
end

#nonterm_symbols_parse(attrs, xml, node) ⇒ Object



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

def nonterm_symbols_parse(attrs, xml, node)
  stash_symbols
  clause_parse(attrs, xml, node)
  pop_symbols
end

#nonterm_term_def_subclause_parse(attrs, xml, node) ⇒ Object



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

def nonterm_term_def_subclause_parse(attrs, xml, node)
  stash_term_def
  clause_parse(attrs, xml, node)
  pop_term_def
end

#norm_ref_parse(attrs, xml, node) ⇒ Object



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

def norm_ref_parse(attrs, xml, node)
  x = biblio_prep(attrs, xml, node) and return x
  @norm_ref = true
  attrs = attrs.merge(normative: node.attr("normative") || true)
  xml.references **attr_code(attrs) do |xml_section|
    xml_section.title { |t| t << node.title }
    xml_section << node.content
  end
  @norm_ref = false
end

#pop_symbolsObject



65
66
67
# File 'lib/metanorma/standoc/terms.rb', line 65

def pop_symbols
  @definitions = @stashed_definitions.pop
end

#pop_term_defObject



55
56
57
# File 'lib/metanorma/standoc/terms.rb', line 55

def pop_term_def
  @term_def = @stashed_term_def.pop
end

#preamble(node) ⇒ Object



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

def preamble(node)
  noko do |xml|
    xml.foreword **attr_code(section_attributes(node)) do |xml_abstract|
      xml_abstract.title do |t|
        t << (node.blocks[0].title || @i18n.foreword)
      end
      xml_abstract << node.content
    end
  end.join("\n")
end

#preface_main_filter(ret, node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/metanorma/standoc/section.rb', line 72

def preface_main_filter(ret, node)
  start_main_section(ret, node)
  if @preface
    self.class::MAIN_CLAUSE_NAMES.include?(ret) and return nil
  else
    self.class::PREFACE_CLAUSE_NAMES.include?(ret) and return nil
  end

  ret
end

#role_style(node, value) ⇒ Object



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

def role_style(node, value)
  node.role == value || node.attr("style") == value
end

#scope_parse(attrs, xml, node) ⇒ Object



186
187
188
# File 'lib/metanorma/standoc/section.rb', line 186

def scope_parse(attrs, xml, node)
  clause_parse(attrs.merge(type: "scope"), xml, node)
end

#section(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/metanorma/standoc/section.rb', line 104

def section(node)
  a = section_attributes(node)
  noko do |xml|
    case sectiontype(node)
    when "misc-container", "metanorma-extension"
      misccontainer_parse(a, xml, node)
    when "introduction" then introduction_parse(a, xml, node)
    when "foreword" then foreword_parse(a, xml, node)
    when "scope" then scope_parse(a, xml, node)
    when "normative references" then norm_ref_parse(a, xml, node)
    when "terms and definitions"
      @term_def = true
      term_def_parse(a, xml, node, true)
      @term_def = false
    when "symbols and abbreviated terms"
      symbols_parse(symbols_attrs(node, a), xml, node)
    when "acknowledgements"
      acknowledgements_parse(a, xml, node)
    when "bibliography"
      bibliography_parse(a, xml, node)
    else
      if @term_def then term_def_subclause_parse(a, xml, node)
      elsif @definitions then symbols_parse(a, xml, node)
      elsif @norm_ref ||
          (node.attr("style") == "bibliography" &&
           sectiontype(node, false) == "normative references")
        norm_ref_parse(a, xml, node)
      elsif @biblio || node.attr("style") == "bibliography"
        bibliography_parse(a, xml, node)
      elsif node.attr("style") == "abstract"
        abstract_parse(a, xml, node)
      elsif node.attr("style") == "index"
        indexsect_parse(a, xml, node)
      elsif node.attr("style") == "appendix" && node.level == 1
        annex_parse(a, xml, node)
      else
        clause_parse(a, xml, node)
      end
    end
  end.join("\n")
end

#section_attributes(node) ⇒ Object



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

def section_attributes(node)
  ret =
    { id: Metanorma::Utils::anchor_or_uuid(node),
      annex: role_style(node, "appendix") && node.level == 1 ? true : nil,
      colophon: role_style(node, "colophon") ? true : nil,
      preface: role_style(node, "preface") ? true : nil }
  %w(language script number branch-number type tag keeptitle
     multilingual-rendering).each do |k|
    a = node.attr(k) and ret[k.to_sym] = a
  end
  section_attributes_change(node, ret).compact
end

#section_attributes_change(node, ret) ⇒ Object



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

def section_attributes_change(node, ret)
  node.attributes["change"] or return ret
  ret.merge(change: node.attributes["change"],
            path: node.attributes["path"],
            path_end: node.attributes["path_end"],
            title: node.attributes["title"])
end

#sectiontype(node, level = true) ⇒ Object



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

def sectiontype(node, level = true)
  ret = sectiontype1(node)
  ret1 = preface_main_filter(sectiontype_streamline(ret), node)
  ret1 == "symbols and abbreviated terms" and return ret1
  !level || node.level == 1 || node.attr("heading") or return nil
  @seen_headers.include? ret and return nil
  @seen_headers << ret unless ret1.nil?
  @seen_headers_canonical << ret1 unless ret1.nil?
  ret1
end

#sectiontype1(node) ⇒ Object



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

def sectiontype1(node)
  node.attr("style") == "abstract" and return "abstract"
  node.attr("heading")&.downcase ||
    node.title
      .gsub(%r{<index>.*?</index>}m, "")
      .gsub(%r{<fn[^>]*>.*?</fn>}m, "")
      .gsub(/<[^>]+>/, "")
      .strip.downcase
      .sub(/\.$/, "")
end

#sectiontype_streamline(ret) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/metanorma/standoc/section.rb', line 34

def sectiontype_streamline(ret)
  case ret
  when "terms and definitions",
    "terms, definitions, symbols and abbreviated terms",
    "terms, definitions, symbols and abbreviations",
    "terms, definitions and symbols",
    "terms, definitions and abbreviations",
    "terms, definitions and abbreviated terms"
    "terms and definitions"
  when "symbols and abbreviated terms",
    "symbols", "abbreviated terms", "abbreviations"
    "symbols and abbreviated terms"
  when "acknowledgements", "acknowledgments"
    "acknowledgements"
  else
    ret
  end
end

#set_obligation(attrs, node) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/metanorma/standoc/section.rb', line 146

def set_obligation(attrs, node)
  attrs[:obligation] =
    if node.attributes.has_key?("obligation")
      node.attr("obligation")
    elsif node.parent.attributes.has_key?("obligation")
      node.parent.attr("obligation")
    else "normative"
    end
end

#start_main_section(ret, node) ⇒ Object



65
66
67
68
69
70
# File 'lib/metanorma/standoc/section.rb', line 65

def start_main_section(ret, node)
  role_style(node, "preface") and return
  @preface = false if self.class::MAIN_CLAUSE_NAMES.include?(ret)
  @preface = false if self.class::PREFACE_CLAUSE_NAMES
    .intersection(@seen_headers_canonical + [ret]).empty?
end

#stash_symbolsObject



59
60
61
62
63
# File 'lib/metanorma/standoc/terms.rb', line 59

def stash_symbols
  @stashed_definitions ||= []
  @stashed_definitions.push(@definitions)
  @definitions = false
end

#stash_term_defObject



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

def stash_term_def
  @stashed_term_def ||= []
  @stashed_term_def.push(@term_def)
  @term_def = false
end

#symbols_attrs(node, attr) ⇒ Object



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

def symbols_attrs(node, attr)
  case sectiontype1(node)
  when "symbols" then attr.merge(type: "symbols")
  when "abbreviated terms", "abbreviations"
    attr.merge(type: "abbreviated_terms")
  else
    attr
  end
end

#symbols_parse(attr, xml, node) ⇒ Object



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

def symbols_parse(attr, xml, node)
  node.role == "nonterm" and return nonterm_symbols_parse(attr, xml, node)
  xml.definitions **attr_code(attr) do |xml_section|
    xml_section.title { |t| t << node.title }
    stash_symbols
    @definitions = true
    stash_term_def
    xml_section << node.content
    pop_symbols
    pop_term_def
  end
end

#term_contains_subclauses(node) ⇒ Object



87
88
89
90
# File 'lib/metanorma/standoc/terms.rb', line 87

def term_contains_subclauses(node)
  sub = node.find_by(context: :section) { |s| s.level == node.level + 1 }
  !sub.empty?
end

#term_def_parse(attrs, xml, node, _toplevel) ⇒ Object



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

def term_def_parse(attrs, xml, node, _toplevel)
  xml.terms **attr_code(attrs) do |section|
    section.title { |t| t << node.title }
    (s = node.attr("source")) && s.split(",").each do |s1|
      section.termdocsource(nil, **attr_code(bibitemid: s1))
    end
    section << node.content
  end
end

#term_def_subclause_parse(attrs, xml, node) ⇒ Object

subclause contains subclauses



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/standoc/terms.rb', line 70

def term_def_subclause_parse(attrs, xml, node)
  node.role == "nonterm" and
    return nonterm_term_def_subclause_parse(attrs, xml, node)
  node.role == "boilerplate" and
    return terms_boilerplate_parse(attrs, xml, node)
  @definitions and return symbols_parse(attrs, xml, node)
  term_contains_subclauses(node) and
    return term_def_parse(attrs, xml, node, false)
  case sectiontype(node, false)
  when "symbols and abbreviated terms"
    return symbols_parse(attrs, xml, node)
  when "terms and definitions"
    return clause_parse(attrs, xml, node)
  end
  term_def_subclause_parse1(attrs, xml, node)
end

#term_def_subclause_parse1(attrs, xml, node) ⇒ Object



92
93
94
95
96
97
# File 'lib/metanorma/standoc/terms.rb', line 92

def term_def_subclause_parse1(attrs, xml, node)
  xml.term **attr_code(attrs) do |xml_section|
    term_designation(xml_section, node, "preferred", node.title)
    xml_section << node.content
  end
end

#term_designation(xml, _node, tag, text) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/metanorma/standoc/terms.rb', line 109

def term_designation(xml, _node, tag, text)
  xml.send tag do |p|
    p.expression do |e|
      e.name { |name| name << text }
    end
  end
end

#termdefinition(node) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/metanorma/standoc/terms.rb', line 176

def termdefinition(node)
  noko do |xml|
    xml.definition **attr_code(type: node.attr("type")) do |d|
      d << node.content
    end
  end.join("\n")
end

#terms_boilerplate_parse(attrs, xml, node) ⇒ Object



43
44
45
46
47
# File 'lib/metanorma/standoc/terms.rb', line 43

def terms_boilerplate_parse(attrs, xml, node)
  stash_term_def
  clause_parse(attrs.merge(type: "boilerplate"), xml, node)
  pop_term_def
end

#termsource(node) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/metanorma/standoc/terms.rb', line 166

def termsource(node)
  matched = extract_termsource_refs(node.content, node) or return
  noko do |xml|
    xml.termsource **termsource_attrs(node, matched) do |xml_t|
      seen_xref = Nokogiri::XML.fragment(matched[:xref])
      add_term_source(node, xml_t, seen_xref, matched)
    end
  end.join("\n")
end

#termsource_attrs(node, matched) ⇒ Object



160
161
162
163
164
# File 'lib/metanorma/standoc/terms.rb', line 160

def termsource_attrs(node, matched)
  status = node.attr("status")&.downcase ||
    (matched[:text] ? "modified" : "identical")
  { status: status, type: node.attr("type")&.downcase || "authoritative" }
end

#termsource_origin_attrs(_node, seen_xref) ⇒ Object



117
118
119
120
121
122
# File 'lib/metanorma/standoc/terms.rb', line 117

def termsource_origin_attrs(_node, seen_xref)
  { case: seen_xref.children[0]["case"],
    droploc: seen_xref.children[0]["droploc"],
    bibitemid: seen_xref.children[0]["target"],
    format: seen_xref.children[0]["format"], type: "inline" }
end