Class: IsoDoc::ITU::Xref

Inherits:
Xref
  • Object
show all
Defined in:
lib/isodoc/itu/xref.rb

Constant Summary collapse

MIDDLE_SECTIONS =
"//clause[title = 'Scope'] | "\
"//foreword | //introduction | //acknowledgements | "\
"//references[@normative = 'true'] | "\
"//sections/terms | //preface/clause | "\
"//sections/definitions | //clause[parent::sections]".freeze

Instance Method Summary collapse

Constructor Details

#initialize(lang, script, klass, labels, options) ⇒ Xref

Returns a new instance of Xref.



8
9
10
11
# File 'lib/isodoc/itu/xref.rb', line 8

def initialize(lang, script, klass, labels, options)
  super
  @hierarchical_assets = options[:hierarchical_assets]
end

Instance Method Details

#annex_name_lbl(clause, num) ⇒ Object



13
14
15
16
# File 'lib/isodoc/itu/xref.rb', line 13

def annex_name_lbl(clause, num)
  lbl = clause["obligation"] == "informative" ? @labels["appendix"] : @labels["annex"]
  l10n("<b>#{lbl} #{num}</b>")
end

#annex_names(clause, num) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/isodoc/itu/xref.rb', line 18

def annex_names(clause, num)
  lbl = clause["obligation"] == "informative" ? @labels["appendix"] : @labels["annex"]
  @anchors[clause["id"]] =
    { label: annex_name_lbl(clause, num), type: "clause",
      xref: "#{lbl} #{num}", level: 1 }
  if a = single_annex_special_section(clause)
    annex_names1(a, "#{num}", 1)
  else
    clause.xpath(ns("./clause | ./references | ./terms | ./definitions")).
      each_with_index do |c, i|
      annex_names1(c, "#{num}.#{i + 1}", 2)
    end
  end
  hierarchical_asset_names(clause, num)
end

#annex_names1(clause, num, level) ⇒ Object



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

def annex_names1(clause, num, level)
  @anchors[clause["id"]] =
    { label: num, xref: "#{@labels["annex_subclause"]} #{num}",
      level: level, type: "clause" }
  clause.xpath(ns("./clause | ./references | ./terms | ./definitions")).each_with_index do |c, i|
    annex_names1(c, "#{num}.#{i + 1}", level + 1)
  end
end

#back_anchor_names(docxml) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/isodoc/itu/xref.rb', line 34

def back_anchor_names(docxml)
  super
  if annexid = docxml&.at(ns("//bibdata/ext/structuredidentifier/annexid"))&.text
    docxml.xpath(ns("//annex")).each { |c| annex_names(c, annexid) }
  else
    docxml.xpath(ns("//annex[@obligation = 'informative']")).each_with_index do |c, i|
      annex_names(c, RomanNumerals.to_roman(i + 1))
    end
    docxml.xpath(ns("//annex[not(@obligation = 'informative')]")).each_with_index do |c, i|
      annex_names(c, (65 + i + (i > 7 ? 1 : 0)).chr.to_s)
    end
  end
end

#hierarchical_figure_names(clause, num) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/isodoc/itu/xref.rb', line 104

def hierarchical_figure_names(clause, num)
  c = IsoDoc::XrefGen::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}#{(96 + j).chr.to_s}")
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] = anchor_struct(label, nil, @labels["figure"], "figure",
                                      t["unnumbered"])
  end
end

#hierarchical_formula_names(clause, num) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/isodoc/itu/xref.rb', line 132

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

#initial_anchor_names(d) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/isodoc/itu/xref.rb', line 57

def initial_anchor_names(d)
  d.xpath(ns("//boilerplate//clause")).each { |c| preface_names(c) }
  d.xpath("//xmlns:preface/child::*").each { |c| preface_names(c) }
  @hierarchical_assets ?
    hierarchical_asset_names(d.xpath("//xmlns:preface/child::*"), "Preface") :
    sequential_asset_names(d.xpath("//xmlns:preface/child::*"))
  n = section_names(d.at(ns("//clause[title = 'Scope']")), 0, 1)
  n = section_names(d.at(ns("//bibliography/clause[.//references[@normative = 'true']] | "\
                            "//bibliography/references[@normative = 'true']")), n, 1)
  n = section_names(d.at(ns("//sections/terms | "\
                            "//sections/clause[descendant::terms]")), n, 1)
  n = section_names(d.at(ns("//sections/definitions")), n, 1)
  clause_names(d, n)
  middle_section_asset_names(d)
  termnote_anchor_names(d)
  termexample_anchor_names(d)
end

#middle_section_asset_names(d) ⇒ Object



81
82
83
84
85
86
# File 'lib/isodoc/itu/xref.rb', line 81

def middle_section_asset_names(d)
  return super unless @hierarchical_assets
  d.xpath(ns(MIDDLE_SECTIONS)).each do |c|
    hierarchical_asset_names(c, @anchors[c["id"]][:label])
  end
end

#reference_names(ref) ⇒ Object



143
144
145
146
# File 'lib/isodoc/itu/xref.rb', line 143

def reference_names(ref)
  super
  @anchors[ref["id"]] = { xref: @anchors[ref["id"]][:xref].sub(/^\[/, '').sub(/\]$/, '') }
end

#sequential_figure_names(clause) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/isodoc/itu/xref.rb', line 88

def sequential_figure_names(clause)
  c = IsoDoc::XrefGen::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? ? "" : "-#{(96 + j).chr.to_s}")
    next if t["id"].nil? || t["id"].empty?
    @anchors[t["id"]] =
      anchor_struct(label, nil, @labels["figure"], "figure", t["unnumbered"])
  end
end

#sequential_formula_names(clause) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/isodoc/itu/xref.rb', line 121

def sequential_formula_names(clause)
  clause&.first&.xpath(ns(MIDDLE_SECTIONS))&.each do |c|
    if c["id"] && @anchors[c["id"]]
      hierarchical_formula_names(c, @anchors[c["id"]][:label] ||
                                 @anchors[c["id"]][:xref] || "???")
    else
      hierarchical_formula_names(c, "???")
    end
  end
end

#termnote_anchor_names(docxml) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/isodoc/itu/xref.rb', line 148

def termnote_anchor_names(docxml)
  docxml.xpath(ns("//term[descendant::termnote]")).each do |t|
    c = IsoDoc::XrefGen::Counter.new
    notes = t.xpath(ns(".//termnote"))
    notes.each do |n|
      return if n["id"].nil? || n["id"].empty?
      idx = notes.size == 1 ? "" : " #{c.increment(n).print}"
      @anchors[n["id"]] = anchor_struct(idx, n, @labels["note_xref"],
                                        "termnote", false)
    end
  end
end