Class: IsoDoc::Iec::PresentationXMLConvert

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

Constant Summary collapse

DICT_PATHS =
{ doctype_dict: "./ext/doctype",
substage_dict: "./status/substage",
function_dict: "./ext/function",
horizontal_dict: "./ext/horizontal" }.freeze

Instance Method Summary collapse

Methods included from Init

#bibrenderer, #convert1, #metadata_init, #std_docid_semantic, #xref_init

Instance Method Details

#bibdata_i18n(bib) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 41

def bibdata_i18n(bib)
  [{ lang: "en", i18n: IsoDoc::Iec::I18n.new("en", "Latn") },
   { lang: "fr", i18n: IsoDoc::Iec::I18n.new("fr", "Latn") }].each do |v|
    DICT_PATHS.each do |lbl, xpath|
      hash_translate(bib, v[:i18n].get[lbl.to_s], xpath, v[:lang])
    end
    bibdata_i18n_stage(bib, bib.at(ns("./status/stage")),
                       bib.at(ns("./ext/doctype")),
                       lang: v[:lang], i18n: v[:i18n])
  end
end

#clause(docxml) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 17

def clause(docxml)
  docxml.xpath(ns("//clause[not(ancestor::annex)] | " \
                  "//definitions | //references | " \
                  "//preface/introduction[clause]"))
    .each do |f|
    f.parent.name == "annex" &&
      @xrefs.klass.single_term_clause?(f.parent) and next
    clause1(f)
  end
  docxml.xpath(ns("//terms")).each do |f|
    termclause1(f)
  end
end

#clause1(elem) ⇒ Object



31
32
33
34
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 31

def clause1(elem)
  IsoDoc::PresentationXMLConvert.instance_method(:clause1).bind(self)
    .call(elem)
end

#concept(docxml) ⇒ Object



11
12
13
14
# File 'lib/isodoc/iec/presentation_terms.rb', line 11

def concept(docxml)
  @is_iev and concept_iev(docxml)
  super
end

#concept_iev(docxml) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/isodoc/iec/presentation_terms.rb', line 16

def concept_iev(docxml)
  labels = @xrefs.get_anchors.each_with_object({}) do |(k, v), m|
    m[v[:label]] = k
  end
  docpart = docxml&.at(ns("//bibdata/ext/structuredidentifier/" \
                          "project-number/@part"))&.text or return
  docxml.xpath(ns("//termref[@base = 'IEV']")).each do |t|
    concept_iev1(t, docpart, labels)
  end
end

#concept_iev1(termref, docpart, labels) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/isodoc/iec/presentation_terms.rb', line 27

def concept_iev1(termref, docpart, labels)
  /^#{docpart}-/.match?(termref["target"]) or return
  newtarget = labels[termref["target"]] or return
  termref.name = "xref"
  termref.delete("base")
  termref["target"] = newtarget
end

#extract_otherlang_designations(term, lgs) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/isodoc/iec/presentation_terms.rb', line 67

def extract_otherlang_designations(term, lgs)
  term.xpath(ns(".//preferred/expression[@language]"))
    .each_with_object([]) do |d, m|
    lg = d["language"]
    d.delete("language")
    next if lgs.include?(lg)

    p = d.parent
    designation_annotate(p, d.at(ns("./name")))
    m << { lang: lg, script: Metanorma::Utils.default_script(lg),
           designation: to_xml(l10n_recursive(p.remove, lg)).strip }
  end
end

#i18n_init(lang, script, locale, i18nyaml = nil) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 9

def i18n_init(lang, script, locale, i18nyaml = nil)
  super
  @i18n_lg = {}
  @i18n_lg["en"] = I18n.new("en", "Latn", i18nyaml: i18nyaml || @i18nyaml)
  @i18n_lg["fr"] = I18n.new("fr", "Latn", i18nyaml: i18nyaml || @i18nyaml)
  @i18n_lg["default"] = @i18n
end

#insert_foreword(docxml) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 59

def insert_foreword(docxml)
  @meta.get[:doctype] == "Amendment" and return
  b = docxml.at(ns("//boilerplate/legal-statement")) or return
  unless f = docxml.at(ns("//preface/foreword"))
    ins = toc_title_insert_pt(docxml)
    f = ins.before(<<~CLAUSE).previous_element
      <foreword id='_#{UUIDTools::UUID.random_create}'> </foreword>
    CLAUSE
  end
  f.children.empty? and f.children = " "
  ins = f.at(ns("./title")) || f.children.first.before(" ").previous
  ins.next =
    "<clause type='boilerplate_legal'>#{to_xml(b.children)}</clause>"
end

#insert_middle_title(docxml) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 74

