Module: IsoDoc::Function::XrefGen

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

Defined Under Namespace

Classes: Counter, Seen_Anchor

Constant Summary collapse

SECTIONS_XPATH =
"//foreword | //introduction | //acknowledgements | //preface/clause | "\
"//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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/isodoc/function/xref_anchor.rb', line 27

def anchor(id, lbl, warning = true)
  return nil if id.nil? || id.empty?
  unless @anchors[id]
    if warning
      @seen ||= Seen_Anchor.instance
      @seen.seen(id) or warn "No label has been processed for ID #{id}"
      @seen.add(id)
      return "[#{id}]"
    end
  end
  @anchors.dig(id, lbl)
end

#anchor_names(docxml) ⇒ Object

extract names for all anchors, xref and label



118
119
120
121
122
123
124
125
126
# File 'lib/isodoc/function/xref_gen.rb', line 118

def anchor_names(docxml)
  initial_anchor_names(docxml)
  back_anchor_names(docxml)
  # preempt clause notes with all other types of note (ISO default)
  note_anchor_names(docxml.xpath(ns("//table | //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



57
58
59
60
61
62
63
64
65
# File 'lib/isodoc/function/xref_anchor.rb', line 57

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



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

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



48
49
50
51
52
53
54
55
# File 'lib/isodoc/function/xref_anchor.rb', line 48

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

#example_anchor_names(sections) ⇒ Object



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

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

#get_anchorsObject



23
24
25
# File 'lib/isodoc/function/xref_anchor.rb', line 23

def get_anchors
  @anchors
end

#hierarchical_asset_names(clause, num) ⇒ Object



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

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, "permission", @permission_lbl)
  hierarchical_permission_names(clause, num, "requirement",
                                @requirement_lbl)
  hierarchical_permission_names(clause, num, "recommendation",
                                @recommendation_lbl)
end

#hierarchical_figure_names(clause, num) ⇒ Object



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

def hierarchical_figure_names(clause, num)
  c = Counter.new
  j = 0
  clause.xpath(ns(".//figure |  .//sourcecode[not(ancestor::example)]")).each do |t|
    if t.parent.name == "figure" then j += 1
    else
      j = 0
      c.increment(t)
    end
    label = "#{num}#{hiersep}#{c.print}" +
      (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



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

def hierarchical_formula_names(clause, num)
  c = Counter.new
  clause.xpath(ns(".//formula")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] =
      anchor_struct("#{num}#{hiersep}#{c.increment(t).print}", t,
                    t["inequality"] ? @inequality_lbl : @formula_lbl,
                    "formula", t["unnumbered"])
  end
end

#hierarchical_permission_names(clause, num, klass, label) ⇒ Object



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

def hierarchical_permission_names(clause, num, klass, label)
  c = Counter.new
  clause.xpath(ns(".//#{klass}#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    id = "#{num}#{hiersep}#{c.increment(t).print}"
    @anchors[t["id"]] = anchor_struct(id, t, label, klass, t["unnumbered"])
    sequential_permission_names2(t, id)
  end
end

#hierarchical_table_names(clause, num) ⇒ Object



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

def hierarchical_table_names(clause, num)
  c = Counter.new
  clause.xpath(ns(".//table")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] =
      anchor_struct("#{num}#{hiersep}#{c.increment(t).print}",
                    nil, @table_lbl, "table", t["unnumbered"])
  end
end

#hierfigsepObject



9
10
11
# File 'lib/isodoc/function/xref_counter.rb', line 9

def hierfigsep
  "-"
end

#hiersepObject



5
6
7
# File 'lib/isodoc/function/xref_counter.rb', line 5

def hiersep
  "."
end

#list_anchor_names(sections) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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"))
    c = Counter.new
    notes.each do |n|
      next if n["id"].nil? || n["id"].empty?
      idx = notes.size == 1 ? "" : " #{c.increment(n).print}"
      @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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/isodoc/function/xref_gen.rb', line 102

def list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list)
  c = Counter.new
  list.xpath(ns("./li")).each do |li|
    label = c.increment(li).listlabel(depth)
    label = "#{prev_label}.#{label}" unless prev_label.empty?
    label = "#{list_anchor[:xref]} #{label}" if refer_list
    li["id"] and @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

#note_anchor_names(sections) ⇒ Object



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

def note_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_NOTES_XPATH)
    c = Counter.new
    notes.each do |n|
      next if @anchors[n["id"]] || n["id"].nil? || n["id"].empty?
      idx = notes.size == 1 ? "" : " #{c.increment(n).print}"
      @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



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

def sequential_asset_names(clause)
  sequential_table_names(clause)
  sequential_figure_names(clause)
  sequential_formula_names(clause)
  sequential_permission_names(clause, "permission", @permission_lbl)
  sequential_permission_names(clause, "requirement", @requirement_lbl)
  sequential_permission_names(clause, "recommendation", @recommendation_lbl)
end

#sequential_figure_names(clause) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/isodoc/function/xref_gen.rb', line 128

def sequential_figure_names(clause)
  c = Counter.new
  j = 0
  clause.xpath(ns(".//figure | .//sourcecode[not(ancestor::example)]")).each do |t|
    if t.parent.name == "figure" then j += 1
    else
      j = 0
      c.increment(t)
    end
    label = c.print + (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



153
154
155
156
157
158
159
160
161
162
# File 'lib/isodoc/function/xref_gen.rb', line 153

def sequential_formula_names(clause)
  c = Counter.new
  clause.xpath(ns(".//formula")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] =
      anchor_struct(c.increment(t).print, t, 
                    t["inequality"] ? @inequality_lbl : @formula_lbl,
                    "formula", t["unnumbered"])
  end
end

#sequential_permission_names(clause, klass, label) ⇒ Object



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

def sequential_permission_names(clause, klass, label)
  c = Counter.new
  clause.xpath(ns(".//#{klass}#{FIRST_LVL_REQ}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    id = c.increment(t).print
    @anchors[t["id"]] = anchor_struct(id, t, label, klass, t["unnumbered"])
    sequential_permission_names2(t, id)
  end
end

#sequential_permission_names1(block, lbl, klass, label) ⇒ Object



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

def sequential_permission_names1(block, lbl, klass, label)
  c = Counter.new
  block.xpath(ns("./#{klass}")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    id = "#{lbl}#{hierfigsep}#{c.increment(t).print}"
    @anchors[t["id"]] = anchor_struct(id, t, label, klass, t["unnumbered"])
    sequential_permission_names2(t, id)
  end
end

#sequential_permission_names2(t, id) ⇒ Object



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

def sequential_permission_names2(t, id)
  sequential_permission_names1(t, id, "permission", @permission_lbl)
  sequential_permission_names1(t, id, "requirement", @requirement_lbl)
  sequential_permission_names1(t, id, "recommendation", @recommendation_lbl)
end

#sequential_table_names(clause) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/isodoc/function/xref_gen.rb', line 144

def sequential_table_names(clause)
  c = Counter.new
  clause.xpath(ns(".//table")).each do |t|
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(c.increment(t).print, nil, 
                                      @table_lbl, "table", t["unnumbered"])
  end
end

#termexample_anchor_names(docxml) ⇒ Object



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

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

#termnote_anchor_names(docxml) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/isodoc/function/xref_gen.rb', line 10

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

#termnote_label(n) ⇒ Object



6
7
8
# File 'lib/isodoc/function/xref_gen.rb', line 6

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