Class: IsoDoc::IEEE::PresentationXMLConvert

Inherits:
PresentationXMLConvert
  • Object
show all
Includes:
Init
Defined in:
lib/isodoc/ieee/presentation_terms.rb,
lib/isodoc/ieee/presentation_xml_convert.rb

Instance Method Summary collapse

Methods included from Init

#fileloc, #i18n_init, #metadata_init, #xref_init

Constructor Details

#initialize(options) ⇒ PresentationXMLConvert

Returns a new instance of PresentationXMLConvert.



8
9
10
11
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 8

def initialize(options)
  @hierarchical_assets = options[:hierarchical_assets]
  super
end

Instance Method Details



100
101
102
103
104
105
106
# File 'lib/isodoc/ieee/presentation_terms.rb', line 100

def admitted_to_related(docxml)
  docxml.xpath(ns("//term/admitted")).each do |a|
    a["type"] = "equivalent"
    a.name = "related"
    a.children = "<preferred>#{a.children.to_xml}</preferred>"
  end
end

#anchor_linkend(node, linkend) ⇒ Object

Style manual 19



57
58
59
60
61
62
63
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 57

def anchor_linkend(node, linkend)
  @bibanchors ||= biblio_ids_titles(node.document)
  if node["citeas"] && i = @bibanchors[node["bibitemid"]]
    biblio_anchor_linkend(node, i)
  else super
  end
end

#anchor_linkend1(node) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 85

def anchor_linkend1(node)
  linkend = @xrefs.anchor(node["target"], :xref)
  @xrefs.anchor(node["target"], :type) == "clause" &&
    @xrefs.anchor(node["target"], :level) > 1 &&
    !start_of_sentence(node) and
    linkend = linkend.sub(/^Clause /, "")
  container = @xrefs.anchor(node["target"], :container, false)
  prefix_container?(container, node) and
    linkend = prefix_container(container, linkend, node["target"])
  capitalise_xref(node, linkend, anchor_value(node["target"]))
end

#annex1(elem) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 163

def annex1(elem)
  lbl = @xrefs.anchor(elem["id"], :label)
  if t = elem.at(ns("./title"))
    t.children = "<strong>#{t.children.to_xml}</strong>"
  end
  prefix_name(elem, "<br/>", lbl, "title")
end

#biblio_anchor_linkend(node, bib) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 65

def biblio_anchor_linkend(node, bib)
  if %w(techreport standard).include?(bib[:type])
    node["citeas"] + " #{bib[:ord]}"
  else
    "#{bib[:title]} " + node["citeas"]
  end
end

#biblio_ids_titles(xmldoc) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 73

def biblio_ids_titles(xmldoc)
  xmldoc.xpath(ns("//references[@normative = 'false']/bibitem"))
    .each_with_object({}) do |b, m|
    m[b["id"]] =
      { docid: pref_ref_code(b), type: b["type"],
        title: b.at(ns("./title"))&.text ||
               b.at(ns("./formattedref"))&.text,
        ord: b.at(ns("./docidentifier[@type = 'metanorma' or "\
                     "@type = 'metanorma-ordinal']")).text }
  end
end

#bibliography_bibitem_number1(bibitem, idx) ⇒ Object



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

def bibliography_bibitem_number1(bibitem, idx)
  if mn = bibitem.at(ns(".//docidentifier[@type = 'metanorma']"))
    /^\[?\d\]?$/.match?(mn&.text) and
      idx = mn.text.sub(/^\[B?/, "").sub(/\]$/, "").to_i
  end
  unless bibliography_bibitem_number_skip(bibitem)

    idx += 1
    bibitem.at(ns(".//docidentifier")).previous =
      "<docidentifier type='metanorma-ordinal'>[B#{idx}]</docidentifier>"
  end
  idx
end

#bibrender_relaton(xml) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 132

def bibrender_relaton(xml)
  bib = xml.dup
  bib["suppress_identifier"] == true and
    bib.xpath(ns("./docidentifier")).each(&:remove)
  xml.children =
    "#{bibrenderer.render(bib.to_xml)}"\
    "#{xml.xpath(ns('./docidentifier | ./uri | ./note | ./title')).to_xml}"
end

#bibrendererObject



127
128
129
130
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 127

def bibrenderer
  ::Relaton::Render::IEEE::General.new(language: @lang,
                                       i18nhash: @i18n.get)
end

#block_delimObject



97
98
99
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 97

def block_delim
  "&#x2014;"
end

#collapse_multidef(elem) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/isodoc/ieee/presentation_terms.rb', line 19

def collapse_multidef(elem)
  ins = elem.at(ns("./definition")).previous_element
  coll = elem.xpath(ns("./definition"))
  coll.each(&:remove)
  ins.next = "<definition>#{coll.map do |c|
                              c.children.to_xml
                            end }</definition>"
end

#collapse_term(docxml) ⇒ Object



108
109
110
111
112
# File 'lib/isodoc/ieee/presentation_terms.rb', line 108

def collapse_term(docxml)
  docxml.xpath(ns("//term")).each do |t|
    collapse_term1(t)
  end
