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
FIGURE_NO_CLASS =
<<~XPATH.freeze
  .//figure[not(@class)] | .//figure[@class = 'pseudocode'] | .//sourcecode[not(ancestor::example)]
XPATH
FIRST_LVL_REQ_RULE =
<<~XPATH.freeze
  [not(ancestor::permission or ancestor::requirement or ancestor::recommendation)]
XPATH
FIRST_LVL_REQ =
<<~XPATH.freeze
  .//permission#{FIRST_LVL_REQ_RULE} | .//requirement#{FIRST_LVL_REQ_RULE} | .//recommendation#{FIRST_LVL_REQ_RULE}
XPATH
REQ_CHILDREN =
<<~XPATH.freeze
  ./permission | ./requirement | ./recommendation
XPATH

Instance Method Summary collapse

Instance Method Details

#amend_autonums(amend) ⇒ Object



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

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

#amend_preprocess(xmldoc) ⇒ Object



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

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)


18
19
20
# File 'lib/isodoc/xref/xref_gen.rb', line 18

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

#bookmark_anchor_names(xml) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/isodoc/xref/xref_gen.rb', line 211

def bookmark_anchor_names(xml)
  xml.xpath(ns(".//bookmark")).noblank.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



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

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"))
    deflist_anchor_names1(notes, Counter.new)
    deflist_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#deflist_anchor_names1(notes, counter) ⇒ Object



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

def deflist_anchor_names1(notes, counter)
  notes.noblank.each do |n|
    @anchors[n["id"]] =
      anchor_struct(increment_label(notes, n, counter), n,
                    @labels["deflist"], "deflist", false)
    deflist_term_anchor_names(n, @anchors[n["id"]])
  end
end

#deflist_term_anchor_names(list, list_anchor) ⇒ Object



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

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



204
205
206
207
208
209
# File 'lib/isodoc/xref/xref_gen.rb', line 204

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



127
128
129
130
131
132
133
# File 'lib/isodoc/xref/xref_gen.rb', line 127

def example_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_EXAMPLES_XPATH)
    example_anchor_names1(notes, Counter.new)
    example_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#example_anchor_names1(notes, counter) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/isodoc/xref/xref_gen.rb', line 135

def example_anchor_names1(notes, counter)
  notes.each do |n|
    next if @anchors[n["id"]] || blank?(n["id"])

    @anchors[n["id"]] =
      anchor_struct(increment_label(notes, n, counter), n,
                    @labels["example_xref"], "example", n["unnumbered"])
  end
end

#hierarchical_asset_names(clause, num) ⇒ Object



192
193
194
195
196
197
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 192

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

#hierarchical_figure_body(num, subfignum, counter, block, klass) ⇒ Object



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

def hierarchical_figure_body(num, subfignum, counter, block, klass)
  label = "#{num}#{hiersep}#{counter.print}" +
    (subfignum.zero? ? "" : "#{hierfigsep}#{subfignum}")

  @anchors[block["id"]] =
    anchor_struct(label, nil, @labels[klass] || klass.capitalize,
                  klass, block["unnumbered"])
end

#hierarchical_figure_class_names(clause, num) ⇒ Object



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

def hierarchical_figure_class_names(clause, num)
  c = {}
  j = 0
  clause.xpath(ns(".//figure[@class][not(@class = 'pseudocode')]"))
    .noblank.each do |t|
    c[t["class"]] ||= Counter.new
    j = subfigure_increment(j, c[t["class"]], t)
    hierarchical_figure_body(num, j, c[t["class"]], t, t["class"])
  end
end

#hierarchical_figure_names(clause, num) ⇒ Object



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

def hierarchical_figure_names(clause, num)
  c = Counter.new
  j = 0
  clause.xpath(ns(FIGURE_NO_CLASS)).noblank.each do |t|
    # next if labelled_ancestor(t) && t.ancestors("figure").empty?
    j = subfigure_increment(j, c, t)
    hierarchical_figure_body(num, j, c, t, "figure")
  end
  hierarchical_figure_class_names(clause, num)
end

#hierarchical_formula_names(clause, num) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 199

def hierarchical_formula_names(clause, num)
  c = Counter.new
  clause.xpath(ns(".//formula")).noblank.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_body(id, block, label, klass, model) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 232

def hierarchical_permission_body(id, block, label, klass, model)
  @anchors[block["id"]] = model.postprocess_anchor_struct(
    block, anchor_struct(id, nil,
                         label, klass, block["unnumbered"])
  )

  model.permission_parts(block, id, label, klass).each do |n|
    @anchors[n[:id]] = anchor_struct(n[:number], nil, n[:label],
                                     n[:klass], false)
  end
end

#hierarchical_permission_children(block, lbl) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 221

def hierarchical_permission_children(block, lbl)
  c = ReqCounter.new
  block.xpath(ns(REQ_CHILDREN)).noblank.each do |t|
    m = @reqt_models.model(t["model"])
    klass, label = reqt2class_nested_label(t, m)
    id = "#{lbl}#{hierfigsep}#{c.increment(label, t).print}"
    hierarchical_permission_body(id, t, label, klass, m)
    hierarchical_permission_children(t, id)
  end
end

#hierarchical_permission_names(clause, num) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 210

def hierarchical_permission_names(clause, num)
  c = ReqCounter.new
  clause.xpath(ns(FIRST_LVL_REQ)).noblank.each do |t|
    m = @reqt_models.model(t["model"])
    klass, label = reqt2class_label(t, m)
    id = "#{num}#{hiersep}#{c.increment(label, t).print}"
    hierarchical_permission_body(id, t, label, klass, m)
    hierarchical_permission_children(t, id)
  end
end

#hierarchical_table_names(clause, num) ⇒ Object



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

