Class: IsoDoc::Iso::PresentationXMLConvert

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

Instance Method Summary collapse

Methods included from Init

#amd, #clausedelim, #i18n_init, #metadata_init

Constructor Details

#initialize(options) ⇒ PresentationXMLConvert

Returns a new instance of PresentationXMLConvert.



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

def initialize(options)
  super
end

Instance Method Details

#add_idObject



4
5
6
# File 'lib/isodoc/iso/index.rb', line 4

def add_id
  %(id="_#{UUIDTools::UUID.random_create}")
end

#also_lblObject



85
86
87
# File 'lib/isodoc/iso/index.rb', line 85

def also_lbl
  @lang == "en" ? @i18n.see_also : "<em>#{@i18n.see_also}</em>"
end

#annex(isoxml) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 20

def annex(isoxml)
  amd(isoxml) and @suppressheadingnumbers = @oldsuppressheadingnumbers
  super
  isoxml.xpath(ns("//annex//clause | //annex//appendix")).each do |f|
    clause1(f)
  end
  amd(isoxml) and @suppressheadingnumbers = true
end

#clause(docxml) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 96

def clause(docxml)
  docxml.xpath(ns("//clause[not(ancestor::annex)] | "\
                  "//terms | //definitions | //references | "\
                  "//preface/introduction[clause]")).
  each do |f|
    clause1(f)
  end
end

#clause1(f) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 88

def clause1(f)
  if !f.at(ns("./title")) &&
      !%w(sections preface bibliography).include?(f.parent.name)
    f["inline-header"] = "true"
  end
  super
end

#convert1(docxml, filename, dir) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 12

def convert1(docxml, filename, dir)
  if amd(docxml)
    @oldsuppressheadingnumbers = @suppressheadingnumbers
    @suppressheadingnumbers = true
  end
  super
end

#eref_localities1(target, type, from, to, delim, lang = "en") ⇒ Object



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

def eref_localities1(target, type, from, to, delim, lang = "en")
  return "" if type == "anchor"
  subsection = from&.text&.match(/\./)
  type = type.downcase
  lang == "zh" and
    return l10n(eref_localities1_zh(target, type, from, to, delim))
  ret = (delim == ";") ? ";" : (type == "list") ? "" : delim
  loc = @i18n.locality[type] || type.sub(/^locality:/, "").capitalize
  ret += " #{loc}" unless subsection && type == "clause" ||
    type == "list" || target.match(/^IEV$|^IEC 60050-/)
  ret += " #{from.text}" if from
  ret += "&ndash;#{to.text}" if to
  ret += ")" if type == "list"
  l10n(ret)
end

#eref_localities1_zh(target, type, from, to, delim) ⇒ Object



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

def eref_localities1_zh(target, type, from, to, delim)
  subsection = from&.text&.match(/\./)
  ret = (delim == ";") ? ";" : (type == "list") ? "" : delim
  ret += "#{from.text}" if from
  ret += "&ndash;#{to.text}" if to
  loc = (@i18n.locality[type] || type.sub(/^locality:/, "").capitalize )
  ret += " #{loc}" unless subsection && type == "clause" ||
    type == "list" || target.match(/^IEV$|^IEC 60050-/)
  ret += ")" if type == "list"
  ret
end

#example1(f) ⇒ Object



41
42
43
44
45
46
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 41

def example1(f)
  n = @xrefs.get[f["id"]]
  lbl = (n.nil? || n[:label].nil? || n[:label].empty?) ? @i18n.example :
    l10n("#{@i18n.example} #{n[:label]}")
  prefix_name(f, "&nbsp;&mdash; ", lbl, "name")
end

#example_span_label(node, div, name) ⇒ Object



81
82
83
84
85
86
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 81

def example_span_label(node, div, name)
  return if name.nil?
  div.span **{ class: "example_label" } do |p|
    name.children.each { |n| parse(n, div) }
  end
