Module: IsoDoc::Function::XrefGen

Included in:
Common
Defined in:
lib/isodoc/function/xref_gen.rb

Constant Summary collapse

SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | //annex | "\
"//sections/clause | //sections/definitions | "\
"//bibliography/references | //bibliography/clause".freeze
CHILD_NOTES_XPATH =
"./*[not(self::xmlns:clause) and not(self::xmlns:appendix) and "\
"not(self::xmlns:terms) and not(self::xmlns:definitions)]//xmlns:note | "\
"./xmlns:note".freeze
CHILD_EXAMPLES_XPATH =
"./*[not(self::xmlns:clause) and not(self::xmlns:appendix) and "\
"not(self::xmlns:terms) and not(self::xmlns:definitions)]//"\
"xmlns:example | ./xmlns:example".freeze
CHILD_SECTIONS =
"./clause | ./appendix | ./terms | ./definitions"
FIRST_LVL_REQ =
"[not(ancestor::permission or ancestor::requirement or ancestor::recommendation)]".freeze

Instance Method Summary collapse

Instance Method Details

#anchor(id, lbl, warning = true) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/isodoc/function/xref_gen.rb', line 12

def anchor(id, lbl, warning = true)
  unless @anchors[id]
    warning and warn "No label has been processed for ID #{id}"
    return nil
  end
  @anchors.dig(id, lbl)
end

#anchor_names(docxml) ⇒ Object

extract names for all anchors, xref and label



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

def anchor_names(docxml)
  initial_anchor_names(docxml)
  back_anchor_names(docxml)
  # preempt clause notes with all other types of note
  note_anchor_names(docxml.xpath(ns("//table | //example | //formula | "\
                                    "//figure")))
  note_anchor_names(docxml.xpath(ns(SECTIONS_XPATH)))
  example_anchor_names(docxml.xpath(ns(SECTIONS_XPATH)))
  list_anchor_names(docxml.xpath(ns(SECTIONS_XPATH)))
end

#anchor_struct(lbl, container, elem, type, unnumbered = false) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/isodoc/function/xref_gen.rb', line 176

def anchor_struct(lbl, container, elem, type, unnumbered = false)
  ret = {}
  ret[:label] = unnumbered == "true" ? nil : anchor_struct_label(lbl, elem)
  ret[:xref] = anchor_struct_xref(unnumbered == "true" ? "(??)" : lbl, elem)
  ret[:xref].gsub!(/ $/, "")
  ret[:container] = get_clause_id(container) unless container.nil?
  ret[:type] = type
  ret
end

#anchor_struct_label(lbl, elem) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/isodoc/function/xref_gen.rb', line 160

def anchor_struct_label(lbl, elem)
  case elem
  when @appendix_lbl then l10n("#{elem} #{lbl}")
  else
    lbl.to_s
  end
end

#anchor_struct_xref(lbl, elem) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/isodoc/function/xref_gen.rb', line 168

def anchor_struct_xref(lbl, elem)
  case elem
  when @formula_lbl then l10n("#{elem} (#{lbl})")
  else
    l10n("#{elem} #{lbl}")
  end
end

#example_anchor_names(sections) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/isodoc/function/xref_gen.rb', line 81

def example_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_EXAMPLES_XPATH)
    i = 0
    notes.each do |n|
      next if @anchors[n["id"]]
      next if n["id"].nil? || n["id"].empty?
      idx = notes.size == 1 ? "" : " #{i + 1}"
      @anchors[n["id"]] = anchor_struct(idx, n, @example_xref_lbl,
                                        "example", n["unnumbered"])
      i += 1 unless n["unnumbered"]
    end
    example_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#get_anchorsObject



8
9
10
# File 'lib/isodoc/function/xref_gen.rb', line 8

def get_anchors
  @anchors
end

#hierarchical_asset_names(clause, num) ⇒ Object



322
323
324
325
326
327
328
329
# File 'lib/isodoc/function/xref_gen.rb', line 322