def insert_middle_title(docxml)
  ins = docxml.at(ns("//preface/clause[@type = 'toc']")) or return
  title1, title2 = middle_title_parts
  title2out = ""
  title2 and title2out = <<~OUTPUT
    <p class="zzSTDTitle1">&#xa0;</p>
    <p class="zzSTDTitle2"><strong>#{title2}</strong></p>
  OUTPUT
  title1 &&= Metanorma::Utils.case_transform_xml(title1, :upcase)
  ins.next = <<~OUTPUT
    <pagebreak/><p class="zzSTDTitle1">#{@i18n.get['IEC']}</p>
    <p class="zzSTDTitle1">____________</p><p class="zzSTDTitle1">&#xa0;</p>
    <p class="zzSTDTitle1"><strong>#{title1}</strong></p>#{title2out}
    <p class="zzSTDTitle1">&#xa0;</p>
  OUTPUT
end

#l10n_recursive(xml, lang) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/isodoc/iec/presentation_terms.rb', line 81

def l10n_recursive(xml, lang)
  script = Metanorma::Utils.default_script(lang)
  c = HTMLEntities.new
  xml.traverse do |x|
    x.text? or next
    text = c.encode(c.decode(x.text), :hexadecimal)
    x.replace(cleanup_entities(l10n(text, lang, script), is_xml: false))
  end
  xml
end

#merge_fr_into_en_term(docxml) ⇒ Object



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

def merge_fr_into_en_term(docxml)
  @is_iev or return
  docxml.xpath(ns("//term[@language = 'en'][@tag]")).each do |en|
    fr = docxml.at(ns("//term[@language = 'fr'][@tag = '#{en['tag']}']"))
    merge_fr_into_en_term1(en, fr) if fr
  end
  @xrefs.parse_inclusions(clauses: true).parse docxml
  docxml.xpath(ns("//term/name")).each(&:remove)
  term(docxml)
end

#merge_fr_into_en_term1(en_term, fr_term) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/isodoc/iec/presentation_terms.rb', line 52

def merge_fr_into_en_term1(en_term, fr_term)
  dl = en_term&.at(ns("./dl[@type = 'other-lang']"))&.remove
  en_term << fr_term.remove.children
  en_term << dl if dl
  en_term["language"] = "en,fr"
  en_term.delete("tag")
end

#merge_otherlang_designations(desgn) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/isodoc/iec/presentation_terms.rb', line 92

def merge_otherlang_designations(desgn)
  h = desgn.each_with_object({}) do |e, m|
    if m[e[:lang]]
      m[e[:lang]][:designation] += e[:designation]
    else m[e[:lang]] = e
    end
  end
  h.keys.sort.each_with_object([]) { |k, m| m << h[k] }
end

#middle_title(docxml) ⇒ Object



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

def middle_title(docxml)
  s = docxml.at(ns("//sections")) or return
  title1, title2 = middle_title_parts
  title1 || title2 or return
  ret = ""
  title1 and ret = "<p class='zzSTDTitle1'><strong>#{title1}</strong></p>"
  title2 and ret += <<~TITLE
    <p class='zzSTDTitle1'>&#xa0;</p>
    <p class='zzSTDTitle2'><strong>#{title2}</strong><p>
  TITLE
  ret += "<p class='zzSTDTitle1'>&#xa0;</p>"
  s.children.first.previous = ret
end

#middle_title_part(title1, title2) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 100

def middle_title_part(title1, title2)
  if @meta.get[:doctitlepart]
    title1 += " \u2014"
    title2 = @meta.get[:doctitlepart]&.sub(/\s+$/, "")
    @meta.get[:doctitlepartlabel] and
      title2 = "#{@meta.get[:doctitlepartlabel]}: #{title2}"
  end
  [title1, title2]
end

#middle_title_partsObject



91
92
93
94
95
96
97
98
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 91

def middle_title_parts
  title1 = @meta.get[:doctitlemain]&.sub(/\s+$/, "")
  @meta.get[:doctitleintro] and
    title1 = "#{@meta.get[:doctitleintro]} \u2014 #{title1}"
  title1, title2 = middle_title_part(title1, nil)
  title1&.empty? and title1 = nil
  [title1, title2]
end


120
121
122
123
124
125
# File 'lib/isodoc/iec/presentation_terms.rb', line 120

def move_related(term)
  defn = term.at(ns("./definition")) or return
  term.xpath(ns("./related")).reverse.each do |r|
    defn.next = r.remove
  end
end

#otherlang_designations(docxml) ⇒ Object



60
61
62
63
64
65
# File 'lib/isodoc/iec/presentation_terms.rb', line 60

def otherlang_designations(docxml)
  @is_iev or return
  docxml.xpath(ns("//term")).each do |t|
    otherlang_designations1(t, t["language"]&.split(",") || %w(en fr))
  end