end

#extract_indexsee(v, terms, label) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/isodoc/iso/index.rb', line 99

def extract_indexsee(v, terms, label)
  terms.each_with_object(v) do |t, v|
    term = t&.at(ns("./primary"))&.children&.to_xml
    term2 = t&.at(ns("./secondary"))&.children&.to_xml
    term3 = t&.at(ns("./tertiary"))&.children&.to_xml
    v[term] ||= {}
    v[term][term2] ||= {}
    v[term][term2][term3] ||= {}
    v[term][term2][term3][label] ||= []
    v[term][term2][term3][label] << t&.at(ns("./target"))&.children&.to_xml
    t.remove
  end
end

#extract_indexterms(terms) ⇒ Object

attributes are decoded into UTF-8, elements in extract_indexsee are still in entities



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/isodoc/iso/index.rb', line 118

def extract_indexterms(terms)
  terms.each_with_object({}) do |t, v|
    term = t&.at(ns("./primary"))&.children&.to_xml
    term2 = t&.at(ns("./secondary"))&.children&.to_xml
    term3 = t&.at(ns("./tertiary"))&.children&.to_xml
    index2bookmark(t)
    v[term] ||= {}
    v[term][term2] ||= {}
    v[term][term2][term3] ||= {}
    v[term][term2][term3][:xref] ||= []
    to = t["to"] ? "to='#{t['to']}' " : ""
    v[term][term2][term3][:xref] << "<xref target='#{t['id']}' #{to}pagenumber='true'/>"
  end
end

#figure1(f) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 33

def figure1(f)
  return if labelled_ancestor(f) && f.ancestors("figure").empty?
  lbl = @xrefs.anchor(f['id'], :label, false) or return
  figname = f.parent.name == "figure" ? "" : "#{@i18n.figure} "
  connective = f.parent.name == "figure" ? "&nbsp; " : "&nbsp;&mdash; "
  prefix_name(f, connective, l10n("#{figname}#{lbl}"), "name")
end

#index(docxml) ⇒ Object



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

def index(docxml)
  unless docxml.at(ns("//index"))
    docxml.xpath(ns("//indexsect")).each { |i| i.remove }
    return
  end
  i = docxml.at(ns("//indexsect")) ||
    docxml.root.add_child("<indexsect #{add_id}><title>#{@i18n.index}</title></indexsect>").first
  index = sort_indexterms(docxml.xpath(ns("//index")), docxml.xpath(ns("//index-xref[@also = 'false']")),
                          docxml.xpath(ns("//index-xref[@also = 'true']")))
  index1(docxml, i, index)
end

#index1(docxml, i, index) ⇒ Object



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

def index1(docxml, i, index)
  c = i.add_child("<ul></ul>").first
  index.keys.sort.each do |k|
    #c = i.add_child "<clause #{add_id}><title>#{k}</title><ul></ul></clause>"
    words = index[k].keys.each_with_object({}) { |w, v| v[sortable(w).downcase] = w }
    words.keys.localize(@lang.to_sym).sort.to_a.each do |w|
      #c.first.at(ns("./ul")).add_child index_entries(words, index[k], w)
      c.add_child index_entries(words, index[k], w)
    end
  end
  docxml.xpath(ns("//indexsect//xref")).each { |x| x.children.remove }
  @xrefs.bookmark_anchor_names(docxml)
end

#index2bookmark(t) ⇒ Object



133
134
135
136
137
# File 'lib/isodoc/iso/index.rb', line 133

def index2bookmark(t)
  t.name = "bookmark"
  t.children.each { |x| x.remove }
  t["id"] = "_#{UUIDTools::UUID.random_create}"
end

#index_entries(words, index, primary) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/isodoc/iso/index.rb', line 42

