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"

Instance Method Summary collapse

Instance Method Details

#anchor_names(docxml) ⇒ Object

extract names for all anchors, xref and label



125
126
127
128
129
130
131
132
133
134
# File 'lib/isodoc/function/xref_gen.rb', line 125

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) ⇒ Object



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

def anchor_struct(lbl, container, elem, type)
  ret = {}
  ret[:label] = anchor_struct_label(lbl, elem)
  ret[:xref] = anchor_struct_xref(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



150
151
152
153
154
155
156
# File 'lib/isodoc/function/xref_gen.rb', line 150

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



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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/isodoc/function/xref_gen.rb', line 73

def example_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_EXAMPLES_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, @example_xref_lbl,
                                        "example")
    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



251
252
253
254
255
256
257
258
# File 'lib/isodoc/function/xref_gen.rb', line 251

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



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

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
    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")
  end
end

#hierarchical_formula_names(clause, num) ⇒ Object



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

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

#hierarchical_permission_names(clause, num) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/isodoc/function/xref_gen.rb', line 268

def hierarchical_permission_names(clause, num)
  clause.xpath(ns(".//permission")).each_with_index do |t, i|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct("#{num}.#{i + 1}",
                                      t, @permission_lbl, "permission")
  end
end

#hierarchical_recommendation_names(clause, num) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/isodoc/function/xref_gen.rb', line 284

def hierarchical_recommendation_names(clause, num)
  clause.xpath(ns(".//recommendation")).each_with_index do |t, i|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct("#{num}.#{i + 1}",
                                      t, @recommendation_lbl, "recommendation")
  end
end

#hierarchical_requirement_names(clause, num) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/isodoc/function/xref_gen.rb', line 276

def hierarchical_requirement_names(clause, num)
  clause.xpath(ns(".//requirement")).each_with_index do |t, i|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct("#{num}.#{i + 1}",
                                      t, @requirement_lbl, "requirement")
  end
end

#hierarchical_table_names(clause, num) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/isodoc/function/xref_gen.rb', line 243

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

#hierfigsepObject



225
226
227
# File 'lib/isodoc/function/xref_gen.rb', line 225

def hierfigsep
  "-"
end

#hiersepObject



221
222
223
# File 'lib/isodoc/function/xref_gen.rb', line 221

def hiersep
  "."
end

#list_anchor_names(sections) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/isodoc/function/xref_gen.rb', line 87

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



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

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



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

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/isodoc/function/xref_gen.rb', line 53

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")
    end
    note_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#sequential_asset_names(clause) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/isodoc/function/xref_gen.rb', line 212

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



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

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
    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")
  end
end

#sequential_formula_names(clause) ⇒ Object



183
184
185
186
187
188
# File 'lib/isodoc/function/xref_gen.rb', line 183

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

#sequential_permission_names(clause) ⇒ Object



190
191
192
193
194
195
# File 'lib/isodoc/function/xref_gen.rb', line 190

def sequential_permission_names(clause)
  clause.xpath(ns(".//permission")).each_with_index do |t, i|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @permission_lbl, "permission")
  end
end

#sequential_recommendation_names(clause) ⇒ Object



204
205
206
207
208
209
# File 'lib/isodoc/function/xref_gen.rb', line 204

def sequential_recommendation_names(clause)
  clause.xpath(ns(".//recommendation")).each_with_index do |t, i|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @recommendation_lbl, "recommendation")
  end
end

#sequential_requirement_names(clause) ⇒ Object



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

def sequential_requirement_names(clause)
  clause.xpath(ns(".//requirement")).each_with_index do |t, i|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(i + 1, t, @requirement_lbl, "requirement")
  end
end

#sequential_table_names(clause) ⇒ Object



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

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

#termexample_anchor_names(docxml) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/isodoc/function/xref_gen.rb', line 29

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("#{@anchors.dig(t['id'], :xref)}, "\
                               "#{@note_xref_lbl} #{i + 1}") }
    end
  end
end

#termnote_anchor_names(docxml) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/isodoc/function/xref_gen.rb', line 16

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("#{@anchors.dig(t['id'], :xref)}, "\
                     "#{@note_xref_lbl} #{i + 1}") }
    end
  end
end

#termnote_label(n) ⇒ Object



12
13
14
# File 'lib/isodoc/function/xref_gen.rb', line 12

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