Class: IsoDoc::Metadata
- Inherits:
-
Object
show all
- Includes:
- ClassUtils
- Defined in:
- lib/isodoc/metadata.rb,
lib/isodoc/metadata_date.rb,
lib/isodoc/metadata_contributor.rb
Constant Summary
collapse
- NOLANG =
"[not(@language) or @language = '']".freeze
- DATETYPES =
%w{published accessed created implemented obsoleted confirmed updated
corrected issued received transmitted copied unchanged circulated
adapted announced vote-started vote-ended stable-until}.freeze
- AGENCY_DATA =
{ subdivision: "./subdivision",
pub_phone: "./phone[not(@type = 'fax')]",
abbr: "./abbreviation",
logo: "./logo/image/@src",
pub_fax: "./phone[@type = 'fax']", pub_email: "./email",
pub_uri: "./uri" }.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#agency(xml) ⇒ Object
-
#agency1(xml) ⇒ Object
-
#agency_addr(xml) ⇒ Object
-
#agency_data(xml) ⇒ Object
-
#agency_data1(org) ⇒ Object
-
#author(xml, _out) ⇒ Object
-
#bibdate(isoxml, _out) ⇒ Object
-
#code_css(isoxml, _out) ⇒ Object
-
#connectives_strip(text) ⇒ Object
-
#contrib_logo_attr?(elem, role) ⇒ Boolean
-
#contrib_logo_attrs(xml, role) ⇒ Object
-
#currlang ⇒ Object
-
#docid(isoxml, _out) ⇒ Object
-
#docnumeric(isoxml, _out) ⇒ Object
-
#docstatus(xml, _out) ⇒ Object
-
#doctype(isoxml, _out) ⇒ Object
-
#draftinfo(draft, revdate) ⇒ Object
-
#extract_person_affiliations(authors) ⇒ Object
-
#extract_person_name_from_components(person) ⇒ Object
-
#extract_person_names(authors) ⇒ Object
-
#extract_person_names_affiliations(authors) ⇒ Object
-
#get ⇒ Object
-
#initialize(lang, script, locale, i18n, fonts_options = {}) ⇒ Metadata
constructor
A new instance of Metadata.
-
#iso?(org) ⇒ Boolean
-
#keywords(isoxml, _out) ⇒ Object
-
#l10n(expr, lang = @lang, script = @script, locale = @locale) ⇒ Object
-
#MMMddyyyy(isodate) ⇒ Object
-
#months ⇒ Object
-
#monthyr(isodate) ⇒ Object
-
#note(isoxml, _out) ⇒ Object
-
#ns(xpath) ⇒ Object
-
#otherid(isoxml, _out) ⇒ Object
-
#personal_authors(isoxml) ⇒ Object
-
#presentation(xml, _out) ⇒ Object
-
#published_default(xml) ⇒ Object
-
#relations(isoxml, _out) ⇒ Object
-
#relations_obsoletes(isoxml) ⇒ Object
-
#relations_partof(isoxml) ⇒ Object
-
#set(key, value) ⇒ Object
-
#stage_abbr(docstatus) ⇒ Object
-
#status_print(status) ⇒ Object
-
#subtitle(_isoxml, _out) ⇒ Object
-
#title(isoxml, _out) ⇒ Object
-
#to_datauri(src) ⇒ Object
-
#unpublishedx(status) ⇒ Object
-
#url(xml, _out) ⇒ Object
-
#version(isoxml, _out) ⇒ Object
Methods included from ClassUtils
#case_strict, #case_strict1, #case_with_markup, #date_range, #liquid, #nearest_block_parent, #start_of_sentence, #to_xml
Constructor Details
#initialize(lang, script, locale, i18n, fonts_options = {}) ⇒ Metadata
Returns a new instance of Metadata.
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/isodoc/metadata.rb', line 25
def initialize(lang, script, locale, i18n, fonts_options = {})
@metadata = { lang:, script: }
DATETYPES.each { |w| @metadata["#{w.tr('-', '_')}date".to_sym] = "XXX" }
@lang = lang
@script = script
@locale = locale
@c = HTMLEntities.new
@i18n = i18n
@labels = @i18n.get
@fonts_options = fonts_options
end
|
Instance Attribute Details
#fonts_options ⇒ Object
Returns the value of attribute fonts_options.
6
7
8
|
# File 'lib/isodoc/metadata.rb', line 6
def fonts_options
@fonts_options
end
|
Returns the value of attribute labels.
6
7
8
|
# File 'lib/isodoc/metadata.rb', line 6
def labels
@labels
end
|
Returns the value of attribute localdir.
6
7
8
|
# File 'lib/isodoc/metadata.rb', line 6
def localdir
@localdir
end
|
Instance Method Details
#agency(xml) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/isodoc/metadata_contributor.rb', line 105
def agency(xml)
agency, publisher, logos = agency1(xml)
set(:agency, agency)
set(:publisher, connectives_strip(@i18n.boolean_conj(publisher, "and")))
set(:copublisher_logos, logos.map { |l| to_datauri(l) })
set(:copublisher_logo_attrs, contrib_logo_attrs(xml, "publisher"))
agency_addr(xml)
end
|
#agency1(xml) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/isodoc/metadata_contributor.rb', line 63
def agency1(xml)
agency = ""
publisher = []
logos = []
agency_data(xml).each do |org|
agency1 = org[:abbr] || org[:name]
org[:name] and publisher << org[:name]
org[:logo] and logos << org[:logo]
agency = iso?(org) ? "ISO/#{agency}" : "#{agency}#{agency1}/"
end
[agency.sub(%r{/$}, ""), publisher, logos]
end
|
#agency_addr(xml) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/isodoc/metadata_contributor.rb', line 134
def agency_addr(xml)
a = xml.at(ns("//bibdata/contributor[xmlns:role/@type = 'publisher'][1]/" \
"organization")) or return
data = agency_data1(a)
data.compact.each do |k, v|
%i(name abbr logo).include?(k) and next
set(k, v)
end
n = a.at(ns("./address/formattedAddress")) and
set(:pub_address, to_xml(n.children))
end
|
#agency_data(xml) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/isodoc/metadata_contributor.rb', line 83
def agency_data(xml)
xml.xpath(ns("//bibdata/contributor[xmlns:role/@type = 'publisher']/" \
"organization")).each_with_object([]) do |org, m|
m << agency_data1(org)
end
end
|
#agency_data1(org) ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/isodoc/metadata_contributor.rb', line 90
def agency_data1(org)
ret = {}
n = org.at(ns("./name[@language = '#{@lang}']")) ||
org.at(ns("./name")) and ret[:name] = n.text
AGENCY_DATA.each do |k, v|
n = org.at(ns(v)) and ret[k] = n.text
end
ret
end
|
#author(xml, _out) ⇒ Object
53
54
55
56
|
# File 'lib/isodoc/metadata_contributor.rb', line 53
def author(xml, _out)
personal_authors(xml)
agency(xml)
end
|
#bibdate(isoxml, _out) ⇒ Object
43
44
45
46
47
|
# File 'lib/isodoc/metadata_date.rb', line 43
def bibdate(isoxml, _out)
isoxml.xpath(ns("//bibdata/date")).each do |d|
set("#{d['type'].tr('-', '_')}date".to_sym, Common::date_range(d))
end
end
|
#code_css(isoxml, _out) ⇒ Object
189
190
191
192
|
# File 'lib/isodoc/metadata.rb', line 189
def code_css(isoxml, _out)
c = isoxml.at(ns("//metanorma-extension/source-highlighter-css")) or return
set(:code_css, c.text)
end
|
#connectives_strip(text) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/isodoc/metadata.rb', line 16
def connectives_strip(text)
text.gsub("<conn>", "")
.gsub("</conn>", "")
.gsub("<enum-comma>", "")
.gsub("</enum-comma>", "")
.gsub("<comma>", "")
.gsub("</comma>", "")
end
|
#contrib_logo_attr?(elem, role) ⇒ Boolean
126
127
128
129
130
131
132
|
# File 'lib/isodoc/metadata_contributor.rb', line 126
def contrib_logo_attr?(elem, role)
elem.name.start_with?("logo-#{role}-") or return false
p = elem.name.split(/[_-]/)
%w(doc html).include?(p[2]) &&
%w(height width).include?(p[3]) &&
(!p[4] || /^\d+/.match?(p[4]))
end
|
#contrib_logo_attrs(xml, role) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/isodoc/metadata_contributor.rb', line 114
def contrib_logo_attrs(xml, role)
xml.xpath(ns("//metanorma-extension/presentation-metadata/*"))
.each_with_object([]) do |x, m|
contrib_logo_attr?(x, role) or next
p = x.name.split(/[_-]/)
idx = (p[4] || "1").to_i - 1
m[idx] ||= {}
m[idx][p[2]] ||= {}
m[idx][p[2]][p[3]] = x.text
end
end
|
47
48
49
|
# File 'lib/isodoc/metadata.rb', line 47
def currlang
"[@language = '#{@lang}']"
end
|
#docid(isoxml, _out) ⇒ Object
100
101
102
103
|
# File 'lib/isodoc/metadata.rb', line 100
def docid(isoxml, _out)
dn = isoxml.at(ns("//bibdata/docidentifier"))
set(:docnumber, dn&.text)
end
|
#docnumeric(isoxml, _out) ⇒ Object
112
113
114
115
|
# File 'lib/isodoc/metadata.rb', line 112
def docnumeric(isoxml, _out)
dn = isoxml.at(ns("//bibdata/docnumber"))
set(:docnumeric, dn&.text)
end
|
#docstatus(xml, _out) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/isodoc/metadata.rb', line 60
def docstatus(xml, _out)
published = published_default(xml)
s = xml.at(ns("//bibdata/status/stage#{NOLANG}")) or return
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)
!published && set(:stageabbr, stage_abbr(s.text))
end
|
#doctype(isoxml, _out) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/isodoc/metadata.rb', line 51
def doctype(isoxml, _out)
b = isoxml.at(ns("//bibdata/ext/doctype#{NOLANG}")) || return
set(:doctype, status_print(b.text))
b1 = isoxml.at(ns("//bibdata/ext/doctype#{currlang}")) || b
set(:doctype_display, status_print(b1.text))
b = isoxml.at(ns("//bibdata/ext/subdoctype#{NOLANG}")) || return
set(:subdoctype, status_print(b.text))
end
|
#draftinfo(draft, revdate) ⇒ Object
117
118
119
120
121
122
123
124
|
# File 'lib/isodoc/metadata.rb', line 117
def draftinfo(draft, revdate)
return "" unless draft
draftinfo = " (#{@labels['draft_label']} #{draft}"
draftinfo += ", #{revdate}" if revdate
draftinfo += ")"
l10n(draftinfo, @lang, @script, @locale)
end
|
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/isodoc/metadata_contributor.rb', line 21
def (authors)
authors.reduce([]) do |m, a|
pos = a.at(ns("./affiliation/name"))&.text
name = a.at(ns("./affiliation/organization/name"))&.text
subdivs = a.xpath(ns("./affiliation/organization/subdivision"))
&.map(&:text)&.join(", ")
location =
a.at(ns("./affiliation/organization/address/formattedAddress"))&.text
m << l10n([pos, name, subdivs, location].map { |x| x&.empty? ? nil : x }
.compact.join(", "))
m
end
end
|
13
14
15
16
17
18
19
|
# File 'lib/isodoc/metadata_contributor.rb', line 13
def (person)
name = person.xpath(ns("./name/forename"))
name.empty? and name = person.xpath(ns("./name/formatted-initials"))
out = name.map(&:text)
out << person.at(ns("./name/surname"))&.text
l10n(out.compact.join(" "))
end
|
3
4
5
6
7
8
9
10
11
|
# File 'lib/isodoc/metadata_contributor.rb', line 3
def (authors)
authors.reduce([]) do |ret, a|
ret << if a.at(ns("./name/completename"))
a.at(ns("./name/completename")).text
else
(a)
end
end
end
|
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/isodoc/metadata_contributor.rb', line 35
def (authors)
names = (authors)
affils = (authors)
ret = {}
affils.each_with_index do |a, i|
ret[a] ||= []
ret[a] << names[i]
end
ret
end
|
37
38
39
|
# File 'lib/isodoc/metadata.rb', line 37
def get
@metadata
end
|
#iso?(org) ⇒ Boolean
58
59
60
61
|
# File 'lib/isodoc/metadata_contributor.rb', line 58
def iso?(org)
org[:abbr] == "ISO" ||
org[:name] == "International Organization for Standardization"
end
|
#keywords(isoxml, _out) ⇒ Object
175
176
177
178
179
|
# File 'lib/isodoc/metadata.rb', line 175
def keywords(isoxml, _out)
ret = []
isoxml.xpath(ns("//bibdata/keyword")).each { |kw| ret << kw.text }
set(:keywords, ret)
end
|
#l10n(expr, lang = @lang, script = @script, locale = @locale) ⇒ Object
12
13
14
|
# File 'lib/isodoc/metadata.rb', line 12
def l10n(expr, lang = @lang, script = @script, locale = @locale)
@i18n.l10n(expr, lang, script, locale)
end
|
#MMMddyyyy(isodate) ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/isodoc/metadata_date.rb', line 32
def MMMddyyyy(isodate)
isodate.nil? and return nil
arr = isodate.split("-")
arr.size == 1 && (/^\d+$/.match isodate) and
return Date.new(*arr.map(&:to_i)).strftime("%Y")
arr.size == 2 and
return Date.new(*arr.map(&:to_i)).strftime("%B %Y")
Date.parse(isodate).strftime("%B %d, %Y")
end
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/isodoc/metadata_date.rb', line 8
def months
{
"01": @labels["month_january"],
"02": @labels["month_february"],
"03": @labels["month_march"],
"04": @labels["month_april"],
"05": @labels["month_may"],
"06": @labels["month_june"],
"07": @labels["month_july"],
"08": @labels["month_august"],
"09": @labels["month_september"],
"10": @labels["month_october"],
"11": @labels["month_november"],
"12": @labels["month_december"],
}
end
|
#monthyr(isodate) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/isodoc/metadata_date.rb', line 25
def monthyr(isodate)
m = /(?<yr>\d\d\d\d)-(?<mo>\d\d)/.match isodate
return isodate unless m && m[:yr] && m[:mo]
l10n("#{months[m[:mo].to_sym]} #{m[:yr]}")
end
|
#note(isoxml, _out) ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/isodoc/metadata.rb', line 181
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
|
#ns(xpath) ⇒ Object
8
9
10
|
# File 'lib/isodoc/metadata.rb', line 8
def ns(xpath)
Common::ns(xpath)
end
|
#otherid(isoxml, _out) ⇒ Object
105
106
107
108
109
110
|
# File 'lib/isodoc/metadata.rb', line 105
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
|
#personal_authors(isoxml) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/isodoc/metadata_contributor.rb', line 46
def personal_authors(isoxml)
authors = isoxml.xpath(ns("//bibdata/contributor[role/@type = 'author' " \
"or xmlns:role/@type = 'editor']/person"))
set(:authors, (authors))
set(:authors_affiliations, (authors))
end
|
#presentation(xml, _out) ⇒ Object
194
195
196
197
198
199
200
201
|
# File 'lib/isodoc/metadata.rb', line 194
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
|
#published_default(xml) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/isodoc/metadata.rb', line 75
def published_default(xml)
override = xml.at(ns("//semantic-metadata/stage-published"))&.text
default = override || "true"
ret = default == "false"
set(:unpublished, ret)
default == "true"
end
|
#relations(isoxml, _out) ⇒ Object
148
149
150
151
|
# File 'lib/isodoc/metadata.rb', line 148
def relations(isoxml, _out)
relations_obsoletes(isoxml)
relations_partof(isoxml)
end
|
#relations_obsoletes(isoxml) ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/isodoc/metadata.rb', line 159
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
|
#relations_partof(isoxml) ⇒ Object
153
154
155
156
157
|
# File 'lib/isodoc/metadata.rb', line 153
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
|
#set(key, value) ⇒ Object
41
42
43
|
# File 'lib/isodoc/metadata.rb', line 41
def set(key, value)
@metadata[key] = value
end
|
#stage_abbr(docstatus) ⇒ Object
83
84
85
|
# File 'lib/isodoc/metadata.rb', line 83
def stage_abbr(docstatus)
status_print(docstatus).split(/ /).map { |s| s[0].upcase }.join
end
|
#status_print(status) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/isodoc/metadata.rb', line 92
def status_print(status)
status.split(/[- ]/).map do |w|
letters = w.chars
letters.first.upcase!
letters.join
end.join(" ")
end
|
#subtitle(_isoxml, _out) ⇒ Object
144
145
146
|
# File 'lib/isodoc/metadata.rb', line 144
def subtitle(_isoxml, _out)
nil
end
|
#title(isoxml, _out) ⇒ Object
139
140
141
142
|
# File 'lib/isodoc/metadata.rb', line 139
def title(isoxml, _out)
main = isoxml.at(ns("//bibdata/title[@language='#{@lang}']"))
&.children and set(:doctitle, to_xml(main))
end
|
#to_datauri(src) ⇒ Object
100
101
102
103
|
# File 'lib/isodoc/metadata_contributor.rb', line 100
def to_datauri(src)
/^data:/.match?(src) and return src
Vectory::Utils::datauri(src, @localdir)
end
|
#unpublishedx(status) ⇒ Object
88
89
90
|
# File 'lib/isodoc/metadata.rb', line 88
def unpublishedx(status)
!status.casecmp("published").zero?
end
|
#url(xml, _out) ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/isodoc/metadata.rb', line 167
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
|
#version(isoxml, _out) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/isodoc/metadata.rb', line 126
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
|