Module: IsoDoc::XrefGen::Blocks

Included in:
IsoDoc::Xref
Defined in:
lib/isodoc/xref/xref_gen.rb,
lib/isodoc/xref/xref_gen_seq.rb

Constant Summary collapse

NUMBERED_BLOCKS =
%w(termnote termexample note example requirement
recommendation permission figure table formula
admonition sourcecode).freeze
SECTIONS_XPATH =
"//foreword | //introduction | //acknowledgements | "\
"//preface/terms | preface/definitions | preface/references | "\
"//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 | "\
"./references".freeze
FIRST_LVL_REQ =
"[not(ancestor::permission or ancestor::requirement or "\
"ancestor::recommendation)]".freeze

Instance Method Summary collapse

Instance Method Details

#amend_autonums(amend) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/isodoc/xref/xref_gen.rb', line 26

def amend_autonums(amend)
  autonum = {}
  amend.xpath(ns("./autonumber")).each do |n|
    autonum[n["type"]] = n.text
  end
  autonum
end

#amend_preprocess(xmldoc) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/isodoc/xref/xref_gen.rb', line 14

def amend_preprocess(xmldoc)
  xmldoc.xpath(ns("//amend[newcontent]")).each do |a|
    autonum = amend_autonums(a)
    NUMBERED_BLOCKS.each do |b|
      a.xpath(ns("./newcontent//#{b}")).each_with_index do |e, i|
        autonum[b] && i.zero? and e["number"] = autonum[b]
        !autonum[b] and e["unnumbered"] = "true"
      end
    end
  end
end

#blank?(text) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/isodoc/xref/xref_gen.rb', line 10

def blank?(text)
  text.nil? || text.empty?
end

#bookmark_anchor_names(xml) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/isodoc/xref/xref_gen.rb', line 187

def bookmark_anchor_names(xml)
  xml.xpath(ns(".//bookmark")).reject { |n| blank?(n["id"]) }.each do |n|
    parent = nil
    n.ancestors.each do |a|
      next unless a["id"] && parent = @anchors.dig(a["id"], :xref)

      break
    end
    @anchors[n["id"]] = { type: "bookmark", label: nil, value: nil,
                          xref: parent || "???" }
  end
end

#deflist_anchor_names(sections) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/isodoc/xref/xref_gen.rb', line 153

def deflist_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(ns(".//dl")) - s.xpath(ns(".//clause//dl")) -
      s.xpath(ns(".//appendix//dl")) - s.xpath(ns(".//dl//dl"))
    c = Counter.new
    notes.reject { |n| blank?(n["id"]) }.each do |n|
      @anchors[n["id"]] =
        anchor_struct(increment_label(notes, n, c), n,
                      @labels["deflist"], "deflist", false)
      deflist_term_anchor_names(n, @anchors[n["id"]])
    end
    deflist_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#deflist_term_anchor_names(list, list_anchor) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/isodoc/xref/xref_gen.rb', line 168

def deflist_term_anchor_names(list, list_anchor)
  list.xpath(ns("./dt")).each do |li|
    label = l10n("#{list_anchor[:xref]}: #{dt2xreflabel(li)}")
    li["id"] and @anchors[li["id"]] =
                   { xref: label, type: "deflistitem",
                     container: list_anchor[:container] }
    li.xpath(ns("./dl")).each do |dl|
      deflist_term_anchor_names(dl, list_anchor)
    end
  end
end

#dt2xreflabel(dterm) ⇒ Object



180
181
182
183
184
185
# File 'lib/isodoc/xref/xref_gen.rb', line 180

def dt2xreflabel(dterm)
  label = dterm.dup
  label.xpath(ns(".//p")).each { |x| x.replace(x.children) }
  label.xpath(ns(".//index")).each(&:remove)
  label.children.to_xml
end

#example_anchor_names(sections) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/isodoc/xref/xref_gen.rb', line 110

