Class: IsoDoc::Metadata
- Inherits:
-
Object
- Object
- IsoDoc::Metadata
- Defined in:
- lib/isodoc/metadata.rb,
lib/isodoc/metadata_date.rb,
lib/isodoc/metadata_contributor.rb
Constant Summary collapse
- NOLANG =
"[not(@language) or @language = '']"
- DATETYPES =
%w{published accessed created implemented obsoleted confirmed updated issued received transmitted copied unchanged circulated vote-started vote-ended}.freeze
Instance Attribute Summary collapse
-
#fonts_options ⇒ Object
Returns the value of attribute fonts_options.
Instance Method Summary collapse
- #agency(xml) ⇒ Object
- #agency1(xml) ⇒ Object
- #agency_addr(xml) ⇒ Object
- #author(xml, _out) ⇒ Object
- #bibdate(isoxml, _out) ⇒ 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_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
- #labels ⇒ Object
- #MMMddyyyy(isodate) ⇒ Object
- #months ⇒ Object
- #monthyr(isodate) ⇒ Object
- #note(isoxml, _out) ⇒ Object
- #ns(xpath) ⇒ Object
- #otherid(isoxml, _out) ⇒ Object
- #personal_authors(isoxml) ⇒ 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
- #unpublished(status) ⇒ Object
- #url(xml, _out) ⇒ Object
- #version(isoxml, _out) ⇒ Object
Constructor Details
#initialize(lang, script, locale, i18n, fonts_options = {}) ⇒ Metadata
Returns a new instance of Metadata.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/isodoc/metadata.rb', line 18 def initialize(lang, script, locale, i18n, = {}) @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 = end |
Instance Attribute Details
#fonts_options ⇒ Object
Returns the value of attribute fonts_options.
8 9 10 |
# File 'lib/isodoc/metadata.rb', line 8 def @fonts_options end |
Instance Method Details
#agency(xml) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/isodoc/metadata_contributor.rb', line 74 def agency(xml) agency, publisher = agency1(xml) set(:agency, agency.sub(%r{/$}, "")) set(:publisher, @i18n.boolean_conj(publisher, "and")) agency_addr(xml) end |
#agency1(xml) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/isodoc/metadata_contributor.rb', line 61 def agency1(xml) agency = "" publisher = [] xml.xpath(ns("//bibdata/contributor[xmlns:role/@type = 'publisher']/"\ "organization")).each do |org| name = org&.at(ns("./name"))&.text agency1 = org&.at(ns("./abbreviation"))&.text || name publisher << name if name agency = iso?(org) ? "ISO/#{agency}" : "#{agency}#{agency1}/" end [agency, publisher] end |
#agency_addr(xml) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/isodoc/metadata_contributor.rb', line 81 def agency_addr(xml) a = xml.at(ns("//bibdata/contributor[xmlns:role/@type = 'publisher'][1]/"\ "organization")) or return { subdivision: "./subdivision", pub_phone: "./phone[not(@type = 'fax')]", pub_fax: "./phone[@type = 'fax']", pub_email: "./email", pub_uri: "./uri" }.each do |k, v| n = a.at(ns(v)) and set(k, n.text) end n = a.at(ns("./address/formattedAddress")) and set(:pub_address, n.children.to_xml) end |
#author(xml, _out) ⇒ Object
49 50 51 52 |
# File 'lib/isodoc/metadata_contributor.rb', line 49 def (xml, _out) (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'].gsub(/-/, '_')}date".to_sym, Common::date_range(d)) end end |
#currlang ⇒ Object
44 45 46 |
# File 'lib/isodoc/metadata.rb', line 44 def currlang "[@language = '#{@lang}']" end |
#docid(isoxml, _out) ⇒ Object
84 85 86 87 |
# File 'lib/isodoc/metadata.rb', line 84 def docid(isoxml, _out) dn = isoxml.at(ns("//bibdata/docidentifier")) set(:docnumber, dn&.text) end |
#docnumeric(isoxml, _out) ⇒ Object
96 97 98 99 |
# File 'lib/isodoc/metadata.rb', line 96 def docnumeric(isoxml, _out) dn = isoxml.at(ns("//bibdata/docnumber")) set(:docnumeric, dn&.text) end |
#docstatus(xml, _out) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/isodoc/metadata.rb', line 55 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 |
#doctype(isoxml, _out) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/isodoc/metadata.rb', line 48 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)) end |
#draftinfo(draft, revdate) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/isodoc/metadata.rb', line 101 def draftinfo(draft, revdate) return "" unless draft draftinfo = " (#{@labels['draft_label']} #{draft}" draftinfo += ", #{revdate}" if revdate draftinfo += ")" l10n(draftinfo, @lang, @script, @locale) end |
#extract_person_affiliations(authors) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/isodoc/metadata_contributor.rb', line 15 def extract_person_affiliations() .reduce([]) do |m, a| name = a&.at(ns("./affiliation/organization/name"))&.text subdivs = a&.xpath(ns("./affiliation/organization/subdivision"))&.map(&:text)&.join(", ") name and subdivs and !subdivs.empty? and name = l10n("#{name}, #{subdivs}") location = a&.at(ns("./affiliation/organization/address/formattedAddress"))&.text m << (if !name.nil? && !location.nil? l10n("#{name}, #{location}") else (name || location || "") end) m end end |
#extract_person_names(authors) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/isodoc/metadata_contributor.rb', line 3 def extract_person_names() .reduce([]) do |ret, a| if a.at(ns("./name/completename")) ret << a.at(ns("./name/completename")).text else fn = a.xpath(ns("./name/forename")) .each_with_object([]) { |f, m| m << f.text } ret << "#{fn.join(' ')} #{a&.at(ns('./name/surname'))&.text}" end end end |
#extract_person_names_affiliations(authors) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/isodoc/metadata_contributor.rb', line 31 def extract_person_names_affiliations() names = extract_person_names() affils = extract_person_affiliations() ret = {} affils.each_with_index do |a, i| ret[a] ||= [] ret[a] << names[i] end ret end |
#get ⇒ Object
30 31 32 |
# File 'lib/isodoc/metadata.rb', line 30 def get @metadata end |
#iso?(org) ⇒ Boolean
54 55 56 57 58 59 |
# File 'lib/isodoc/metadata_contributor.rb', line 54 def iso?(org) name = org&.at(ns("./name"))&.text abbrev = org&.at(ns("./abbreviation"))&.text (abbrev == "ISO" || name == "International Organization for Standardization") end |
#keywords(isoxml, _out) ⇒ Object
158 159 160 161 162 |
# File 'lib/isodoc/metadata.rb', line 158 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
14 15 16 |
# File 'lib/isodoc/metadata.rb', line 14 def l10n(expr, lang = @lang, script = @script, locale = @locale) @i18n.l10n(expr, lang, script, locale) end |
#labels ⇒ Object
34 35 36 |
# File 'lib/isodoc/metadata.rb', line 34 def labels @labels 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 |
#months ⇒ Object
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
164 165 166 167 168 169 170 |
# File 'lib/isodoc/metadata.rb', line 164 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
10 11 12 |
# File 'lib/isodoc/metadata.rb', line 10 def ns(xpath) Common::ns(xpath) end |
#otherid(isoxml, _out) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/isodoc/metadata.rb', line 89 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
42 43 44 45 46 47 |
# File 'lib/isodoc/metadata_contributor.rb', line 42 def (isoxml) = isoxml.xpath(ns("//bibdata/contributor[role/@type = 'author' "\ "or xmlns:role/@type = 'editor']/person")) set(:authors, extract_person_names()) set(:authors_affiliations, extract_person_names_affiliations()) end |
#relations(isoxml, _out) ⇒ Object
131 132 133 134 |
# File 'lib/isodoc/metadata.rb', line 131 def relations(isoxml, _out) relations_obsoletes(isoxml) relations_partof(isoxml) end |
#relations_obsoletes(isoxml) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/isodoc/metadata.rb', line 142 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
136 137 138 139 140 |
# File 'lib/isodoc/metadata.rb', line 136 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
38 39 40 |
# File 'lib/isodoc/metadata.rb', line 38 def set(key, value) @metadata[key] = value end |
#stage_abbr(docstatus) ⇒ Object
72 73 74 |
# File 'lib/isodoc/metadata.rb', line 72 def stage_abbr(docstatus) status_print(docstatus).split(/ /).map { |s| s[0].upcase }.join end |
#status_print(status) ⇒ Object
80 81 82 |
# File 'lib/isodoc/metadata.rb', line 80 def status_print(status) status.split(/[- ]/).map(&:capitalize).join(" ") end |
#subtitle(_isoxml, _out) ⇒ Object
127 128 129 |
# File 'lib/isodoc/metadata.rb', line 127 def subtitle(_isoxml, _out) nil end |
#title(isoxml, _out) ⇒ Object
122 123 124 125 |
# File 'lib/isodoc/metadata.rb', line 122 def title(isoxml, _out) main = isoxml&.at(ns("//bibdata/title[@language='#{@lang}']"))&.text set(:doctitle, main) end |
#unpublished(status) ⇒ Object
76 77 78 |
# File 'lib/isodoc/metadata.rb', line 76 def unpublished(status) !status.casecmp("published").zero? end |
#url(xml, _out) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/isodoc/metadata.rb', line 150 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
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/isodoc/metadata.rb', line 110 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 |