def hierarchical_asset_names(clause, num)
  hierarchical_table_names(clause, num)
  hierarchical_figure_names(clause, num)
  hierarchical_formula_names(clause, num)
  hierarchical_permission_names(clause, num)
  hierarchical_requirement_names(clause, num)
  hierarchical_recommendation_names(clause, num)
end

#hierarchical_figure_names(clause, num) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/isodoc/function/xref_gen.rb', line 298

def hierarchical_figure_names(clause, num)
  i = j = 0
  clause.xpath(ns(".//figure")).each do |t|
    if t.parent.name == "figure" then j += 1
    else
      j = 0
      i += 1 unless t["unnumbered"]
    end
    label = "#{num}#{hiersep}#{i}" + (j.zero? ? "" : "#{hierfigsep}#{j}")
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(label, nil, @figure_lbl, "figure", t["unnumbered"])
  end
end

#hierarchical_formula_names(clause, num) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'lib/isodoc/function/xref_gen.rb', line 331

def hierarchical_formula_names(clause, num)
  i = 0
  clause.xpath(ns(".//formula")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct("#{num}#{hiersep}#{i + 1}",
                                      t, @formula_lbl, "formula", t["unnumbered"])
    i += 1 unless t["unnumbered"]
  end
end

#hierarchical_permission_names(clause, num) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/isodoc/function/xref_gen.rb', line 341

def hierarchical_permission_names(clause, num)
  i = 0
  clause.xpath(ns(".//permission#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    lbl = "#{num}#{hiersep}#{i + 1}"
    @anchors[t["id"]] = anchor_struct(lbl, t, @permission_lbl, "permission", t["unnumbered"])
    sequential_permission_names1(t, lbl)
    sequential_requirement_names1(t, lbl)
    sequential_recommendation_names1(t, lbl)
    i += 1 unless t["unnumbered"]
  end
end

#hierarchical_recommendation_names(clause, num) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/isodoc/function/xref_gen.rb', line 367

def hierarchical_recommendation_names(clause, num)
  i = 0
  clause.xpath(ns(".//recommendation#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    lbl = "#{num}#{hiersep}#{i + 1}"
    @anchors[t["id"]] = anchor_struct(lbl, t, @recommendation_lbl, "recommendation", t["unnumbered"])
    sequential_permission_names1(t, lbl)
    sequential_requirement_names1(t, lbl)
    sequential_recommendation_names1(t, lbl)
    i += 1 unless t["unnumbered"]
  end
end

#hierarchical_requirement_names(clause, num) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/isodoc/function/xref_gen.rb', line 354

def hierarchical_requirement_names(clause, num)
  i = 0
  clause.xpath(ns(".//requirement#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    lbl = "#{num}#{hiersep}#{i + 1}"
    @anchors[t["id"]] = anchor_struct(lbl, t, @requirement_lbl, "requirement", t["unnumbered"])
    sequential_permission_names1(t, lbl)
    sequential_requirement_names1(t, lbl)
    sequential_recommendation_names1(t, lbl)
    i += 1 unless t["unnumbered"]
  end
end

#hierarchical_table_names(clause, num) ⇒ Object



312
313
314
315
316
317
318
319
320
# File 'lib/isodoc/function/xref_gen.rb', line 312

def hierarchical_table_names(clause, num)
  i = 0
  clause.xpath(ns(".//table")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct("#{num}#{hiersep}#{i + 1}",
                                      nil, @table_lbl, "table", t["unnumbered"])
    i += 1 unless t["unnumbered"]
  end
end

#hierfigsepObject



294
295
296
# File 'lib/isodoc/function/xref_gen.rb', line 294

def hierfigsep
  "-"
end

#hiersepObject



290
291
292
# File 'lib/isodoc/function/xref_gen.rb', line 290

def hiersep
  "."
end

#list_anchor_names(sections) ⇒ Object



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

def list_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(ns(".//ol")) - s.xpath(ns(".//clause//ol")) -
      s.xpath(ns(".//appendix//ol")) - s.xpath(ns(".//ol//ol"))
    notes.each_with_index do |n, i|
      next if n["id"].nil? || n["id"].empty?
      idx = notes.size == 1 ? "" : " #{i + 1}"
      @anchors[n["id"]] = anchor_struct(idx, n, @list_lbl, "list", false)
      list_item_anchor_names(n, @anchors[n["id"]], 1, "", notes.size != 1)
    end
    list_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/isodoc/function/xref_gen.rb', line 120

def list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list)
  list.xpath(ns("./li")).each_with_index do |li, i|
    label = listlabel(depth, i + 1)
    label = "#{prev_label}.#{label}" unless prev_label.empty?
    label = "#{list_anchor[:xref]} #{label}" if refer_list
    li["id"] && @anchors[li["id"]] = { xref: "#{label})",
                                       type: "listitem",
                                       container: list_anchor[:container] }
    li.xpath(ns("./ol")).each do |ol|
      list_item_anchor_names(ol, list_anchor, depth + 1, label, false)
    end
  end
end

#listlabel(depth, i) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/isodoc/function/xref_gen.rb', line 111

def listlabel(depth, i)
  return i.to_s if [2, 7].include? depth
  return (96 + i).chr.to_s if [1, 6].include? depth
  return (64 + i).chr.to_s if [4, 9].include? depth
  return RomanNumerals.to_roman(i).downcase if [3, 8].include? depth
  return RomanNumerals.to_roman(i).upcase if [5, 10].include? depth
  return i.to_s
end

#note_anchor_names(sections) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/isodoc/function/xref_gen.rb', line 61

def note_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_NOTES_XPATH)
    notes.each_with_index do |n, i|
      next if @anchors[n["id"]]
      next if n["id"].nil? || n["id"].empty?
      idx = notes.size == 1 ? "" : " #{i + 1}"
      @anchors[n["id"]] = anchor_struct(idx, n, @note_xref_lbl, "note", false)
    end
    note_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#sequential_asset_names(clause) ⇒ Object



281
282
283
284
285
286
287
288
# File 'lib/isodoc/function/xref_gen.rb', line 281

def sequential_asset_names(clause)
  sequential_table_names(clause)
  sequential_figure_names(clause)
  sequential_formula_names(clause)
  sequential_permission_names(clause)
  sequential_requirement_names(clause)
  sequential_recommendation_names(clause)
end

#sequential_figure_names(clause) ⇒ Object



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

def sequential_figure_names(clause)
  i = j = 0
  clause.xpath(ns(".//figure")).each do |t|
    if t.parent.name == "figure" then j += 1
    else
      j = 0
      i += 1 unless t["unnumbered"]
    end
    label = i.to_s + (j.zero? ? "" : "-#{j}")
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(label, nil, @figure_lbl, "figure", t["unnumbered"])
  end
end

#sequential_formula_names(clause) ⇒ Object



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

def sequential_formula_names(clause)
  i = 0
  clause.xpath(ns(".//formula")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @formula_lbl, "formula", t["unnumbered"])
    i += 1 unless t["unnumbered"]
  end
end

#sequential_permission_names(clause) ⇒ Object



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

def sequential_permission_names(clause)
  i = 0
  clause.xpath(ns(".//permission#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @permission_lbl, "permission", t["unnumbered"])
    sequential_permission_names1(t, i + 1)
    sequential_requirement_names1(t, i + 1)
    sequential_recommendation_names1(t, i + 1)
    i += 1 unless t["unnumbered"]
  end
end

#sequential_permission_names1(block, lbl) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/isodoc/function/xref_gen.rb', line 218

def sequential_permission_names1(block, lbl)
  i = 0
  block.xpath(ns("./permission")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    newlbl = "#{lbl}#{hierfigsep}#{i + 1}"
    @anchors[t["id"]] = anchor_struct(newlbl, t, @permission_lbl, "permission", t["unnumbered"])
    sequential_permission_names1(t, newlbl)
    sequential_requirement_names1(t, newlbl)
    sequential_recommendation_names1(t, newlbl)
    i += 1 unless t["unnumbered"]
  end
end

#sequential_recommendation_names(clause) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/isodoc/function/xref_gen.rb', line 256

def sequential_recommendation_names(clause)
  i = 0
  clause.xpath(ns(".//recommendation#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @recommendation_lbl, "recommendation", t["unnumbered"])
    sequential_permission_names1(t, i + 1)
    sequential_requirement_names1(t, i + 1)
    sequential_recommendation_names1(t, i + 1)
    i += 1 unless t["unnumbered"]
  end
end

#sequential_recommendation_names1(block, lbl) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/isodoc/function/xref_gen.rb', line 268

def sequential_recommendation_names1(block, lbl)
  i = 0
  block.xpath(ns("./recommendation")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    newlbl = "#{lbl}#{hierfigsep}#{i + 1}"
    @anchors[t["id"]] = anchor_struct(newlbl, t, @recommendation_lbl, "recommendation", t["unnumbered"])
    sequential_permission_names1(t, newlbl)
    sequential_requirement_names1(t, newlbl)
    sequential_recommendation_names1(t, newlbl)
    i += 1 unless t["unnumbered"]
  end
end

#sequential_requirement_names(clause) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/isodoc/function/xref_gen.rb', line 231

def sequential_requirement_names(clause)
  i = 0
  clause.xpath(ns(".//requirement#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @requirement_lbl, "requirement", t["unnumbered"])
    sequential_permission_names1(t, i + 1)
    sequential_requirement_names1(t, i + 1)
    sequential_recommendation_names1(t, i + 1)
    i += 1 unless t["unnumbered"]
  end
end

#sequential_requirement_names1(block, lbl) ⇒ Object



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

def sequential_requirement_names1(block, lbl)
  i = 0
  block.xpath(ns("./requirement")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    newlbl = "#{lbl}#{hierfigsep}#{i + 1}"
    @anchors[t["id"]] = anchor_struct(newlbl, t, @requirement_lbl, "requirement", t["unnumbered"])
    sequential_permission_names1(t, newlbl)
    sequential_requirement_names1(t, newlbl)
    sequential_recommendation_names1(t, newlbl)
    i += 1 unless t["unnumbered"]
  end
end

#sequential_table_names(clause) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/isodoc/function/xref_gen.rb', line 186

def sequential_table_names(clause)
  i = 0
  clause.xpath(ns(".//table")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, nil, @table_lbl, "table", t["unnumbered"])
    i += 1 unless t["unnumbered"]
  end
end

#termexample_anchor_names(docxml) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/isodoc/function/xref_gen.rb', line 37

def termexample_anchor_names(docxml)
  docxml.xpath(ns("//term[descendant::termexample]")).each do |t|
    examples = t.xpath(ns(".//termexample"))
    examples.each_with_index do |n, i|
      return if n["id"].nil? || n["id"].empty?
      idx = examples.size == 1 ? "" : (i + 1).to_s
      @anchors[n["id"]] = {
        type: "termexample",
        label: idx, xref: l10n("#{anchor(t['id'], :xref)}, "\
                               "#{@note_xref_lbl} #{i + 1}") }
    end
  end
end

#termnote_anchor_names(docxml) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/isodoc/function/xref_gen.rb', line 24

def termnote_anchor_names(docxml)
  docxml.xpath(ns("//term[descendant::termnote]")).each do |t|
    t.xpath(ns(".//termnote")).each_with_index do |n, i|
      return if n["id"].nil? || n["id"].empty?
      @anchors[n["id"]] =
        { label: termnote_label(i + 1),
          type: "termnote",
          xref: l10n("#{anchor(t['id'], :xref)}, "\
                     "#{@note_xref_lbl} #{i + 1}") }
    end
  end
end

#termnote_label(n) ⇒ Object



20
21
22
# File 'lib/isodoc/function/xref_gen.rb', line 20

def termnote_label(n)
  @termnote_lbl.gsub(/%/, n.to_s)
end