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 | //bibliography/references | "\
"//bibliography/clause".freeze
CHILD_NOTES_XPATH =
"./*[not(self::xmlns:clause) and "\
"not(self::xmlns:appendix)]//xmlns:note | ./xmlns:note".freeze
CHILD_EXAMPLES_XPATH =
"./*[not(self::xmlns:clause) and not(self::xmlns:appendix)]//"\
"xmlns:example | ./xmlns:example".freeze

Instance Method Summary collapse

Instance Method Details

#anchor_names(docxml) ⇒ Object

extract names for all anchors, xref and label



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

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



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

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



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

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



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

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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/isodoc/function/xref_gen.rb', line 69

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("./clause | ./appendix")))
  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



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/isodoc/function/xref_gen.rb', line 197

def hierarchical_asset_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}.#{i + 1}", nil, @table_lbl, "table")
  end
  hierarchical_figure_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}.#{i + 1}", t, @formula_lbl, "formula")
  end
end

#hierarchical_figure_names(clause, num) ⇒ Object



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

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}.#{i}" + (j.zero? ? "" : "-#{j}")
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(label, nil, @figure_lbl, "figure")
  end
end

#list_anchor_names(sections) ⇒ Object



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

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("./clause | ./appendix")))
  end
end

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



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

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



96
97
98
99
100
101
102
103
# File 'lib/isodoc/function/xref_gen.rb', line 96

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



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

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("./clause | ./appendix")))
  end
end

#sequential_asset_names(clause) ⇒ Object



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

def sequential_asset_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
  sequential_figure_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_figure_names(clause) ⇒ Object



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

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

#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