end

#otherlang_designations1(term, lgs) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/iec/presentation_terms.rb', line 102

def otherlang_designations1(term, lgs)
  pr = merge_otherlang_designations(
    extract_otherlang_designations(term, lgs),
  )
  pr.empty? and return
  prefs = pr.map do |p|
    "<dt>#{p[:lang]}</dt>" \
      "<dd language='#{p[:lang]}' script='#{p[:script]}'>" \
      "#{cleanup_entities(p[:designation])}</dd>"
  end
  term << "<dl type='other-lang'>#{prefs.join}</dl>"
end

#rearrange_clauses(docxml) ⇒ Object



53
54
55
56
57
# File 'lib/isodoc/iec/presentation_xml_convert.rb', line 53

def rearrange_clauses(docxml)
  insert_foreword(docxml) # feeds preface_rearrange
  super
  insert_middle_title(docxml)
end


115
116
117
118
# File 'lib/isodoc/iec/presentation_terms.rb', line 115

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

#related1(node) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/isodoc/iec/presentation_terms.rb', line 127

def related1(node)
  lg = node&.at("./ancestor::xmlns:term/@language")&.text
  @i18n = @i18n_lg[lg] if lg && @i18n_lg[lg]
  p = node.at(ns("./preferred"))
  ref = node.at(ns("./xref | ./eref | ./termref"))
  label = @i18n.relatedterms[node["type"]].upcase
  node.replace(l10n("<p>#{label}: " \
                    "#{to_xml(p.children)} (#{to_xml(ref)})</p>"))
  @i18n = @i18n_lg["default"]
end

#termclause1(elem) ⇒ Object



4
5
6
7
8
9
# File 'lib/isodoc/iec/presentation_terms.rb', line 4

def termclause1(elem)
  @is_iev or return clause1(elem)
  @suppressheadingnumbers || elem["unnumbered"] and return
  lbl = @xrefs.anchor(elem["id"], :label, true) or return
  prefix_name(elem, " ", "#{lbl}#{clausedelim}", "title")
end

#termexample(docxml) ⇒ Object



161
162
163
164
165
# File 'lib/isodoc/iec/presentation_terms.rb', line 161

def termexample(docxml)
  docxml.xpath(ns("//termexample")).each do |f|
    termexample1(f)
  end
end

#termexample1(elem) ⇒ Object



167
168
169
170
171
172
# File 'lib/isodoc/iec/presentation_terms.rb', line 167

def termexample1(elem)
  lg = elem&.at("./ancestor::xmlns:term/@language")&.text
  @i18n = @i18n_lg[lg] if lg && @i18n_lg[lg]
  example1(elem)
  @i18n = @i18n_lg["default"]
end

#termnote1(elem) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/isodoc/iec/presentation_terms.rb', line 174

def termnote1(elem)
  lg = elem&.at("./ancestor::xmlns:term/@language")&.text
  @i18n = @i18n_lg[lg] if lg && @i18n_lg[lg]

  val = @xrefs.anchor(elem["id"], :value) || "???"
  lbl = @i18n.termnote.gsub("%", val)
  prefix_name(elem, "", lower2cap(lbl), "name")
  @i18n = @i18n_lg["default"]
end

#terms(docxml) ⇒ Object



35
36
37
38
39
# File 'lib/isodoc/iec/presentation_terms.rb', line 35

def terms(docxml)
  otherlang_designations(docxml)
  super
  merge_fr_into_en_term(docxml)
end

#termsource1(node) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/isodoc/iec/presentation_terms.rb', line 145

def termsource1(node)
  lg = node&.at("./ancestor::xmlns:term/@language")&.text
  @i18n = @i18n_lg[lg] if lg && @i18n_lg[lg]
  if @is_iev then termsource1_iev(node)
  else super
  end
  @i18n = @i18n_lg["default"]
end

#termsource1_iev(elem) ⇒ Object



154
155
156
157
158
159
# File 'lib/isodoc/iec/presentation_terms.rb', line 154

def termsource1_iev(elem)
  while elem&.next_element&.name == "termsource"
    elem << "; #{to_xml(elem.next_element.remove.children)}"
  end
  elem.children = l10n("#{@i18n.source}: #{to_xml(elem.children).strip}")
end

#termsource_modification(node) ⇒ Object



138
139
140
141
142
143
# File 'lib/isodoc/iec/presentation_terms.rb', line 138

def termsource_modification(node)
  lg = node&.at("./ancestor::xmlns:term/@language")&.text
  @i18n = @i18n_lg[lg] if lg && @i18n_lg[lg]
  super
  @i18n = @i18n_lg["default"]
end