Class: IsoDoc::ITU::Metadata

Inherits:
Metadata
  • Object
show all
Defined in:
lib/isodoc/itu/metadata.rb,
lib/isodoc/itu/metadata_date.rb

Constant Summary collapse

TITLE_XPATHS =
{ doctitle: "//bibdata/title[@language='@_lang'][@type = 'main']",
docsubtitle: "//bibdata/title[@language='@_lang']" \
             "[@type = 'subtitle']",
amendmenttitle: "//bibdata/title[@language='@_lang']" \
                "[@type = 'amendment']",
corrigendumtitle: "//bibdata/title[@language='@_lang']" \
                  "[@type = 'corrigendum']",
series: "//bibdata/series[@type='main']/title",
series1: "//bibdata/series[@type='secondary']/title",
series2: "//bibdata/series[@type='tertiary']/title",
annextitle: "//bibdata/title[@type='annex']",
collectiontitle: "//bibdata/title[@type='collection']",
slogantitle: "//bibdata/title[@type='slogan']",
positiontitle: "//bibdata/title[@type='position-sp']" }.freeze
PERSON_ATTRS =
{ affiliations: "./affiliation/organization/name",
addresses: "./affiliation/organization/address/formattedAddress",
emails: "./email", faxes: "./phone[@type = 'fax']",
phones: "./phone[not(@type = 'fax')]" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(lang, script, locale, labels) ⇒ Metadata

Returns a new instance of Metadata.



7
8
9
10
11
12
13
14
15
16
# File 'lib/isodoc/itu/metadata.rb', line 7

def initialize(lang, script, locale, labels)
  super
  n = "International_Telecommunication_Union_Logo.svg"
  set(:logo_html, fileloc(n))
  set(:logo_comb, fileloc("itu-document-comb.png"))
  set(:logo_word, fileloc(n))
  set(:logo_sp, fileloc("logo-sp.png"))
  set(:logo_small, fileloc("logo-small.png"))
  @isodoc = IsoDoc::ITU::HtmlConvert.new({})
end

Instance Method Details

#append(key, value) ⇒ Object



101
102
103
# File 'lib/isodoc/itu/metadata.rb', line 101

def append(key, value)
  @metadata[key] << value
end

#author(xml, _out) ⇒ Object



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
# File 'lib/isodoc/itu/metadata.rb', line 64

def author(xml, _out)
  sector = xml.at(ns("//bibdata/ext/editorialgroup/sector"))
  set(:sector, sector.text) if sector
  bureau(xml)
  tc = xml.at(ns("//bibdata/ext/editorialgroup/committee"))
  set(:tc, tc.text) if tc
  tc = xml.at(ns("//bibdata/ext/editorialgroup/group/name"))
  set(:group, tc.text) if tc
  tc = xml.at(ns("//bibdata/ext/editorialgroup/group/acronym"))
  set(:group_acronym, tc.text) if tc
  start1 = xml.at(ns("//bibdata/ext/editorialgroup/group/period/start"))
  end1 = xml.at(ns("//bibdata/ext/editorialgroup/group/period/end"))
  if start1
    set(:study_group_period,
        @i18n.l10n("#{start1.text}#{end1.text}"))
  end
  tc = xml.at(ns("//bibdata/ext/editorialgroup/subgroup/name"))
  set(:subgroup, tc.text) if tc
  tc = xml.at(ns("//bibdata/ext/editorialgroup/workgroup/name"))
  set(:workgroup, tc.text) if tc
  super
  authors = xml.xpath(ns("//bibdata/contributor[role/@type = 'author' " \
                         "or xmlns:role/@type = 'editor']/person"))
  person_attributes(authors) unless authors.empty?
end

#bibdate(xml, _out) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/isodoc/itu/metadata.rb', line 143

def bibdate(xml, _out)
  d = xml.at(ns("//bibdata/date[not(@format)][@type = 'published']"))
  d and set(:pubdate_monthyear, monthyr(d.text))
  d = xml.at(ns("//bibdata/date[@format = 'ddMMMyyyy'][@type = 'published']"))
  d and set(:pubdate_ddMMMyyyy, monthyr(d.text))
  d = xml.at(ns("//bibdata/date[not(@format)][@type = 'published']")) ||
    xml.at(ns("//bibdata/copyright/from"))
  d and set(:placedate_year, @labels["placedate"]
              .sub("%", d.text.sub(/^(\d\d\d\d).*$/, "\\1")))
end

#bureau(xml) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/isodoc/itu/metadata.rb', line 90

def bureau(xml)
  if bureau = xml.at(ns("//bibdata/ext/editorialgroup/bureau"))
    set(:bureau, bureau.text)
    case bureau.text
    when "T" then set(:bureau_full, @i18n.tsb_full)
    when "D" then set(:bureau_full, @i18n.bdt_full)
    when "R" then set(:bureau_full, @i18n.br_full)
    end
  end
end

#contribution(xml, _out) ⇒ Object



181
182
183
184
185
# File 'lib/isodoc/itu/metadata.rb', line 181

def contribution(xml, _out)
  a = xml.at(ns("//bibdata/ext/timing")) and set(:timing, a.text)
  a = xml.at(ns("//bibdata/ext/recommendationstatus/approvalstage/@process")) and
    set(:approval_process, a.text)
end

#ddMMMMYYYY(date1, date2) ⇒ Object



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
# File 'lib/isodoc/itu/metadata_date.rb', line 18

def ddMMMMYYYY(date1, date2)
  m1 = /(?<yr>\d\d\d\d)-(?<mo>\d\d)-(?<dd>\d\d)/.match date1
  m2 = /(?<yr>\d\d\d\d)-(?<mo>\d\d)-(?<dd>\d\d)/.match date2
  if m1 && m1[:yr] && m1[:mo] && m1[:dd]
    dd1 = m1[:dd].sub(/^0/, "")
    if m2 && m2[:yr] && m2[:mo] && m2[:dd]
      dd2 = m2[:dd].sub(/^0/, "")
      if m1[:yr] == m2[:yr]
        if m1[:mo] == m2[:mo]
          @i18n.l10n("#{dd1}&#x2013;#{dd2} #{months[m1[:mo].to_sym]} #{m1[:yr]}")
        else
          @i18n.l10n("#{dd1} #{months[m1[:mo].to_sym]} &#x2013; " \
                     "#{dd2} #{months[m2[:mo].to_sym]} #{m1[:yr]}")
        end
      else
        @i18n.l10n("#{dd1} #{months[m1[:mo].to_sym]} #{m1[:yr]} &#x2013; " \
                   "#{dd2} #{months[m2[:mo].to_sym]} #{m2[:yr]}")
      end
    else
      date2.nil? ? @i18n.l10n("#{dd1} #{months[m1[:mo].to_sym]} #{m1[:yr]}") : "#{date1}/#{date2}"
    end
  else
    date2.nil? ? date1 : "#{date1}/#{date2}"
  end
end

#ddMMMYYYY(isodate) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/isodoc/itu/metadata_date.rb', line 10

def ddMMMYYYY(isodate)
  m = /(?<yr>\d\d\d\d)-(?<mo>\d\d)-(?<dd>\d\d)/.match isodate
  m && m[:yr] && m[:mo] && m[:dd] or return isodate
  mmm = DateTime.parse(isodate).localize(@lang.to_sym)
    .to_additional_s("yMMM")
  @i18n.l10n("#{m[:dd]} #{mmm}")
end

#docid(xml, _out) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/isodoc/itu/metadata.rb', line 120

def docid(xml, _out)
  { docnumber: "ITU", recommendationnumber: "ITU-Recommendation",
    docnumber_lang: "ITU-lang", docnumber_td: "ITU-TemporaryDocument",
    docnumber_provisional: "ITU-provisional" }
    .each do |k, v|
      dn = xml.at(ns("//bibdata/docidentifier[@type = '#{v}']")) and
        set(k, dn.text)
    end
  dn = xml.at(ns("//bibdata/ext/structuredidentifier/annexid"))
  oblig = xml.at(ns("//annex/@obligation"))&.text
  lbl = oblig == "informative" ? @labels["appendix"] : @labels["annex"]
  dn and set(:annexid, @i18n.l10n("#{lbl} #{dn.text}"))
  dn = xml.at(ns("//bibdata/ext/structuredidentifier/amendment")) and
    set(:amendmentid, @i18n.l10n("#{@labels['amendment']} #{dn.text}"))
  dn = xml.at(ns("//bibdata/ext/structuredidentifier/corrigendum")) and
    set(:corrigendumid,
        @i18n.l10n("#{@labels['corrigendum']} #{dn.text}"))
end

#doctype(xml, _out) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/isodoc/itu/metadata.rb', line 162

def doctype(xml, _out)
  d = xml&.at(ns("//bibdata/ext/doctype"))&.text
  set(:doctype_original, d)
  set(:doctype_abbreviated, @labels.dig("doctype_abbrev", d))
  if d == "recommendation-annex"
    set(:doctype, "Recommendation")
    set(:doctype_display, "Recommendation")
  else super
  end
  d = get[:doctype] and
    set(:draft_new_doctype, @labels["draft_new"].sub("%", d))
end

#fileloc(file) ⇒ Object



18
19
20
21
# File 'lib/isodoc/itu/metadata.rb', line 18

def fileloc(file)
  here = File.dirname(__FILE__)
  File.expand_path(File.join(here, "html", file))
end

#ip_notice_received(xml, _out) ⇒ Object



175
176
177
178
179
# File 'lib/isodoc/itu/metadata.rb', line 175

def ip_notice_received(xml, _out)
  received = xml.at(ns("//bibdata/ext/ip-notice-received"))&.text ||
    "false"
  set(:ip_notice_received, received)
end

#keywords(xml, _out) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/isodoc/itu/metadata.rb', line 154

def keywords(xml, _out)
  super
  set(:keywords, get[:keywords].sort)
  q = xml.xpath(ns("//bibdata/ext/question/identifier"))
  q.empty? or set(:questions,
                  q.map { |x| x.text.sub(/^Q/, "") }.join(", "))
end

#meeting(xml) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/isodoc/itu/metadata.rb', line 202

def meeting(xml)
  resolution =
    xml.at(ns("//bibdata/ext/doctype"))&.text == "resolution"
  if o = xml.at(ns("//bibdata/ext/meeting-date/on"))&.text
    set(:meeting_date, resolution ? ddMMMMYYYY(o, nil) : ddMMMYYYY(o))
  elsif f = xml.at(ns("//bibdata/ext/meeting-date/from"))&.text
    t = xml.at(ns("//bibdata/ext/meeting-date/to"))&.text
    set(:meeting_date,
        resolution ? ddMMMMYYYY(f, t) : "#{ddMMMYYYY(f)}/#{ddMMMYYYY(t)}")
  end
end

#monthyr(isodate) ⇒ Object



4
5
6
7
8
# File 'lib/isodoc/itu/metadata_date.rb', line 4

def monthyr(isodate)
  m = /(?<yr>\d\d\d\d)-(?<mo>\d\d)/.match isodate
  m && m[:yr] && m[:mo] or return isodate
  "#{m[:mo]}/#{m[:yr]}"
end

#person_attributes(authors) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/isodoc/itu/metadata.rb', line 111

def person_attributes(authors)
  PERSON_ATTRS.each_key { |k| set(k, []) }
  authors.each do |a|
    PERSON_ATTRS.each do |k, v|
      append(k, a.at(ns(v))&.text)
    end
  end
end

#subtitle(_xml, _out) ⇒ Object



60
61
62
# File 'lib/isodoc/itu/metadata.rb', line 60

def subtitle(_xml, _out)
  nil
end

#techreport(xml, _out) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/isodoc/itu/metadata.rb', line 187

def techreport(xml, _out)
  if a = xml.at(ns("//bibdata/ext/meeting"))&.text
    set(:meeting, a)
    set(:meeting_acronym, a)
  end
  a = xml.at(ns("//bibdata/ext/meeting/@acronym"))&.text and
    set(:meeting_acronym, a)
  a = xml.at(ns("//bibdata/ext/meeting-place"))&.text and
    set(:meeting_place, a)
  a = xml.at(ns("//bibdata/ext/intended-type"))&.text and
    set(:intended_type, a)
  a = xml.at(ns("//bibdata/ext/source"))&.text and set(:source, a)
  meeting(xml)
end

#title(xml, _out) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/isodoc/itu/metadata.rb', line 39

def title(xml, _out)
  TITLE_XPATHS.each do |k, v|
    titleset(xml, k, v.sub("@_lang", @lang))
  end
  titleset(xml, :doctitle_en,
           "//bibdata/title[@language='en'][@type = 'main']") or
    titleset(xml, :doctitle_en,
             "//bibdata/title[@language='#{@lang}'][@type = 'main']")
end

#titleset(xml, key, xpath) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/isodoc/itu/metadata.rb', line 49

def titleset(xml, key, xpath)
  value = xml.at(ns(xpath)) or return
  out = @isodoc.noko do |x|
    x.span do |s|
      value.children.each { |c| @isodoc.parse(c, s) }
    end
  end.join
  set(key, out.sub(%r{^<span>}, "").sub(%r{</span>$}, ""))
  true
end

#unpublished(status) ⇒ Object



139
140
141
# File 'lib/isodoc/itu/metadata.rb', line 139

def unpublished(status)
  %w(in-force-prepublished draft).include? status.downcase
end