Class: IsoDoc::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc/metadata.rb

Constant Summary collapse

DATETYPES =
%w{published accessed created implemented obsoleted confirmed
updated issued}.freeze
STAGE_ABBRS =
{
  "00": "PWI",
  "10": "NWIP",
  "20": "WD",
  "30": "CD",
  "40": "DIS",
  "50": "FDIS",
  "60": "IS",
  "90": "(Review)",
  "95": "(Withdrawal)",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(lang, script, labels) ⇒ Metadata

Returns a new instance of Metadata.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/isodoc/metadata.rb', line 10

def initialize(lang, script, labels)
  @metadata = {
    tc: "XXXX",
    sc: "XXXX",
    wg: "XXXX",
    editorialgroup: [],
    secretariat: "XXXX",
    obsoletes: nil,
    obsoletes_part: nil
  }
  DATETYPES.each { |w| @metadata["#{w}date".to_sym] = "XXX" }
  @lang = lang
  @script = script
  @c = HTMLEntities.new
  @labels = labels
end

Instance Method Details

#agency(xml) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/isodoc/metadata.rb', line 99

def agency(xml)
  agency = ""
  xml.xpath(ns("//bibdata/contributor[xmlns:role/@type = 'publisher']/"\
               "organization")).each do |org|
    name = org&.at(ns("./name"))&.text
    abbrev = org&.at(ns("./abbreviation"))&.text
    agency1 = abbrev || name
    agency = iso?(org) ?  "ISO/#{agency}" : "#{agency}#{agency1}/"
  end
  set(:agency, agency.sub(%r{/$}, ""))
end

#author(xml, _out) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/isodoc/metadata.rb', line 35

def author(xml, _out)
  tc(xml)
  sc(xml)
  wg(xml)
  secretariat(xml)
  agency(xml)
end

#bibdate(isoxml, _out) ⇒ Object



79
80
81
82
83
# File 'lib/isodoc/metadata.rb', line 79

def bibdate(isoxml, _out)
  isoxml.xpath(ns("//bibdata/date")).each do |d|
    set("#{d['type']}date".to_sym, Common::date_range(d))
  end
end

#compose_title(main, intro, part, partnum, subpartnum, lang) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/isodoc/metadata.rb', line 196

def compose_title(main, intro, part, partnum, subpartnum, lang)
  main = main.nil? ? "" : @c.encode(main.text, :hexadecimal)
  intro &&
    main = "#{@c.encode(intro.text, :hexadecimal)} — #{main}"
  if part
    suffix = part_title(part, partnum, subpartnum, lang)
    main = "#{main} — #{suffix}"
  end
  main
end

#docid(isoxml, _out) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/isodoc/metadata.rb', line 150

def docid(isoxml, _out)
  dn = docnumber(isoxml)
  docstatus = get[:stage]
  if docstatus
    abbr = get[:stageabbr]
    docstatus = get[:stage]
    (docstatus.to_i < 60) && dn = abbr + " " + dn
  end
  set(:docnumber, dn)
end

#docnumber(isoxml) ⇒ Object



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

def docnumber(isoxml)
  docnumber = isoxml.at(ns("//project-number"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  subpartnumber = isoxml.at(ns("//project-number/@subpart"))
  dn = docnumber&.text || ""
  dn += "-#{partnumber.text}" if partnumber
  dn += "-#{subpartnumber.text}" if subpartnumber
  dn
end

#docstatus(isoxml, _out) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/isodoc/metadata.rb', line 140

def docstatus(isoxml, _out)
  docstatus = isoxml.at(ns("//status/stage"))
  if docstatus
    set(:stage, docstatus.text)
    abbr = stage_abbrev(docstatus.text, isoxml.at(ns("//status/iteration")),
                        isoxml.at(ns("//version/draft")))
    set(:stageabbr, abbr)
  end
end

#doctype(isoxml, _out) ⇒ Object



85
86
87
88
89
90
# File 'lib/isodoc/metadata.rb', line 85

def doctype(isoxml, _out)
  b = isoxml.at(ns("//bibdata")) || return
  return unless b["type"]
  t = b["type"].split(/-/).map{ |w| w.capitalize }.join(" ")
  set(:doctype, t)
end

#draftinfo(draft, revdate) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/isodoc/metadata.rb', line 161

def draftinfo(draft, revdate)
  draftinfo = ""
  if draft
    draftinfo = " (#{@labels["draft"]} #{draft}"
    draftinfo += ", #{revdate}" if revdate
    draftinfo += ")"
  end
  IsoDoc::Function::I18n::l10n(draftinfo, @lang, @script)
end

#getObject



27
28
29
# File 'lib/isodoc/metadata.rb', line 27

def get
  @metadata
end

#iso?(org) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/isodoc/metadata.rb', line 92

def iso?(org)
  name = org&.at(ns("./name"))&.text
  abbrev = org&.at(ns("./abbreviation"))&.text
  (abbrev == "ISO" ||
   name == "International Organization for Standardization" )
end

#ns(xpath) ⇒ Object



6
7
8
# File 'lib/isodoc/metadata.rb', line 6

def ns(xpath)
  Common::ns(xpath)
end

#part_label(lang) ⇒ Object

we don’t leave this to i18n.rb, because we have both English and French titles in the same document



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

def part_label(lang)
  case lang
  when "en" then "Part"
  when "fr" then "Partie"
  end
end

#part_title(part, partnum, subpartnum, lang) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/isodoc/metadata.rb', line 188

def part_title(part, partnum, subpartnum, lang)
  return "" unless part
  suffix = @c.encode(part.text, :hexadecimal)
  partnum = "#{partnum}&ndash;#{subpartnum}" if partnum && subpartnum
  suffix = "#{part_label(lang)}&nbsp;#{partnum}: " + suffix if partnum
  suffix
end

#relations(isoxml, _out) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/isodoc/metadata.rb', line 234

def relations(isoxml, _out)
  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

#sc(xml) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/isodoc/metadata.rb', line 54

def sc(xml)
  sc_num = xml.at(ns("//editorialgroup/subcommittee/@number"))
  sc_type = xml.at(ns("//editorialgroup/subcommittee/@type"))&.text || "SC"
  if sc_num
    scid = "#{sc_type} #{sc_num.text}"
    set(:sc, scid)
    set(:editorialgroup, get[:editorialgroup] << scid)
  end
end

#secretariat(xml) ⇒ Object



74
75
76
77
# File 'lib/isodoc/metadata.rb', line 74

def secretariat(xml)
  sec = xml.at(ns("//editorialgroup/secretariat"))
  set(:secretariat, sec.text) if sec
end

#set(key, value) ⇒ Object



31
32
33
# File 'lib/isodoc/metadata.rb', line 31

def set(key, value)
  @metadata[key] = value
end

#stage_abbrev(stage, iter, draft) ⇒ Object



133
134
135
136
137
138
# File 'lib/isodoc/metadata.rb', line 133

def stage_abbrev(stage, iter, draft)
  stage = STAGE_ABBRS[stage.to_sym] || "??"
  stage += iter.text if iter
  stage = "Pre" + stage if draft&.text =~ /^0\./
  stage
end

#subtitle(isoxml, _out) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/isodoc/metadata.rb', line 221

def subtitle(isoxml, _out)
  intro = isoxml.at(ns("//title-intro[@language='fr']"))
  main = isoxml.at(ns("//title-main[@language='fr']"))
  part = isoxml.at(ns("//title-part[@language='fr']"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  subpartnumber = isoxml.at(ns("//project-number/@subpart"))
  set(:docsubtitlemain, @c.encode(main ? main.text : "", :hexadecimal))
  main = compose_title(main, intro, part, partnumber, subpartnumber, "fr")
  set(:docsubtitle, main)
  set(:docsubtitleintro, @c.encode(intro ? intro.text : "", :hexadecimal)) if intro
  set(:docsubtitlepart, part_title(part, partnumber, subpartnumber, "fr"))
end

#tc(xml) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/isodoc/metadata.rb', line 43

def tc(xml)
  tc_num = xml.at(ns("//editorialgroup/technical-committee/@number"))
  tc_type = xml.at(ns("//editorialgroup/technical-committee/@type"))&.
    text || "TC"
  if tc_num
    tcid = "#{tc_type} #{tc_num.text}"
    set(:tc,  tcid)
    set(:editorialgroup, get[:editorialgroup] << tcid)
  end
end

#title(isoxml, _out) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/isodoc/metadata.rb', line 207

def title(isoxml, _out)
  intro = isoxml.at(ns("//title-intro[@language='en']"))
  main = isoxml.at(ns("//title-main[@language='en']"))
  part = isoxml.at(ns("//title-part[@language='en']"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  subpartnumber = isoxml.at(ns("//project-number/@subpart"))

  set(:doctitlemain, @c.encode(main ? main.text : "", :hexadecimal))
  main = compose_title(main, intro, part, partnumber, subpartnumber, "en")
  set(:doctitle, main)
  set(:doctitleintro, @c.encode(intro ? intro.text : "", :hexadecimal)) if intro
  set(:doctitlepart, part_title(part, partnumber, subpartnumber, "en"))
end

#version(isoxml, _out) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/isodoc/metadata.rb', line 171

def version(isoxml, _out)
  set(:docyear, isoxml&.at(ns("//copyright/from"))&.text)
  set(:draft, isoxml&.at(ns("//version/draft"))&.text)
  set(:revdate, isoxml&.at(ns("//version/revision-date"))&.text)
  set(:draftinfo,
      draftinfo(get[:draft], get[:revdate]))
end

#wg(xml) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/isodoc/metadata.rb', line 64

def wg(xml)
  wg_num = xml.at(ns("//editorialgroup/workgroup/@number"))
  wg_type = xml.at(ns("//editorialgroup/workgroup/@type"))&.text || "WG"
  if wg_num
    wgid = "#{wg_type} #{wg_num.text}"
    set(:wg, wgid)
    set(:editorialgroup, get[:editorialgroup] << wgid)
  end
end