metanorma-iso 2.9.2 → 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,173 +0,0 @@
1
- module IsoDoc
2
- module Iso
3
- class PresentationXMLConvert < IsoDoc::PresentationXMLConvert
4
- def add_id
5
- %(id="_#{UUIDTools::UUID.random_create}")
6
- end
7
-
8
- def index(xml)
9
- if xml.at(ns("//index"))
10
- i = xml.at(ns("//indexsect")) ||
11
- xml.root.add_child("<indexsect #{add_id}><title>#{@i18n.index}" \
12
- "</title></indexsect>").first
13
- index = sort_indexterms(xml.xpath(ns("//index")),
14
- xml.xpath(ns("//index-xref[@also = 'false']")),
15
- xml.xpath(ns("//index-xref[@also = 'true']")))
16
- index1(xml, i, index)
17
- else xml.xpath(ns("//indexsect")).each(&:remove)
18
- end
19
- end
20
-
21
- def index1(docxml, indexsect, index)
22
- c = indexsect.add_child("<ul></ul>").first
23
- index.keys.sort.each do |k|
24
- words = index[k].keys.each_with_object({}) do |w, v|
25
- v[sortable(w).downcase] = w
26
- end
27
- words.keys.localize(@lang.to_sym).sort.to_a.each do |w|
28
- c.add_child index_entries(words, index[k], w)
29
- end
30
- end
31
- index1_cleanup(docxml)
32
- end
33
-
34
- def index1_cleanup(docxml)
35
- docxml.xpath(ns("//indexsect//xref")).each do |x|
36
- x.children.remove
37
- end
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&.compact
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]],
76
- index_entries_opt) + "</li>")
77
- end
78
- ret += "</ul>"
79
- end
80
- "#{ret}</li>"
81
- end
82
-
83
- def index_entries_head(head, entries, opt)
84
- ret = "<li>#{head}"
85
- xref = entries&.dig(:xref)&.join(", ")
86
- see = index_entries_see(entries, :see)
87
- also = index_entries_see(entries, :also)
88
- ret += "#{opt[:xref_lbl]} #{xref}" if xref
89
- ret += "#{opt[:see_lbl]} #{see}" if see
90
- ret += "#{opt[:also_lbl]} #{also}" if also
91
- ret
92
- end
93
-
94
- def index_entries_see(entries, label)
95
- see_sort = entries&.dig(label) or return nil
96
-
97
- x = see_sort.each_with_object({}) do |w, v|
98
- v[sortable(w).downcase] = w
99
- end
100
- x.keys.localize(@lang.to_sym).sort.to_a.map do |k|
101
- # see_sort[k]
102
- x[k]
103
- end.join(", ")
104
- end
105
-
106
- def see_lbl
107
- @lang == "en" ? @i18n.see : "<em>#{@i18n.see}</em>"
108
- end
109
-
110
- def also_lbl
111
- @lang == "en" ? @i18n.see_also : "<em>#{@i18n.see_also}</em>"
112
- end
113
-
114
- def sort_indexterms(terms, see, also)
115
- index = extract_indexterms(terms)
116
- index = extract_indexsee(index, see, :see)
117
- index = extract_indexsee(index, also, :also)
118
- index.keys.sort.each_with_object({}) do |k, v|
119
- v[sortable(k)[0].upcase.transliterate] ||= {}
120
- v[sortable(k)[0].upcase.transliterate][k] = index[k]
121
- end
122
- end
123
-
124
- def extract_indexsee(val, terms, label)
125
- terms.each_with_object(val) do |t, v|
126
- term, term2, term3 = extract_indexterms_init(t)
127
- term_hash_init(v, term, term2, term3, label)
128
- v[term][term2][term3][label] << to_xml(t.at(ns("./target"))&.children)
129
- t.remove
130
- end
131
- end
132
-
133
- def xml_encode_attr(str)
134
- HTMLEntities.new.encode(str, :basic, :hexadecimal)
135
- .gsub(/&#x([^;]+);/) do |_x|
136
- "&#x#{$1.upcase};"
137
- end
138
- end
139
-
140
- # attributes are decoded into UTF-8,
141
- # elements in extract_indexsee are still in entities
142
- def extract_indexterms(terms)
143
- terms.each_with_object({}) do |t, v|
144
- term, term2, term3 = extract_indexterms_init(t)
145
- index2bookmark(t)
146
- term_hash_init(v, term, term2, term3, :xref)
147
- to = t["to"] ? "to='#{t['to']}' " : ""
148
- v[term][term2][term3][:xref] << "<xref target='#{t['id']}' " \
149
- "#{to}pagenumber='true'/>"
150
- end
151
- end
152
-
153
- def extract_indexterms_init(term)
154
- %w(primary secondary tertiary).each_with_object([]) do |x, m|
155
- m << to_xml(term.at(ns("./#{x}"))&.children)
156
- end
157
- end
158
-
159
- def term_hash_init(hash, term, term2, term3, label)
160
- hash[term] ||= {}
161
- hash[term][term2] ||= {}
162
- hash[term][term2][term3] ||= {}
163
- hash[term][term2][term3][label] ||= []
164
- end
165
-
166
- def index2bookmark(node)
167
- node.name = "bookmark"
168
- node.children.each(&:remove)
169
- node["id"] = "_#{UUIDTools::UUID.random_create}"
170
- end
171
- end
172
- end
173
- end