metanorma-bipm 2.5.8 → 2.5.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,163 +0,0 @@
1
- require "twitter_cldr"
2
- require "sterile"
3
-
4
- module IsoDoc
5
- module Bipm
6
- class PresentationXMLConvert < IsoDoc::Generic::PresentationXMLConvert
7
- def add_id
8
- %(id="_#{UUIDTools::UUID.random_create}")
9
- end
10
-
11
- def index(docxml)
12
- unless docxml.at(ns("//index"))
13
- docxml.xpath(ns("//indexsect")).each(&:remove)
14
- return
15
- end
16
- i = docxml.at(ns("//indexsect")) ||
17
- docxml.root.add_child("<indexsect #{add_id}><title>#{@i18n.index}" \
18
- "</title></indexsect>").first
19
- index = sort_indexterms(
20
- docxml.xpath(ns("//index")),
21
- docxml.xpath(ns("//index-xref[@also = 'false']")),
22
- docxml.xpath(ns("//index-xref[@also = 'true']")),
23
- )
24
- index1(docxml, i, index)
25
- end
26
-
27
- def index1(docxml, i, index)
28
- index.keys.sort.each do |k|
29
- c = i.add_child "<clause #{add_id}><title>#{k}</title><ul></ul></clause>"
30
- words = index[k].keys.each_with_object({}) do |w, v|
31
- v[sortable(w).downcase] = w
32
- end
33
- words.keys.localize(@lang.to_sym).sort.to_a.each do |w|
34
- c.first.at(ns("./ul")).add_child index_entries(words, index[k], w)
35
- end
36
- end
37
- docxml.xpath(ns("//indexsect//xref")).each { |x| x.children.remove }
38
- @xrefs.bookmark_anchor_names(docxml)
39
- end
40
-
41
- def sortable(str)
42
- HTMLEntities.new.decode(Nokogiri::XML.fragment(str).text)
43
- end
44
-
45
- def index_entries_opt
46
- { xref_lbl: ", ", see_lbl: ", #{see_lbl}", also_lbl: ", #{also_lbl}" }
47
- end
48
-
49
- def index_entries(words, index, primary)
50
- ret = index_entries_head(words[primary],
51
- index.dig(words[primary], nil, nil),
52
- index_entries_opt)
53
- words2 = index[words[primary]]&.keys&.compact
54
- &.each_with_object({}) { |w, v| v[w.downcase] = w }
55
- unless words2.empty?
56
- ret += "<ul>"
57
- words2.keys.localize(@lang.to_sym).sort.to_a.each do |w|
58
- ret += index_entries2(words2, index[words[primary]], w)
59
- end
60
- ret += "</ul>"
61
- end
62
- "#{ret}</li>"
63
- end
64
-
65
- def index_entries2(words, index, secondary)
66
- ret = index_entries_head(words[secondary],
67
- index.dig(words[secondary], nil),
68
- index_entries_opt)
69
- words3 = index[words[secondary]]&.keys&.reject(&:nil?)
70
- &.each_with_object({}) { |w, v| v[w.downcase] = w }
71
- unless words3.empty?
72
- ret += "<ul>"
73
- words3.keys.localize(@lang.to_sym).sort.to_a.each do |w|
74
- ret += (index_entries_head(words3[w],
75
- index[words[secondary]][words3[w]], index_entries_opt) + "</li>")
76
- end
77
- ret += "</ul>"
78
- end
79
- "#{ret}</li>"
80
- end
81
-
82
- def index_entries_head(head, entries, opt)
83
- ret = "<li>#{head}"
84
- xref = entries&.dig(:xref)&.join(", ")
85
- see_sort = entries&.dig(:see)&.each_with_object({}) do |w, v|
86
- v[sortable(w).downcase] = w
87
- end
88
- see = see_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map do |k|
89
- see_sort[k]
90
- end&.join(", ")
91
- also_sort = entries&.dig(:also)&.each_with_object({}) do |w, v|
92
- v[sortable(w).downcase] = w
93
- end
94
- also = also_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map do |k|
95
- also_sort[k]
96
- end&.join(", ")
97
- ret += "#{opt[:xref_lbl]} #{xref}" if xref
98
- ret += "#{opt[:see_lbl]} #{see}" if see
99
- ret += "#{opt[:also_lbl]} #{also}" if also
100
- ret
101
- end
102
-
103
- def see_lbl
104
- @lang == "en" ? @i18n.see : "<em>#{@i18n.see}</em>"
105
- end
106
-
107
- def also_lbl
108
- @lang == "en" ? @i18n.see_also : "<em>#{@i18n.see_also}</em>"
109
- end
110
-
111
- def sort_indexterms(terms, see, also)
112
- index = extract_indexterms(terms)
113
- index = extract_indexsee(index, see, :see)
114
- index = extract_indexsee(index, also, :also)
115
- index.keys.sort.each_with_object({}) do |k, v|
116
- v[sortable(k)[0].upcase.transliterate] ||= {}
117
- v[sortable(k)[0].upcase.transliterate][k] = index[k]
118
- end
119
- end
120
-
121
- def extract_indexsee(idx, terms, label)
122
- terms.each_with_object(idx) do |t, v|
123
- term = to_xml(t.at(ns("./primary"))&.children)
124
- term2 = to_xml(t.at(ns("./secondary"))&.children)
125
- term3 = to_xml(t.at(ns("./tertiary"))&.children)
126
- v[term] ||= {}
127
- v[term][term2] ||= {}
128
- v[term][term2][term3] ||= {}
129
- v[term][term2][term3][label] ||= []
130
- v[term][term2][term3][label] << to_xml(t.at(ns("./target"))&.children)
131
- t.remove
132
- end
133
- end
134
-
135
- def xml_encode_attr(str)
136
- HTMLEntities.new.encode(str, :basic, :hexadecimal)
137
- .gsub(/&#x([^;]+);/) { |_x| "&#x#{$1.upcase};" }
138
- end
139
-
140
- # attributes are decoded into UTF-8, elements in extract_indexsee are still in entities
141
- def extract_indexterms(terms)
142
- terms.each_with_object({}) do |t, v|
143
- term = to_xml(t.at(ns("./primary"))&.children)
144
- term2 = to_xml(t.at(ns("./secondary"))&.children)
145
- term3 = to_xml(t.at(ns("./tertiary"))&.children)
146
- index2bookmark(t)
147
- v[term] ||= {}
148
- v[term][term2] ||= {}
149
- v[term][term2][term3] ||= {}
150
- v[term][term2][term3][:xref] ||= []
151
- to = t["to"] ? "to='#{t['to']}' " : ""
152
- v[term][term2][term3][:xref] << "<xref target='#{t['id']}' #{to}pagenumber='true'/>"
153
- end
154
- end
155
-
156
- def index2bookmark(node)
157
- node.name = "bookmark"
158
- node.children.each(&:remove)
159
- node["id"] = "_#{UUIDTools::UUID.random_create}"
160
- end
161
- end
162
- end
163
- end