def example_anchor_names(sections)
  sections.each do |s|
    c = Counter.new
    (notes = s.xpath(CHILD_EXAMPLES_XPATH)).each do |n|
      next if @anchors[n["id"]] || n["id"].nil? || n["id"].empty?

      @anchors[n["id"]] =
        anchor_struct(increment_label(notes, n, c), n,
                      @labels["example_xref"], "example", n["unnumbered"])
    end
    example_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#hierarchical_asset_names(clause, num) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 137

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",
                                @labels["permission"])
  hierarchical_permission_names(clause, num, "requirement",
                                @labels["requirement"])
  hierarchical_permission_names(clause, num, "recommendation",
                                @labels["recommendation"])
end

#hierarchical_figure_names(clause, num) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 109

def hierarchical_figure_names(clause, num)
  c = Counter.new
  j = 0
  clause.xpath(ns(".//figure |  .//sourcecode[not(ancestor::example)]"))
    .each do |t|
    next if labelled_ancestor(t) && t.ancestors("figure").empty?

    j = subfigure_increment(j, c, t)
    label = "#{num}#{hiersep}#{c.print}" +
      (j.zero? ? "" : "#{hierfigsep}#{j}")
    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



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

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

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



162
163
164
165
166
167
168
169
170
171
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 162

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

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



182
183
184
185
186
187
188
189
190
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 182

def hierarchical_permission_names1(block, lbl, klass, label)
  c = Counter.new
  block.xpath(ns("./#{klass}")).reject { |n| blank?(n["id"]) }.each do |t|
    id = "#{lbl}#{hierfigsep}#{c.increment(t).print}"
    @anchors[t["id"]] =
      anchor_struct(id, nil, label, klass, t["unnumbered"])
    hierarchical_permission_names2(t, id)
  end
end

#hierarchical_permission_names2(elem, ident) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 173

def hierarchical_permission_names2(elem, ident)
  hierarchical_permission_names1(elem, ident, "permission",
                                 @labels["permission"])
  hierarchical_permission_names1(elem, ident, "requirement",
                                 @labels["requirement"])
  hierarchical_permission_names1(elem, ident, "recommendation",
                                 @labels["recommendation"])
end

#hierarchical_table_names(clause, num) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 126

def hierarchical_table_names(clause, num)
  c = Counter.new
  clause.xpath(ns(".//table")).reject { |n| blank?(n["id"]) }.each do |t|
    next if labelled_ancestor(t)

    @anchors[t["id"]] =
      anchor_struct("#{num}#{hiersep}#{c.increment(t).print}",
                    nil, @labels["table"], "table", t["unnumbered"])
  end
end

#hierfigsepObject



10
11
12
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 10

def hierfigsep
  "-"
end

#hiersepObject



6
7
8
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 6

def hiersep
  "."
end

#increment_label(elems, node, counter, increment = true) ⇒ Object



38
39
40
41
42
43
# File 'lib/isodoc/xref/xref_gen.rb', line 38

def increment_label(elems, node, counter, increment = true)
  return "" if elems.size == 1 && !node["number"]

  counter.increment(node) if increment
  " #{counter.print}"
end