def hierarchical_table_names(clause, num)
  c = Counter.new
  clause.xpath(ns(".//table")).noblank.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



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

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



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/isodoc/xref/xref_gen.rb', line 145

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.noblank.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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/isodoc/xref/xref_gen.rb', line 159

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", refer_list:
                     refer_list, container: list_anchor[:container] }
    (li.xpath(ns(".//ol")) - li.xpath(ns(".//ol//ol"))).each do |ol|
      list_item_anchor_names(ol, list_anchor, depth + 1, label, false)
    end
  end
end

#noblank(xpath) ⇒ Object



22
23
24
# File 'lib/isodoc/xref/xref_gen.rb', line 22

def noblank(xpath)
  xpath.reject { |n| blank?(n["id"]) }
end

#note_anchor_names(sections) ⇒ Object



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

def note_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_NOTES_XPATH) -
      s.xpath(ns(".//figure//note | .//table//note"))
    note_anchor_names1(notes, Counter.new)
    note_anchor_names(s.xpath(ns(CHILD_SECTIONS)))
  end
end

#note_anchor_names1(notes, counter) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/isodoc/xref/xref_gen.rb', line 109

def note_anchor_names1(notes, counter)
  notes.each do |n|
    next if @anchors[n["id"]] || blank?(n["id"])

    @anchors[n["id"]] =
      anchor_struct(increment_label(notes, n, counter), n,
                    @labels["note_xref"], "note", false)
  end
end

#reqt2class_label(block, model) ⇒ Object



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

def reqt2class_label(block, model)
  model.req_class_paths.each do |n|
    v1 = ns("/#{n[:xpath]}").sub(%r{^/}, "")
    block.at("./self::#{v1}") and return [n[:klass], n[:label]]
  end
  [nil, nil]
end

#reqt2class_nested_label(block, model) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 135

def reqt2class_nested_label(block, model)
  model.req_nested_class_paths.each do |n|
    v1 = ns("/#{n[:xpath]}").sub(%r{^/}, "")
    block.at("./self::#{v1}") and return [n[:klass], n[:label]]
  end
  [nil, nil]
end

#sections_xpathObject



91
92
93
# File 'lib/isodoc/xref/xref_gen.rb', line 91

def sections_xpath
  SECTIONS_XPATH
end

#sequential_asset_names(clause) ⇒ Object



143
144
145
146
147
148
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 143

def sequential_asset_names(clause)
  sequential_table_names(clause)
  sequential_figure_names(clause)
  sequential_formula_names(clause)
  sequential_permission_names(clause)
end

#sequential_figure_body(subfignum, counter, block, klass) ⇒ Object



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

def sequential_figure_body(subfignum, counter, block, klass)
  label = counter.print
  label &&= label + (subfignum.zero? ? "" : "-#{subfignum}")

  @anchors[block["id"]] = anchor_struct(
    label, nil, @labels[klass] || klass.capitalize, klass,
    block["unnumbered"]
  )
end

#sequential_figure_class_names(clause) ⇒ Object



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

def sequential_figure_class_names(clause)
  c = {}
  j = 0
  clause.xpath(ns(".//figure[@class][not(@class = 'pseudocode')]"))
    .each do |t|
    c[t["class"]] ||= Counter.new
    j = subfigure_increment(j, c[t["class"]], t)
    sequential_figure_body(j, c[t["class"]], t, t["class"])
  end
end

#sequential_figure_names(clause) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 27

def sequential_figure_names(clause)
  c = Counter.new
  j = 0
  clause.xpath(ns(FIGURE_NO_CLASS)).noblank.each do |t|
    j = subfigure_increment(j, c, t)
    sequential_figure_body(j, c, t, "figure")
  end
  sequential_figure_class_names(clause)
end

#sequential_formula_names(clause) ⇒ Object



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

def sequential_formula_names(clause)
  c = Counter.new
  clause.xpath(ns(".//formula")).noblank.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_body(id, block, label, klass, model) ⇒ Object



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

def sequential_permission_body(id, block, label, klass, model)
  @anchors[block["id"]] = model.postprocess_anchor_struct(
    block, anchor_struct(id, block,
                         label, klass, block["unnumbered"])
  )

  model.permission_parts(block, id, label, klass).each do |n|
    @anchors[n[:id]] = anchor_struct(n[:number], n[:elem], n[:label],
                                     n[:klass], false)
  end
end

#sequential_permission_children(block, lbl) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 104

def sequential_permission_children(block, lbl)
  c = ReqCounter.new
  block.xpath(ns(REQ_CHILDREN)).noblank.each do |t|
    m = @reqt_models.model(t["model"])
    klass, label = reqt2class_nested_label(t, m)
    id = "#{lbl}#{hierfigsep}#{c.increment(label, t).print}"
    sequential_permission_body(id, t, label, klass, m)
    sequential_permission_children(t, id)
  end
end

#sequential_permission_names(clause) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 93

def sequential_permission_names(clause)
  c = ReqCounter.new
  clause.xpath(ns(FIRST_LVL_REQ)).noblank.each do |t|
    m = @reqt_models.model(t["model"])
    klass, label = reqt2class_label(t, m)
    id = c.increment(label, t).print
    sequential_permission_body(id, t, label, klass, m)
    sequential_permission_children(t, id)
  end
end

#sequential_table_names(clause) ⇒ Object



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

def sequential_table_names(clause)
  c = Counter.new
  clause.xpath(ns(".//table")).noblank.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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/isodoc/xref/xref_gen.rb', line 71

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

#termnote_anchor_names(docxml) ⇒ Object



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

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

#termnote_label(note) ⇒ Object



46
47
48
# File 'lib/isodoc/xref/xref_gen.rb', line 46

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