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



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

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



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

def anchor_struct(lbl, container, elem)
  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
end

#anchor_struct_label(lbl, elem) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/isodoc/function/xref_gen.rb', line 141

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



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

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/isodoc/function/xref_gen.rb', line 66

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



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

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

#hierarchical_figure_names(clause, num) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/isodoc/function/xref_gen.rb', line 178

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

#list_anchor_names(sections) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/isodoc/function/xref_gen.rb', line 79

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_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



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

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



93
94
95
96
97
98
99
100
# File 'lib/isodoc/function/xref_gen.rb', line 93

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



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

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

#sequential_asset_names(clause) ⇒ Object



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

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

#sequential_figure_names(clause) ⇒ Object



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

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

#termexample_anchor_names(docxml) ⇒ Object



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

def termexample_anchor_names(docxml)
  docxml.xpath(ns("//term[termexample]")).each do |t|
    t.xpath(ns("./termexample")).each_with_index do |n, i|
      return if n["id"].nil? || n["id"].empty?
      @anchors[n["id"]] =
        { label: (i + 1).to_s,
          xref: l10n("#{@anchors[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
# File 'lib/isodoc/function/xref_gen.rb', line 16

def termnote_anchor_names(docxml)
  docxml.xpath(ns("//term[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),
          xref: l10n("#{@anchors[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