Module: IsoDoc::Function::XrefGen
- Included in:
- Common
- Defined in:
- lib/isodoc/function/xref_gen.rb,
lib/isodoc/function/xref_counter.rb
Defined Under Namespace
Classes: Counter
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
- #anchor(id, lbl, warning = true) ⇒ Object
-
#anchor_names(docxml) ⇒ Object
extract names for all anchors, xref and label.
- #anchor_struct(lbl, container, elem, type, unnumbered = false) ⇒ Object
- #anchor_struct_label(lbl, elem) ⇒ Object
- #anchor_struct_xref(lbl, elem) ⇒ Object
- #example_anchor_names(sections) ⇒ Object
- #get_anchors ⇒ Object
- #hierarchical_asset_names(clause, num) ⇒ Object
- #hierarchical_figure_names(clause, num) ⇒ Object
- #hierarchical_formula_names(clause, num) ⇒ Object
- #hierarchical_permission_names(clause, num, klass, label) ⇒ Object
- #hierarchical_table_names(clause, num) ⇒ Object
- #hierfigsep ⇒ Object
- #hiersep ⇒ Object
- #list_anchor_names(sections) ⇒ Object
- #list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list) ⇒ Object
- #note_anchor_names(sections) ⇒ Object
- #sequential_asset_names(clause) ⇒ Object
- #sequential_figure_names(clause) ⇒ Object
- #sequential_formula_names(clause) ⇒ Object
- #sequential_permission_names(clause, klass, label) ⇒ Object
- #sequential_permission_names1(block, lbl, klass, label) ⇒ Object
- #sequential_table_names(clause) ⇒ Object
- #termexample_anchor_names(docxml) ⇒ Object
- #termnote_anchor_names(docxml) ⇒ Object
- #termnote_label(n) ⇒ Object
Instance Method Details
#anchor(id, lbl, warning = true) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/isodoc/function/xref_gen.rb', line 13 def anchor(id, lbl, warning = true) unless @anchors[id] warning and warn "No label has been processed for ID #{id}" return "[#{id}]" end @anchors.dig(id, lbl) end |
#anchor_names(docxml) ⇒ Object
extract names for all anchors, xref and label
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/isodoc/function/xref_gen.rb', line 133 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
177 178 179 180 181 182 183 184 185 |
# File 'lib/isodoc/function/xref_gen.rb', line 177 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 175 |
# File 'lib/isodoc/function/xref_gen.rb', line 168 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/isodoc/function/xref_gen.rb', line 87 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_anchors ⇒ Object
9 10 11 |
# File 'lib/isodoc/function/xref_gen.rb', line 9 def get_anchors @anchors end |
#hierarchical_asset_names(clause, num) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/isodoc/function/xref_gen.rb', line 277 def hierarchical_asset_names(clause, num) hierarchical_table_names(clause, num) hierarchical_figure_names(clause, num) hierarchical_formula_names(clause, num) (clause, num, "permission", @permission_lbl) (clause, num, "requirement", @requirement_lbl) (clause, num, "recommendation", @recommendation_lbl) end |
#hierarchical_figure_names(clause, num) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/isodoc/function/xref_gen.rb', line 250 def hierarchical_figure_names(clause, num) c = Counter.new j = 0 clause.xpath(ns(".//figure")).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
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/isodoc/function/xref_gen.rb', line 286 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
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/isodoc/function/xref_gen.rb', line 297 def (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? lbl = "#{num}#{hiersep}#{c.increment(t).print}" @anchors[t["id"]] = anchor_struct(lbl, t, label, klass, t["unnumbered"]) (t, lbl, "permission", @permission_lbl) (t, lbl, "requirement", @requirement_lbl) (t, lbl, "recommendation", @recommendation_lbl) end end |
#hierarchical_table_names(clause, num) ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/isodoc/function/xref_gen.rb', line 267 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 |
#hierfigsep ⇒ Object
246 247 248 |
# File 'lib/isodoc/function/xref_gen.rb', line 246 def hierfigsep "-" end |
#hiersep ⇒ Object
242 243 244 |
# File 'lib/isodoc/function/xref_gen.rb', line 242 def hiersep "." end |
#list_anchor_names(sections) ⇒ 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_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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/isodoc/function/xref_gen.rb', line 117 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
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/isodoc/function/xref_gen.rb', line 66 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
233 234 235 236 237 238 239 240 |
# File 'lib/isodoc/function/xref_gen.rb', line 233 def sequential_asset_names(clause) sequential_table_names(clause) sequential_figure_names(clause) sequential_formula_names(clause) (clause, "permission", @permission_lbl) (clause, "requirement", @requirement_lbl) (clause, "recommendation", @recommendation_lbl) end |
#sequential_figure_names(clause) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/isodoc/function/xref_gen.rb', line 144 def sequential_figure_names(clause) c = Counter.new j = 0 clause.xpath(ns(".//figure")).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
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/isodoc/function/xref_gen.rb', line 196 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
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/isodoc/function/xref_gen.rb', line 209 def (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"]) (t, id, "permission", @permission_lbl) (t, id, "requirement", @requirement_lbl) (t, id, "recommendation", @recommendation_lbl) end end |
#sequential_permission_names1(block, lbl, klass, label) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/isodoc/function/xref_gen.rb', line 221 def (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"]) (t, id, "permission", @permission_lbl) (t, id, "requirement", @requirement_lbl) (t, id, "recommendation", @recommendation_lbl) end end |
#sequential_table_names(clause) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/isodoc/function/xref_gen.rb', line 187 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/isodoc/function/xref_gen.rb', line 40 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/isodoc/function/xref_gen.rb', line 25 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
21 22 23 |
# File 'lib/isodoc/function/xref_gen.rb', line 21 def termnote_label(n) @termnote_lbl.gsub(/%/, n.to_s) end |