Module: IsoDoc::XrefGen::Sections

Included in:
IsoDoc::Xref
Defined in:
lib/isodoc/xref/xref_sect_gen.rb

Constant Summary collapse

SUBCLAUSES =
"./clause | ./references | ./term  | ./terms | ./definitions".freeze

Instance Method Summary collapse

Instance Method Details

#annex_anchor_names(xml) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 48

def annex_anchor_names(xml)
  i = Counter.new("@")
  clause_order_annex(xml).each do |a|
    xml.xpath(ns(a[:path])).each do |c|
      annex_names(c, i.increment(c).print)
      a[:multi] or break
    end
  end
end

#annex_name_anchors(clause, num, level) ⇒ Object



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

def annex_name_anchors(clause, num, level)
  label = num
  level == 1 && clause.name == "annex" and
    label = annex_name_lbl(clause, num)
  @anchors[clause["id"]] =
    { label: label,
      elem: @labels["annex"], type: "clause",
      subtype: "annex", value: num.to_s, level: level,
      title: clause_title(clause),
      xref: "#{@labels['annex']} #{num}" }
end

#annex_name_lbl(clause, num) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 198

def annex_name_lbl(clause, num)
  obl = l10n("(#{@labels['inform_annex']})")
  clause["obligation"] == "normative" and
    obl = l10n("(#{@labels['norm_annex']})")
  title = Common::case_with_markup(@labels["annex"], "capital",
                                   @script)
  l10n("<strong>#{title} #{num}</strong><br/>#{obl}")
end

#annex_names(clause, num) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 219

def annex_names(clause, num)
  annex_name_anchors(clause, num, 1)
  if @klass.single_term_clause?(clause)
    annex_names1(clause.at(ns("./references | ./terms | ./definitions")),
                 num.to_s, 1)
  else
    clause.xpath(ns(SUBCLAUSES))
      .each_with_object(Counter.new(0, prefix: "#{num}.")) do |c, i|
      annex_names1(c, i.increment(c).print, 2)
    end
  end
  hierarchical_asset_names(clause, num)
end

#annex_names1(clause, num, level) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 233

def annex_names1(clause, num, level)
  annex_name_anchors(clause, num, level)
  i = Counter.new(0, prefix: "#{num}.")
  clause.xpath(ns(SUBCLAUSES)).each do |c|
    annex_names1(c, i.increment(c).print, level + 1)
  end
end

#asset_anchor_names(doc) ⇒ Object

preempt clause notes with all other types of note (ISO default)



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 100

def asset_anchor_names(doc)
  @parse_settings.empty? or return
  middle_section_asset_names(doc)
  termnote_anchor_names(doc)
  termexample_anchor_names(doc)
  note_anchor_names(doc.xpath(ns("//table | //figure")))
  note_anchor_names(doc.xpath(ns(sections_xpath)))
  admonition_anchor_names(doc.xpath(ns(sections_xpath)))
  example_anchor_names(doc.xpath(ns(sections_xpath)))
  list_anchor_names(doc.xpath(ns(sections_xpath)))
  deflist_anchor_names(doc.xpath(ns(sections_xpath)))
  bookmark_anchor_names(doc)
end

#back_anchor_names(xml) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 40

def back_anchor_names(xml)
  if @parse_settings.empty? || @parse_settings[:clauses]
    annex_anchor_names(xml)
    back_clauses_anchor_names(xml)
  end
  references(xml) if @parse_settings.empty? || @parse_settings[:refs]
end

#back_clauses_anchor_names(xml) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 58

def back_clauses_anchor_names(xml)
  clause_order_back(xml).each do |a|
    xml.xpath(ns(a[:path])).each do |c|
      back_names(c)
      a[:multi] or break
    end
  end
end

#back_names(clause) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 130

def back_names(clause)
  unnumbered_names(clause)
  sequential_asset_names(
    Nokogiri::XML::NodeSet.new(clause.document, [clause]),
    container: true,
  )
end

#clause_order(docxml) ⇒ Object



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

def clause_order(docxml)
  { preface: clause_order_preface(docxml),
    main: clause_order_main(docxml),
    annex: clause_order_annex(docxml),
    back: clause_order_back(docxml) }
end

#clause_order_annex(_docxml) ⇒ Object



28
29
30
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 28

def clause_order_annex(_docxml)
  [{ path: "//annex", multi: true }]
end

#clause_order_back(_docxml) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 32

def clause_order_back(_docxml)
  [
    { path: @klass.bibliography_xpath },
    { path: "//indexsect", multi: true },
    { path: "//colophon/*", multi: true },
  ]
end

#clause_order_main(docxml) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 15

def clause_order_main(docxml)
  [
    { path: "//sections/clause[@type = 'scope']" },
    { path: @klass.norm_ref_xpath },
    { path: "//sections/terms | " \
      "//sections/clause[descendant::terms]" },
    { path: "//sections/definitions | " \
      "//sections/clause[descendant::definitions]" \
      "[not(descendant::terms)]" },
    { path: @klass.middle_clause(docxml), multi: true },
  ]
