/
metadata.rb
188 lines (157 loc) · 5.53 KB
/
metadata.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require_relative "metadata_date"
require_relative "metadata_contributor"
module IsoDoc
class Metadata
attr_accessor :fonts_options, :labels
def ns(xpath)
Common::ns(xpath)
end
def l10n(expr, lang = @lang, script = @script, locale = @locale)
@i18n.l10n(expr, lang, script, locale)
end
def initialize(lang, script, locale, i18n, fonts_options = {})
@metadata = { lang: lang, script: script }
DATETYPES.each { |w| @metadata["#{w.gsub('-', '_')}date".to_sym] = "XXX" }
@lang = lang
@script = script
@locale = locale
@c = HTMLEntities.new
@i18n = i18n
@labels = @i18n.get
@fonts_options = fonts_options
end
def get
@metadata
end
def labels
@labels
end
def set(key, value)
@metadata[key] = value
end
NOLANG = "[not(@language) or @language = '']".freeze
def currlang
"[@language = '#{@lang}']"
end
def doctype(isoxml, _out)
b = isoxml&.at(ns("//bibdata/ext/doctype#{NOLANG}"))&.text || return
set(:doctype, status_print(b))
b1 = isoxml&.at(ns("//bibdata/ext/doctype#{currlang}"))&.text || b
set(:doctype_display, status_print(b1))
b = isoxml&.at(ns("//bibdata/ext/subdoctype#{NOLANG}"))&.text || return
set(:subdoctype, status_print(b))
end
def docstatus(xml, _out)
set(:unpublished, true)
return unless s = xml.at(ns("//bibdata/status/stage#{NOLANG}"))
s1 = xml.at(ns("//bibdata/status/stage#{currlang}")) || s
set(:stage, status_print(s.text))
s1 and set(:stage_display, status_print(s1.text))
(i = xml&.at(ns("//bibdata/status/substage#{NOLANG}"))&.text) and
set(:substage, i)
(i1 = xml&.at(ns("//bibdata/status/substage#{currlang}"))&.text || i) and
set(:substage_display, i1)
(i2 = xml&.at(ns("//bibdata/status/iteration"))&.text) and
set(:iteration, i2)
set(:unpublished, unpublished(s.text))
unpublished(s.text) && set(:stageabbr, stage_abbr(s.text))
end
def stage_abbr(docstatus)
status_print(docstatus).split(/ /).map { |s| s[0].upcase }.join
end
def unpublished(status)
!status.casecmp("published").zero?
end
def status_print(status)
status.split(/[- ]/).map(&:capitalize).join(" ")
end
def docid(isoxml, _out)
dn = isoxml.at(ns("//bibdata/docidentifier"))
set(:docnumber, dn&.text)
end
def otherid(isoxml, _out)
dn = isoxml.at(ns('//bibdata/docidentifier[@type = "ISBN"]'))
set(:isbn, dn&.text)
dn = isoxml.at(ns('//bibdata/docidentifier[@type = "ISBN10"]'))
set(:isbn10, dn&.text)
end
def docnumeric(isoxml, _out)
dn = isoxml.at(ns("//bibdata/docnumber"))
set(:docnumeric, dn&.text)
end
def draftinfo(draft, revdate)
return "" unless draft
draftinfo = " (#{@labels['draft_label']} #{draft}"
draftinfo += ", #{revdate}" if revdate
draftinfo += ")"
l10n(draftinfo, @lang, @script, @locale)
end
def version(isoxml, _out)
set(:edition, isoxml.at(ns("//bibdata/edition#{NOLANG}"))&.text)
set(:edition_display,
isoxml.at(ns("//bibdata/edition#{currlang}"))&.text)
set(:docyear, isoxml.at(ns("//bibdata/copyright/from"))&.text)
set(:draft, isoxml.at(ns("//bibdata/version/draft"))&.text)
revdate = isoxml.at(ns("//bibdata/version/revision-date"))&.text
set(:revdate, revdate)
set(:revdate_monthyear, monthyr(revdate))
set(:draftinfo,
draftinfo(get[:draft], get[:revdate]))
end
def title(isoxml, _out)
main = isoxml.at(ns("//bibdata/title[@language='#{@lang}']"))
&.children&.to_xml
set(:doctitle, main)
end
def subtitle(_isoxml, _out)
nil
end
def relations(isoxml, _out)
relations_obsoletes(isoxml)
relations_partof(isoxml)
end
def relations_partof(isoxml)
std = isoxml.at(ns("//bibdata/relation[@type = 'partOf']")) || return
id = std.at(ns(".//docidentifier"))
set(:partof, id.text) if id
end
def relations_obsoletes(isoxml)
std = isoxml.at(ns("//bibdata/relation[@type = 'obsoletes']")) || return
locality = std.at(ns(".//locality"))
id = std.at(ns(".//docidentifier"))
set(:obsoletes, id.text) if id
set(:obsoletes_part, locality.text) if locality
end
def url(xml, _out)
(a = xml.at(ns("//bibdata/uri[not(@type)]"))) && set(:url, a.text)
(a = xml.at(ns("//bibdata/uri[@type = 'html']"))) && set(:html, a.text)
(a = xml.at(ns("//bibdata/uri[@type = 'xml']"))) && set(:xml, a.text)
(a = xml.at(ns("//bibdata/uri[@type = 'pdf']"))) && set(:pdf, a.text)
(a = xml.at(ns("//bibdata/uri[@type = 'doc']"))) && set(:doc, a.text)
end
def keywords(isoxml, _out)
ret = []
isoxml.xpath(ns("//bibdata/keyword")).each { |kw| ret << kw.text }
set(:keywords, ret)
end
def note(isoxml, _out)
ret = []
isoxml.xpath(ns("//bibdata/note[@type = 'title-footnote']")).each do |n|
ret << n.text
end
set(:title_footnote, ret)
end
def code_css(isoxml, _out)
c = isoxml.at(ns("//metanorma-extension/source-highlighter-css")) or return
set(:code_css, c.text)
end
def presentation(xml, _out)
xml.xpath(ns("//metanorma-extension/presentation-metadata")).each do |p|
((n = p.at(ns("./name"))) && (v = p.at(ns("./value")))) or next
lbl = "presentation_metadata_#{n.text}".to_sym
m = get[lbl] || []
set(lbl, m << v.text)
end
end
end
end