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/clause | "\
"//preface/terms | preface/definitions | preface/references | "\
"//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"
FIRST_LVL_REQ =
"[not(ancestor::permission or ancestor::requirement or "\
"ancestor::recommendation)]".freeze

Instance Method Summary collapse

Instance Method Details

#amend_autonums(amend) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/isodoc/xref/xref_gen.rb', line 20

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

#amend_preprocess(xmldoc) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/isodoc/xref/xref_gen.rb', line 8

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

#bookmark_anchor_names(docxml) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/isodoc/xref/xref_gen.rb', line 156

def bookmark_anchor_names(docxml)
  docxml.xpath(ns(".//bookmark")).each do |n|
    next if n["id"].nil? || n["id"].empty?

    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

#example_anchor_names(sections) ⇒ Object



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

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"]] || 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



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

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



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

def hierarchical_figure_names(clause, num)
  c = Counter.new
  j = 0
  clause.xpath(ns(".//figure |  .//sourcecode[not(ancestor::example)]"))
    .each do |t|
    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



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 142

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}", nil,
      t["inequality"] ? @labels["inequality"] : @labels["formula"],
      "formula", t["unnumbered"]
    )
  end
end

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



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

def hierarchical_permission_names(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?

    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



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 176

def hierarchical_permission_names1(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, nil, label, klass, t["unnumbered"])
    hierarchical_permission_names2(t, id)
  end
end

#hierarchical_permission_names2(elem, ident) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 167

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 119

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, @labels["table"], "table", t["unnumbered"])
  end
end

#hierfigsepObject



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

def hierfigsep
  "-"
end

#hiersepObject



3
4
5
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 3

def hiersep
  "."
end

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



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

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



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

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?

      @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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/isodoc/xref/xref_gen.rb', line 141

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



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

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?

      @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



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

def sections_xpath
  SECTIONS_XPATH
end

#sequential_asset_names(clause) ⇒ Object



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

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



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

def sequential_figure_names(clause)
  c = Counter.new
  j = 0
  clause.xpath(ns(".//figure | .//sourcecode[not(ancestor::example)]"))
    .each do |t|
    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



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

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"] ? @labels["inequality"] : @labels["formula"],
      "formula", t["unnumbered"]
    )
  end
end

#sequential_permission_names(clause, klass, label) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 63

def sequential_permission_names(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"])
    sequential_permission_names2(t, id)
  end
end

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 83

def sequential_permission_names1(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"])
    sequential_permission_names2(t, id)
  end
end

#sequential_permission_names2(elem, ident) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 74

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



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

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,
      @labels["table"], "table", t["unnumbered"]
    )
  end
end

#subfigure_increment(idx, counter, elem) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/isodoc/xref/xref_gen_seq.rb', line 11

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



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

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|
      next if n["id"].nil? || n["id"].empty?

      c.increment(n)
      idx = increment_label(examples, n, c, false)
      @anchors[n["id"]] = {
        type: "termexample", label: idx, value: c.print,
        xref: l10n("#{anchor(t['id'], :xref)}, "\
                   "#{@labels['example_xref']} #{c.print}") }
    end
  end
end

#termnote_anchor_names(docxml) ⇒ Object



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

def termnote_anchor_names(docxml)
  docxml.xpath(ns("//term[descendant::termnote]")).each do |t|
    c = Counter.new
    t.xpath(ns(".//termnote")).each do |n|
      next if n["id"].nil? || n["id"].empty?

      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



28
29
30
# File 'lib/isodoc/xref/xref_gen.rb', line 28

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