end

#clause_order_preface(_docxml) ⇒ Object



11
12
13
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 11

def clause_order_preface(_docxml)
  [{ path: "//preface/*", multi: true }]
end

#clause_title(clause, use_elem_name: false) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 114

def clause_title(clause, use_elem_name: false)
  ret = clause.at(ns("./title"))&.text
  if use_elem_name && !ret
    clause.name.capitalize
  else ret
  end
end

#initial_anchor_names(xml) ⇒ Object



73
74
75
76
77
78
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 73

def initial_anchor_names(xml)
  if @parse_settings.empty? || @parse_settings[:clauses]
    preface_anchor_names(xml)
    main_anchor_names(xml)
  end
end

#main_anchor_names(xml) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 89

def main_anchor_names(xml)
  n = Counter.new
  clause_order_main(xml).each do |a|
    xml.xpath(ns(a[:path])).each do |c|
      section_names(c, n, 1)
      a[:multi] or break
    end
  end
end

#middle_section_asset_names(doc) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 164

def middle_section_asset_names(doc)
  middle_sections =
    "//clause[@type = 'scope'] | #{@klass.norm_ref_xpath} | " \
    "//sections/terms | //preface/* | " \
    "//sections/definitions | //clause[parent::sections]"
  sequential_asset_names(doc.xpath(ns(middle_sections)))
end

#preface_anchor_names(xml) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 80

def preface_anchor_names(xml)
  clause_order_preface(xml).each do |a|
    xml.xpath(ns(a[:path])).each do |c|
      preface_names(c)
      a[:multi] or break
    end
  end
end

#preface_name_anchors(clause, level, title) ⇒ Object



157
158
159
160
161
162
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 157

def preface_name_anchors(clause, level, title)
  @anchors[clause["id"]] =
    { label: nil, level: level,
      xref: title, title: nil,
      type: "clause", elem: @labels["clause"] }
end

#preface_names(clause) ⇒ Object

in StanDoc, prefaces have no numbering; they are referenced only by title



126
127
128
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 126

def preface_names(clause)
  unnumbered_names(clause)
end

#preface_names1(clause, title, parent_title, level) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 148

def preface_names1(clause, title, parent_title, level)
  label = title || parent_title
  preface_name_anchors(clause, level, title || parent_title)
  clause.xpath(ns(SUBCLAUSES)).each_with_index do |c, i|
    preface_names1(c, c.at(ns("./title"))&.text, "#{label} #{i + 1}",
                   level + 1)
  end
end

#reference_names(ref) ⇒ Object



241
242
243
244
245
246
247
248
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 241

def reference_names(ref)
  ids = @klass.bibitem_ref_code(ref)
  identifiers = @klass.render_identifier(ids)
  reference = @klass
    .docid_l10n(identifiers[:metanorma] || identifiers[:sdo] ||
                identifiers[:ordinal] || identifiers[:doi])
  @anchors[ref["id"]] = { xref: reference }
end

#references(docxml) ⇒ Object



67
68
69
70
71
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 67

def references(docxml)
  docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |ref|
    reference_names(ref)
  end
end

#section_name_anchors(clause, num, level) ⇒ Object



191
192
193
194
195
196
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 191

def section_name_anchors(clause, num, level)
  @anchors[clause["id"]] =
    { label: num, xref: l10n("#{@labels['clause']} #{num}"),
      title: clause_title(clause), level: level, type: "clause",
      elem: @labels["clause"] }
end

#section_names(clause, num, lvl) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 172

def section_names(clause, num, lvl)
  clause.nil? and return num
  num.increment(clause)
  section_name_anchors(clause, num.print, lvl)
  clause.xpath(ns(SUBCLAUSES))
    .each_with_object(Counter.new(0, prefix: "#{num.print}.")) do |c, i|
    section_names1(c, i.increment(c).print, lvl + 1)
  end
  num
end

#section_names1(clause, num, level) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 183

def section_names1(clause, num, level)
  section_name_anchors(clause, num, level)
  i = Counter.new(0, prefix: "#{num}.")
  clause.xpath(ns(SUBCLAUSES)).each do |c|
    section_names1(c, i.increment(c).print, level + 1)
  end
end

#unnumbered_names(clause) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/isodoc/xref/xref_sect_gen.rb', line 138

def unnumbered_names(clause)
  clause.nil? and return
  preface_name_anchors(clause, 1,
                       clause_title(clause, use_elem_name: true))
  clause.xpath(ns(SUBCLAUSES)).each_with_index do |c, i|
    preface_names1(c, c.at(ns("./title"))&.text,
                   "#{clause_title(clause)}, #{i + 1}", 2)
  end
end