#list_anchor_names(sections) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/isodoc/xref/xref_gen.rb', line 124

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.reject { |n| blank?(n["id"]) }.each do |n|
      @anchors[n["id"]] = anchor_struct(increment_label(notes, n, c), n,
                                        @labels["list"], "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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/isodoc/xref/xref_gen.rb', line 138

def list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list)
  c = Counter.new(list["start"] ? list["start"].to_i - 1 : 0)
  list.xpath(ns("./li")).each do |li|
    label = c.increment(li).listlabel(list, 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



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

def note_anchor_names(sections)
  sections.each do |s|
    c = Counter.new
    (notes = s.xpath(CHILD_NOTES_XPATH)).each do |n|
      next if @anchors[n["id"]] || n["id"].nil? || n["id"].empty?

      @anchors[n["id"]] =
        anchor_struct(increment_label(notes, n, c), n,
                      @labels["note_xref"], "note", false)
    end
    note_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#sections_xpathObject



79
80
81
# File 'lib/isodoc/xref/xref_gen.rb', line 79

def sections_xpath
  SECTIONS_XPATH
end

#sequential_asset_names(clause) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 98

def sequential_asset_names(clause)
  sequential_table_names(clause)
  sequential_figure_names(clause)
  sequential_formula_names(clause)
  sequential_permission_names(clause, "permission", @labels["permission"])
  sequential_permission_names(clause, "requirement",
                              @labels["requirement"])
  sequential_permission_names(clause, "recommendation",
                              @labels["recommendation"])
end

#sequential_figure_names(clause) ⇒ Object



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

def sequential_figure_names(clause)
  c = Counter.new
  j = 0
  clause.xpath(ns(".//figure | .//sourcecode[not(ancestor::example)]"))
    .each do |t|
    next if labelled_ancestor(t) && t.ancestors("figure").empty?

    j = subfigure_increment(j, c, t)
    label = c.print + (j.zero? ? "" : "-#{j}")
    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



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

def sequential_formula_names(clause)
  c = Counter.new
  clause.xpath(ns(".//formula")).reject do |n|
    blank?(n["id"])
  end.each do |t|
    @anchors[t["id"]] = anchor_struct(
      c.increment(t).print, t,
      t["inequality"] ? @labels["inequality"] : @labels["formula"],
      "formula", t["unnumbered"]
    )
  end
end

#sequential_permission_names(clause, klass, label) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 68

def sequential_permission_names(clause, klass, label)
  c = Counter.new
  clause.xpath(ns(".//#{klass}#{FIRST_LVL_REQ}"))
    .reject { |n| blank?(n["id"]) }.each do |t|
    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



88
89
90
91
92
93
94
95
96
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 88

def sequential_permission_names1(block, lbl, klass, label)
  c = Counter.new
  block.xpath(ns("./#{klass}")).reject { |n| blank?(n["id"]) }.each do |t|
    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(elem, ident) ⇒ Object



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

def sequential_permission_names2(elem, ident)
  sequential_permission_names1(elem, ident, "permission",
                               @labels["permission"])
  sequential_permission_names1(elem, ident, "requirement",
                               @labels["requirement"])
  sequential_permission_names1(elem, ident, "recommendation",
                               @labels["recommendation"])
end

#sequential_table_names(clause) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 40

def sequential_table_names(clause)
  c = Counter.new
  clause.xpath(ns(".//table")).reject { |n| blank?(n["id"]) }.each do |t|
    next if labelled_ancestor(t)

    @anchors[t["id"]] = anchor_struct(
      c.increment(t).print, nil,
      @labels["table"], "table", t["unnumbered"]
    )
  end
end

#subfigure_increment(idx, counter, elem) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 14

def subfigure_increment(idx, counter, elem)
  if elem.parent.name == "figure" then idx += 1
  else
    idx = 0
    counter.increment(elem)
  end
  idx
end

#termexample_anchor_names(docxml) ⇒ Object



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

def termexample_anchor_names(docxml)
  docxml.xpath(ns("//term[termexample]")).each do |t|
    examples = t.xpath(ns("./termexample"))
    c = Counter.new
    examples.reject { |n| blank?(n["id"]) }.each do |n|
      c.increment(n)
      idx = increment_label(examples, n, c, false)
      @anchors[n["id"]] =
        anchor_struct(idx, n, @labels["example_xref"], "termexample",
                      n["unnumbered"])
    end
  end
end

#termnote_anchor_names(docxml) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/isodoc/xref/xref_gen.rb', line 45

def termnote_anchor_names(docxml)
  docxml.xpath(ns("//term[termnote]")).each do |t|
    c = Counter.new
    t.xpath(ns("./termnote")).reject { |n| blank?(n["id"]) }.each do |n|
      c.increment(n)
      @anchors[n["id"]] =
        { label: termnote_label(c.print), type: "termnote", value: c.print,
          xref: l10n("#{anchor(t['id'], :xref)}, "\
                     "#{@labels['note_xref']} #{c.print}") }
    end
  end
end

#termnote_label(note) ⇒ Object



34
35
36
# File 'lib/isodoc/xref/xref_gen.rb', line 34

def termnote_label(note)
  @labels["termnote"].gsub(/%/, note.to_s)
end