end

#collapse_term1(term) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/isodoc/ieee/presentation_terms.rb', line 114

def collapse_term1(term)
  ret = collapse_term_template(
    pref: term.at(ns("./preferred")).remove,
    def: term.at(ns("./definition")),
    rels: term.xpath(ns("./related")).map(&:remove),
    source: term.at(ns("./termsource")),
  )
  (ins = term.elements.first and ins.previous = ret) or
    term << ret
end


125
126
127
128
129
130
131
132
# File 'lib/isodoc/ieee/presentation_terms.rb', line 125

def collapse_term_related(rels)
  ret = rels.map do |r|
    "<em>#{@i18n.relatedterms[r['type']]}:</em> "\
      "#{r.at(ns('./preferred')).children.to_xml}"
  end.join(". ")
  ret += "." unless ret.empty?
  ret
end

#collapse_term_template(opt) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/isodoc/ieee/presentation_terms.rb', line 134

def collapse_term_template(opt)
  defn = collapse_unwrap_definition(opt[:def])
  src = nil
  opt[:source] and src = "(#{opt[:source].remove.children.to_xml.strip})"
  <<~TERM
    <p>#{opt[:pref].children.to_xml}: #{defn}
    #{collapse_term_related(opt[:rels])}
    #{src}</p>
  TERM
end

#collapse_unwrap_definition(defn) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/isodoc/ieee/presentation_terms.rb', line 145

def collapse_unwrap_definition(defn)
  return nil if defn.nil?

  s = defn.remove.xpath(ns("./termsource"))
  p = defn.at(ns("./p"))
  !s.empty? && p and p << s.map(&:remove).map(&:children).map(&:to_xml).join
  if defn.elements.size == 1 && defn.elements.first.name == "p"
    defn.elements.first.children
  else defn.elements
  end
end

#concept1(node) ⇒ Object



188
189
190
191
# File 'lib/isodoc/ieee/presentation_terms.rb', line 188

def concept1(node)
  concept_render(node, ital: "false", ref: "false",
                       linkref: "false", linkmention: "false")
end

#creatornames(bibitem) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 141

def creatornames(bibitem)
  ::Relaton::Render::IEEE::General
    .new(language: @lang, i18nhash: @i18n.get,
         template: { (bibitem["type"] || "misc").to_sym =>
                     "{{ creatornames }}" })
    .parse1(RelatonBib::XMLParser.from_xml(bibitem.to_xml))
end

#designation_field(desgn, name) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/isodoc/ieee/presentation_terms.rb', line 163

def designation_field(desgn, name)
  if desgn.name == "preferred"
    f = desgn.xpath(ns("./../domain | ./../subject")).map(&:remove)
      .map { |u| u.children.to_xml }.join(", ")
    name << ", &#x3c;#{f}&#x3e;" unless f.empty?
  end
  super
end

#display_order(docxml) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 113

def display_order(docxml)
  i = 0
  i = display_order_xpath(docxml, "//preface/*", i)
  i = display_order_at(docxml, "//clause[@type = 'overview']", i)
  i = display_order_at(docxml, @xrefs.klass.norm_ref_xpath, i)
  i = display_order_at(docxml, "//sections/terms | "\
                               "//sections/clause[descendant::terms]", i)
  i = display_order_at(docxml, "//sections/definitions", i)
  i = display_order_xpath(docxml, @xrefs.klass.middle_clause(docxml), i)
  i = display_order_xpath(docxml, "//annex", i)
  i = display_order_xpath(docxml, @xrefs.klass.bibliography_xpath, i)
  display_order_xpath(docxml, "//indexsect", i)
end

#eref_localities1(target, type, from, upto, node, lang = "en") ⇒ Object



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

def eref_localities1(target, type, from, upto, node, lang = "en")
  return nil if type == "anchor"

  type = type.downcase
  lang == "zh" and
    return l10n(eref_localities1_zh(target, type, from, upto,
                                    node))
  ret = ""
  node["droploc"] != "true" && !subclause?(target, type,
                                           from) and
    ret = eref_locality_populate(type, node)
  ret += " #{from}" if from
  ret += "&#x2013;#{upto}" if upto
  ret += ")" if type == "list"
  l10n(ret)
end

#eref_localities_conflated(refs, target, node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 13

def eref_localities_conflated(refs, target, node)
  droploc = node["droploc"]
  node["droploc"] = true
  ret = resolve_eref_connectives(eref_locality_stacks(refs, target,
                                                      node))
  node["droploc"] = droploc
  eref_localities1(target,
                   prefix_clause(target, refs.first.at(ns("./locality"))),
                   l10n(ret[1..-1].join), nil, node, @lang)
end

#merge_second_preferred(term) ⇒ Object



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

def merge_second_preferred(term)
  super
  term.xpath(ns("./preferred[expression/name]")).each_with_index do |p, i|
    unless i.zero?
      p.remove # whatever was eligible to display has already been merged
    end
  end
end

#multidef(elem) ⇒ Object