def index_entries(words, index, primary)
  ret = index_entries_head(words[primary], index.dig(words[primary], nil, nil), index_entries_opt)
  words2 = index[words[primary]]&.keys&.reject { |k| k.nil?}&.each_with_object({}) { |w, v| v[w.downcase] = w }
  unless words2.empty?
    ret += "<ul>"
    words2.keys.localize(@lang.to_sym).sort.to_a.each do |w|
      ret += index_entries2(words2, index[words[primary]], w)
    end
    ret += "</ul>"
  end
  ret + "</li>"
end

#index_entries2(words, index, secondary) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/isodoc/iso/index.rb', line 55

def index_entries2(words, index, secondary)
  ret = index_entries_head(words[secondary], index.dig(words[secondary], nil), index_entries_opt)
  words3 = index[words[secondary]]&.keys&.reject { |k| k.nil?}&.each_with_object({}) { |w, v| v[w.downcase] = w }
  unless words3.empty?
    ret += "<ul>"
    words3.keys.localize(@lang.to_sym).sort.to_a.each do |w|
      ret += (index_entries_head(words3[w], index[words[secondary]][words3[w]], index_entries_opt) + "</li>")
    end
    ret += "</ul>"
  end
  ret + "</li>"
end

#index_entries_head(head, entries, opt) ⇒ Object



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

def index_entries_head(head, entries, opt)
  ret = "<li>#{head}"
  xref = entries&.dig(:xref)&.join(", ")
  see_sort = entries&.dig(:see)&.each_with_object({}) { |w, v| v[sortable(w).downcase] = w }
  see = see_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map { |k| see_sort[k] }&.join(", ")
  also_sort = entries&.dig(:also)&.each_with_object({}) { |w, v| v[sortable(w).downcase] = w }
  also = also_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map { |k| also_sort[k] }&.join(", ")
  ret += "#{opt[:xref_lbl]} #{xref}" if xref
  ret += "#{opt[:see_lbl]} #{see}" if see
  ret += "#{opt[:also_lbl]} #{also}" if also
  ret
end

#index_entries_optObject



38
39
40
# File 'lib/isodoc/iso/index.rb', line 38

def index_entries_opt
  { xref_lbl: ", ", see_lbl: ", #{see_lbl}", also_lbl: ", #{also_lbl}" }
end

#prefix_container(container, linkend, target) ⇒ Object



76
77
78
79
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 76

def prefix_container(container, linkend, target)
  delim = @xrefs.anchor(target, :type) == "listitem" ? " " : ", "
  l10n(@xrefs.anchor(container, :xref) + delim + linkend)
end

#see_lblObject



81
82
83
# File 'lib/isodoc/iso/index.rb', line 81

def see_lbl
  @lang == "en" ? @i18n.see : "<em>#{@i18n.see}</em>"
end

#sort_indexterms(terms, see, also) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/isodoc/iso/index.rb', line 89

def sort_indexterms(terms, see, also)
  index = extract_indexterms(terms)
  index = extract_indexsee(index, see, :see)
  index = extract_indexsee(index, also, :also)
  index.keys.sort.each_with_object({}) do |k, v|
    v[sortable(k)[0].upcase.transliterate] ||= {}
    v[sortable(k)[0].upcase.transliterate][k] = index[k]
  end
end

#sortable(s) ⇒ Object



34
35
36
# File 'lib/isodoc/iso/index.rb', line 34

def sortable(s)
  HTMLEntities.new.decode(Nokogiri::XML.fragment(s).text)
end

#xml_encode_attr(s) ⇒ Object



113
114
115
# File 'lib/isodoc/iso/index.rb', line 113

def xml_encode_attr(s)
  HTMLEntities.new.encode(s, :basic, :hexadecimal).gsub(/\&#x([^;]+);/) { |x| "&#x#{$1.upcase};" }
end

#xref_init(lang, script, klass, labels, options) ⇒ Object



29
30
31
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 29

def xref_init(lang, script, klass, labels, options)
  @xrefs = Xref.new(lang, script, klass, labels, options)
end