Class: IsoDoc::Metadata
- Inherits:
-
Object
- Object
- IsoDoc::Metadata
- Defined in:
- lib/isodoc/metadata.rb,
lib/isodoc/metadata_date.rb
Constant Summary collapse
- 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
- #author(xml, _out) ⇒ Object
- #bibdate(isoxml, _out) ⇒ Object
- #docid(isoxml, _out) ⇒ Object
- #docnumeric(isoxml, _out) ⇒ Object
- #docstatus(isoxml, _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, i18n, fonts_options = {}) ⇒ Metadata
constructor
A new instance of Metadata.
- #iso?(org) ⇒ Boolean
- #keywords(isoxml, _out) ⇒ Object
- #l10n(a, b, c) ⇒ Object
- #labels ⇒ Object
- #MMMddyyyy(isodate) ⇒ Object
- #months ⇒ Object
- #monthyr(isodate) ⇒ Object
- #multiple_and(names, andword) ⇒ Object
- #ns(xpath) ⇒ 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, i18n, fonts_options = {}) ⇒ Metadata
Returns a new instance of Metadata.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/isodoc/metadata.rb', line 22 def initialize(lang, script, i18n, = {}) = {} DATETYPES.each { |w| ["#{w.gsub(/-/, '_')}date".to_sym] = 'XXX' } @lang = lang @script = script @c = HTMLEntities.new @i18n = i18n @labels = @i18n.get = end |
Instance Attribute Details
#fonts_options ⇒ Object
Returns the value of attribute fonts_options.
12 13 14 |
# File 'lib/isodoc/metadata.rb', line 12 def end |
Instance Method Details
#agency(xml) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/isodoc/metadata.rb', line 112 def agency(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 set(:agency, agency.sub(%r{/$}, '')) set(:publisher, multiple_and(publisher, @labels['and'])) end |
#author(xml, _out) ⇒ Object
88 89 90 91 |
# File 'lib/isodoc/metadata.rb', line 88 def (xml, _out) (xml) agency(xml) end |
#bibdate(isoxml, _out) ⇒ Object
93 94 95 96 97 |
# File 'lib/isodoc/metadata.rb', line 93 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 |
#docid(isoxml, _out) ⇒ Object
162 163 164 165 |
# File 'lib/isodoc/metadata.rb', line 162 def docid(isoxml, _out) dn = isoxml.at(ns('//bibdata/docidentifier')) set(:docnumber, dn&.text) end |
#docnumeric(isoxml, _out) ⇒ Object
167 168 169 170 |
# File 'lib/isodoc/metadata.rb', line 167 def docnumeric(isoxml, _out) dn = isoxml.at(ns('//bibdata/docnumber')) set(:docnumeric, dn&.text) end |
#docstatus(isoxml, _out) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/isodoc/metadata.rb', line 134 def docstatus(isoxml, _out) docstatus = isoxml.at(ns('//bibdata/status/stage')) set(:unpublished, true) if docstatus set(:stage, status_print(docstatus.text)) (i = isoxml&.at(ns('//bibdata/status/substage'))&.text) && set(:substage, i) (i = isoxml&.at(ns('//bibdata/status/iteration'))&.text) && set(:iteration, i) set(:unpublished, unpublished(docstatus.text)) unpublished(docstatus.text) && set(:stageabbr, stage_abbr(docstatus.text)) end end |
#doctype(isoxml, _out) ⇒ Object
99 100 101 102 103 |
# File 'lib/isodoc/metadata.rb', line 99 def doctype(isoxml, _out) b = isoxml&.at(ns('//bibdata/ext/doctype'))&.text || return t = b.split(/[- ]/).map(&:capitalize).join(' ') set(:doctype, t) end |
#draftinfo(draft, revdate) ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'lib/isodoc/metadata.rb', line 172 def draftinfo(draft, revdate) draftinfo = '' if draft draftinfo = " (#{@labels['draft_label']} #{draft}" draftinfo += ", #{revdate}" if revdate draftinfo += ')' end l10n(draftinfo, @lang, @script) end |
#extract_person_affiliations(authors) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/isodoc/metadata.rb', line 59 def extract_person_affiliations() .reduce([]) do |m, a| name = a&.at(ns('./affiliation/organization/name'))&.text location = a&.at(ns('./affiliation/organization/address/'\ 'formattedAddress'))&.text m << (!name.nil? && !location.nil? ? "#{name}, #{location}" : (name || location || '')) m end end |
#extract_person_names(authors) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/isodoc/metadata.rb', line 45 def extract_person_names() .reduce([]) do |ret, a| if a.at(ns('./name/completename')) ret << a.at(ns('./name/completename')).text else fn = [] forenames = a.xpath(ns('./name/forename')) forenames.each { |f| fn << f.text } surname = a&.at(ns('./name/surname'))&.text ret << fn.join(' ') + ' ' + surname end end end |
#extract_person_names_affiliations(authors) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/isodoc/metadata.rb', line 70 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
33 34 35 |
# File 'lib/isodoc/metadata.rb', line 33 def get end |
#iso?(org) ⇒ Boolean
105 106 107 108 109 110 |
# File 'lib/isodoc/metadata.rb', line 105 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
229 230 231 232 233 |
# File 'lib/isodoc/metadata.rb', line 229 def keywords(isoxml, _out) ret = [] isoxml.xpath(ns('//bibdata/keyword')).each { |kw| ret << kw.text } set(:keywords, ret) end |
#l10n(a, b, c) ⇒ Object
18 19 20 |
# File 'lib/isodoc/metadata.rb', line 18 def l10n(a, b, c) @i18n.l10n(a, b, c) end |
#labels ⇒ Object
37 38 39 |
# File 'lib/isodoc/metadata.rb', line 37 def labels @labels end |
#MMMddyyyy(isodate) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/isodoc/metadata_date.rb', line 27 def MMMddyyyy(isodate) return nil if isodate.nil? arr = isodate.split("-") date = if arr.size == 1 and (/^\d+$/.match isodate) Date.new(*arr.map(&:to_i)).strftime("%Y") elsif arr.size == 2 Date.new(*arr.map(&:to_i)).strftime("%B %Y") else Date.parse(isodate).strftime("%B %d, %Y") end end |
#months ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/isodoc/metadata_date.rb', line 3 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
20 21 22 23 24 25 |
# File 'lib/isodoc/metadata_date.rb', line 20 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]}", @lang, @script) end |
#multiple_and(names, andword) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/isodoc/metadata.rb', line 126 def multiple_and(names, andword) return '' if names.empty? return names[0] if names.length == 1 (names.length == 2) && (return l10n("#{names[0]} #{andword} #{names[1]}", @lang, @script)) l10n(names[0..-2].join(', ') + " #{andword} #{names[-1]}", @lang, @script) end |
#ns(xpath) ⇒ Object
14 15 16 |
# File 'lib/isodoc/metadata.rb', line 14 def ns(xpath) Common::ns(xpath) end |
#personal_authors(isoxml) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/isodoc/metadata.rb', line 81 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
202 203 204 205 |
# File 'lib/isodoc/metadata.rb', line 202 def relations(isoxml, _out) relations_obsoletes(isoxml) relations_partof(isoxml) end |
#relations_obsoletes(isoxml) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/isodoc/metadata.rb', line 213 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
207 208 209 210 211 |
# File 'lib/isodoc/metadata.rb', line 207 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) [key] = value end |
#stage_abbr(docstatus) ⇒ Object
149 150 151 152 |
# File 'lib/isodoc/metadata.rb', line 149 def stage_abbr(docstatus) status_print(docstatus).split(/ /) .map { |s| s[0].upcase }.join('') end |
#status_print(status) ⇒ Object
158 159 160 |
# File 'lib/isodoc/metadata.rb', line 158 def status_print(status) status.split(/-/).map(&:capitalize).join(' ') end |
#subtitle(_isoxml, _out) ⇒ Object
198 199 200 |
# File 'lib/isodoc/metadata.rb', line 198 def subtitle(_isoxml, _out) nil end |
#title(isoxml, _out) ⇒ Object
193 194 195 196 |
# File 'lib/isodoc/metadata.rb', line 193 def title(isoxml, _out) main = isoxml&.at(ns("//bibdata/title[@language='en']"))&.text set(:doctitle, main) end |
#unpublished(status) ⇒ Object
154 155 156 |
# File 'lib/isodoc/metadata.rb', line 154 def unpublished(status) !status.casecmp('published').zero? end |
#url(xml, _out) ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/isodoc/metadata.rb', line 221 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
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/isodoc/metadata.rb', line 182 def version(isoxml, _out) set(:edition, isoxml&.at(ns('//bibdata/edition'))&.text) set(:docyear, isoxml&.at(ns('//bibdata/copyright/from'))&.text) set(:draft, isoxml&.at(ns('//version/draft'))&.text) revdate = isoxml&.at(ns('//version/revision-date'))&.text set(:revdate, revdate) set(:revdate_monthyear, monthyr(revdate)) set(:draftinfo, draftinfo(get[:draft], get[:revdate])) end |