6
7
8
9
# File 'lib/isodoc/ieee/presentation_terms.rb', line 6

def multidef(elem)
  number_multidef(elem)
  collapse_multidef(elem)
end

#note1(elem) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 101

def note1(elem)
  return if elem.parent.name == "bibitem" || elem["notag"] == "true"

  n = @xrefs.get[elem["id"]]
  lbl = if n.nil? || n[:label].nil? || n[:label].empty?
          @i18n.note
        else
          l10n("#{@i18n.note} #{n[:label]}")
        end
  prefix_name(elem, block_delim, lbl, "name")
end

#number_multidef(elem) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/isodoc/ieee/presentation_terms.rb', line 11

def number_multidef(elem)
  c = IsoDoc::XrefGen::Counter.new("@")
  elem.xpath(ns("./definition")).each do |d|
    c.increment(d)
    d.elements.first.previous = "<strong>(#{c.print})</strong>&#xa0;"
  end
end

#prefix_clause(target, loc) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 24

def prefix_clause(target, loc)
  loc["type"] == "clause" or return loc["type"]

  if subclause?(target, loc["type"],
                loc&.at(ns("./referenceFrom"))&.text)
    ""
  else "clause"
  end
end


40
41
42
# File 'lib/isodoc/ieee/presentation_terms.rb', line 40

def related(docxml)
  docxml.xpath(ns("//term[related]")).each { |f| related_term(f) }
end


44
45
46
47
# File 'lib/isodoc/ieee/presentation_terms.rb', line 44

def related_term(term)
  coll = term_related_reorder(term.xpath(ns("./related")))
  term_related_collapse(coll)
end


77
78
79
80
81
# File 'lib/isodoc/ieee/presentation_terms.rb', line 77

def sort_related(coll)
  coll.sort do |a, b|
    sort_related_key(a) <=> sort_related_key(b)
  end
end


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

def sort_related_key(related)
  type = case related["type"]
         when "contrast" then 1
         when "equivalent" then 2
         when "see" then 3
         when "seealso" then 4
         else "5-#{related['type']}"
         end
  "#{type} :: #{sort_terms_key(related)}"
end

#sort_terms_key(term) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/isodoc/ieee/presentation_terms.rb', line 61

def sort_terms_key(term)
  d = term.at(ns("./preferred/expression/name | "\
                 "./preferred/letter-designation/name | "\
                 "./preferred/graphical-symbol/figure/name | "\
                 "./preferred/graphical-symbol/figure/@id"))
  d&.text&.downcase
end

#subclause?(target, type, from) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/isodoc/ieee/presentation_xml_convert.rb', line 34

def subclause?(target, type, from)
  (from&.match?(/\./) && type == "clause") ||
    target&.gsub(/<[^>]+>/, "")&.match(/^IEV$|^IEC 60050-/)
end

#term(docxml) ⇒ Object



186
# File 'lib/isodoc/ieee/presentation_terms.rb', line 186

def term(docxml); end


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/isodoc/ieee/presentation_terms.rb', line 49

def term_related_collapse(coll)
  prev = 0
  coll[1..-1].each_with_index do |r, i|
    if coll[prev]["type"] == r["type"]
      coll[prev].at(ns("./preferred")) << "; #{r.at(ns('./preferred'))
        .children.to_xml}"
      r.remove
    else prev = i
    end
  end
end


69
70
71
72
73
74
75
# File 'lib/isodoc/ieee/presentation_terms.rb', line 69

def term_related_reorder(coll)
  ins = coll.first.previous_element
  ret = sort_related(coll)
  coll.each(&:remove)
  ret.reverse.each { |t| ins.next = t }
  ins.parent.xpath(ns("./related"))
end

#termnote1(elem) ⇒ Object



181
182
183
184
# File 'lib/isodoc/ieee/presentation_terms.rb', line 181

def termnote1(elem)
  lbl = l10n(@xrefs.anchor(elem["id"], :label)&.strip || "???")
  prefix_name(elem, block_delim, lower2cap(lbl), "name")
end

#terms(docxml) ⇒ Object



94
95
96
97
98
# File 'lib/isodoc/ieee/presentation_terms.rb', line 94

def terms(docxml)
  admitted_to_related docxml
  super
  collapse_term docxml
end

#termsource1(elem) ⇒ Object



157
158
159
160
161
# File 'lib/isodoc/ieee/presentation_terms.rb', line 157

def termsource1(elem)
  while elem&.next_element&.name == "termsource"
    elem << "; #{elem.next_element.remove.children.to_xml}"
  end
end

#unwrap_definition(docxml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/isodoc/ieee/presentation_terms.rb', line 28

def unwrap_definition(docxml)
  docxml.xpath(ns("//definition/verbal-definition")).each do |v|
    next unless v.elements.all? { |e| %w(termsource p).include?(e.name) }

    s = v.xpath(ns("./termsource"))
    p = v.xpath(ns("./p"))
    v.children =
      "<p>#{p.map(&:children).map(&:to_xml).join("\n")}</p>#{s.to_xml}"
  end
  super
end