Module: Iso690Render

Defined in:
lib/isodoc/nist/render.rb,
lib/isodoc/nist/render_dates.rb,
lib/isodoc/nist/render_contributors.rb

Class Method Summary collapse

Class Method Details

.accessLocation(doc) ⇒ Object



109
110
111
112
# File 'lib/isodoc/nist/render.rb', line 109

def self.accessLocation(doc)
  s = doc.at("./accessLocation") or return ""
  s.text
end

.blank?(x) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/isodoc/nist/render.rb', line 21

def self.blank?(x)
  x.nil? || x.empty?
end

.commajoin(a, b) ⇒ Object



29
30
31
32
33
34
# File 'lib/isodoc/nist/render_contributors.rb', line 29

def self.commajoin(a, b)
  return a unless b
  return b unless a
  #"#{a}, #{b}"
  "#{a} #{b}"
end

.contributorRole(contributors) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/isodoc/nist/render_contributors.rb', line 56

def self.contributorRole(contributors)
  return "" unless contributors.length > 0
  if contributors[0]&.at("role/@type")&.text == "editor"
    return contributors.length > 1 ? " (Eds.)" : "(Ed.)"
  end
  ""
end

.creatornames(doc) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/isodoc/nist/render_contributors.rb', line 64

def self.creatornames(doc)
  cr = doc.xpath("./contributor[role/@type = 'author']")
  cr.empty? and cr = doc.xpath("./contributor[role/@type = 'performer']")
  cr.empty? and cr = doc.xpath("./contributor[role/@type = 'adapter']")
  cr.empty? and cr = doc.xpath("./contributor[role/@type = 'translator']")
  cr.empty? and cr = doc.xpath("./contributor[role/@type = 'editor']")
  cr.empty? and cr = doc.xpath("./contributor[role/@type = 'publisher']")
  cr.empty? and cr = doc.xpath("./contributor[role/@type = 'distributor']")
  cr.empty? and cr = doc.xpath("./contributor")
  cr.empty? and return ""
  ret = []
  cr.each do |x|
    ret << extractname(x)
  end
  multiplenames(ret) + contributorRole(cr)
end

.date(doc) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/isodoc/nist/render_dates.rb', line 12

def self.date(doc)
  updated = date1(doc&.at("./date[@type = 'updated']"))
  pub = date1(doc&.at("./date[@type = 'issued']"))
  if pub
    ret = pub
    ret += " (updated #{updated})" if updated
    return ret
  end
  pub = date1(doc&.at("./date[@type = 'circulated']")) and return pub
  date1(doc&.at("./date"))
end

.date1(date) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/isodoc/nist/render_dates.rb', line 2

def self.date1(date)
  return nil if date.nil?
  on = date&.at("./on")&.text
  from = date&.at("./from")&.text
  to = date&.at("./to")&.text
  return MMMddyyyy(on) if on
  return "#{MMMddyyyy(from)}&ndash;#{MMMddyyyy(to)}" if from
  nil
end

.draft(doc) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/isodoc/nist/render.rb', line 165

def self.draft(doc)
  return nil unless is_nist(doc)
  dr = doc&.at("./status/stage")&.text
  iter = doc&.at("./status/iteration")&.text
  return nil unless /^draft/.match(dr)
  iterord = iter_ordinal(doc)
  status = status_print(dr)
  status = "#{iterord} #{status}" if iterord
  status
end

.extent(localities) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/isodoc/nist/render.rb', line 149

def self.extent(localities)
  ret = []
  ret1 = ""
  localities.each do |l|
    if %w(localityStack).include? l.name
      ret << ret1
      ret1 = ""
      ret << extent1(l.children)
    else
      ret1 += extent1([l])
    end
  end
  ret << ret1
  ret.reject { |c| c.empty? }.join("; ")
end

.extent1(localities) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/isodoc/nist/render.rb', line 141

def self.extent1(localities)
  ret = []
  localities.each do |l|
    ret << extent2(l["type"] || "page", l.at("./referenceFrom"), l.at("./referenceTo"))
  end
  ret.join(", ")
end

.extent2(type, from, to) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/isodoc/nist/render.rb', line 129

def self.extent2(type, from, to)
  ret = ""
  case type 
  when "page" then type = to ? "pp." : "p."
  when "volume" then type = to ? "Vols." : "Vol."
  end
  ret += "#{type} "
  ret += from.text if from
  ret += "&ndash;#{to.text}" if to
  ret
end

.extract_orgname(org) ⇒ Object



15
16
17
18
# File 'lib/isodoc/nist/render_contributors.rb', line 15

def self.extract_orgname(org)
  name = org.at("./name")
  name&.text || "--"
end

.extract_personname(person) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/isodoc/nist/render_contributors.rb', line 36

def self.extract_personname(person)
  completename = person.at("./name/completename")
  return completename.text if completename
  surname = person.at("./name/surname")
  initials = person.xpath("./name/initial")
  forenames = person.xpath("./name/forename")
  #given = []
  #forenames.each { |x| given << x.text }
  #given.empty? && initials.each { |x| given << x.text }
  commajoin(surname&.text, frontname(forenames, initials))
end

.extractname(contributor) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/isodoc/nist/render_contributors.rb', line 48

def self.extractname(contributor)
  org = contributor.at("./organization")
  person = contributor.at("./person")
  return extract_orgname(org) if org
  return extract_personname(person) if person
  "--"
end

.frontname(given, initials) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/isodoc/nist/render_contributors.rb', line 20

def self.frontname(given, initials)
  if given.empty? && initials.empty? then ""
  elsif initials.empty?
    given.map{ |m| m.text[0] }.join("")
  else
    initials.map{ |m| m.text[0] }.join("")
  end
end

.included(type) ⇒ Object



114
115
116
# File 'lib/isodoc/nist/render.rb', line 114

def self.included(type)
  ["article", "inbook", "incollection", "inproceedings"].include? type
end

.is_nist(doc) ⇒ Object

def self.edition(doc)

  x = doc.at("./edition")
  return "" unless x
  return x.text unless /^\d+$/.match x.text
  x.text.to_i.localize.to_rbnf_s("SpelloutRules", "spellout-ordinal")
end


34
35
36
37
38
39
# File 'lib/isodoc/nist/render.rb', line 34

def self.is_nist(doc)
  publisher = doc&.at("./contributor[role/@type = 'publisher']/organization/name")&.text
  abbr = doc&.at("./contributor[role/@type = 'publisher']/organization/abbreviation")&.text
  publisher == "NIST" || abbr == "NIST" ||
    publisher == "National Institute of Standards and Technology"
end

.iter_ordinal(isoxml) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/isodoc/nist/render.rb', line 176

def self.iter_ordinal(isoxml)
  docstatus = isoxml.at(("./status/stage"))&.text
  return nil unless docstatus == "draft-public"
  iter = isoxml.at(("./status/iteration"))&.text || "1"
  return "Initial" if iter == "1"
  return "Final" if iter.downcase == "final"
  iter.to_i.localize.to_rbnf_s("SpelloutRules", "spellout-ordinal").capitalize
end

.medium(doc) ⇒ Object



17
18
19
# File 'lib/isodoc/nist/render.rb', line 17

def self.medium(doc)
  doc&.at("./medium")&.text
end

.MMMddyyyy(isodate) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/isodoc/nist/render_dates.rb', line 29

def self.MMMddyyyy(isodate)
  return nil if isodate.nil?
  return isodate if isodate == "--"
  arr = isodate.split("-")
  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

.multiplenames(names) ⇒ Object

def self.multiplenames_and(names)

  return "" if names.length == 0
  return names[0] if names.length == 1
  return "#{names[0]} and #{names[1]}" if names.length == 2
  names[0..-2].join(", ") + " and #{names[-1]}"
end


11
12
13
# File 'lib/isodoc/nist/render_contributors.rb', line 11

def self.multiplenames(names)
  names.join(", ")
end

.parse(doc, embedded = false) ⇒ Object

converting bibitem to <formattedref> + <docidentifier>



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/isodoc/nist/render.rb', line 198

def self.parse(doc, embedded = false)
  f = doc.at("./formattedref") and 
    return embedded ? f.children.to_xml : doc.to_xml

  ret = ""
  type = type(doc)
  container = doc.at("./relation[@type='includedIn']")
  if container && !date(doc) && date(container&.at("./bibitem"))
    doc << ( container&.at("./bibitem/date[@type = 'issued' or @type = 'published' or "\
                    "@type = 'circulated']")&.remove )
  end
  dr = draft(doc)
  cr = creatornames(doc)
  # NIST has seen fit to completely change rendering based on the type of publication.
  if series_title(doc) == "NIST Federal Information Processing Standards"
    cr = "National Institute of Standards and Technology"
  end
  pub = placepub(doc)

  ret += wrap(cr, "", "")
  if dr
    mdy = MMMddyyyy(date(doc)) and ret += wrap(mdy, " (", ")")
  else
    yr = year(date(doc)) and ret += wrap(yr, " (", ")")
  end
  ret += included(type) ? wrap(title(doc), " ", "") : wrap(title(doc), " <em>", "</em>")
  ret += wrap(medium(doc), " [", "]")
  #ret += wrap(edition(doc), "", " edition.")
  if cr != pub
    ret += wrap(pub, " (", ")")
  end
  if cr != pub && pub && !pub.empty? && (dr || !blank?(series(doc, type)))
    ret += ","
  end
  if dr
    ret += " Draft (#{dr})"
  end
  ret += wrap(series(doc, type), " ", "")
  ret += wrap(date(doc), ", ", "")
  ret += wrap(standardidentifier(doc), ". ", "") unless is_nist(doc)
  ret += wrap(uri(doc), ". ", "")
  ret += wrap(accessLocation(doc), ". At: ", "")
  if container 
    ret += wrap(parse(container.at("./bibitem"), true), ". In: ", "")
    locality = doc.xpath("./extent")
    ret += wrap(extent(locality), ", " , "")
  else
    ret += wrap(extent(doc.xpath("./extent")), ", ", "")
  end
  if !embedded
    ret += "."
  end

  embedded ? ret : "<formattedref>#{ret}</formattedref>#{doc.xpath('./docidentifier').to_xml}"
end

.placepub(doc) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/isodoc/nist/render.rb', line 41

def self.placepub(doc)
  place = doc&.at("./place")&.text
  publisher = doc&.at("./contributor[role/@type = 'publisher']/organization/name")&.text
  abbr = doc&.at("./contributor[role/@type = 'publisher']/organization/abbreviation")&.text
  series = series_title(doc)
  series == "NIST Federal Information Processing Standards" and
    return "U.S. Department of Commerce, Washington, D.C."
  is_nist(doc) and
    return "National Institute of Standards and Technology, Gaithersburg, MD"
  ret = ""
  ret += place if place
  ret += ": " if place && publisher
  ret += publisher if publisher
  ret
end

.render(bib, embedded = false) ⇒ Object



7
8
9
10
11
# File 'lib/isodoc/nist/render.rb', line 7

def self.render(bib, embedded = false)
  docxml = Nokogiri::XML(bib)
  docxml.remove_namespaces!
  parse(docxml.root, embedded)
end

.series(doc, type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/isodoc/nist/render.rb', line 62

def self.series(doc, type)
  s = doc.at("./series[@type = 'main']") || doc.at("./series[not(@type)]") || doc.at("./series")
  return "" unless s
  f = s.at("./formattedref") and return f.text
  t = s.at("./title")
  a = s.at("./abbreviation")
  n = s.at("./number")
  p = s.at("./partnumber")
  dn = doc.at("./docnumber")
  rev = doc&.at(".//edition")&.text&.sub(/^Revision /, "")
  ret = ""
  if t
    title = included(type) ? wrap(t.text, " <em>", "</em>") : wrap(t.text, " ", "")
    ret += title
    ret += " (#{a.text.sub(/^NIST /, "")})" if a
  end
  if n || p
    ret += " #{n.text}" if n
    ret += ".#{p.text}" if p
  elsif dn && is_nist(doc)
    ret += " #{dn.text}"
    ret += " Rev. #{rev}" if rev
  end
  ret
end

.series_title(doc) ⇒ Object



57
58
59
60
# File 'lib/isodoc/nist/render.rb', line 57

def self.series_title(doc)
  s = doc.at("./series[@type = 'main']") || doc.at("./series[not(@type)]") || doc.at("./series")
  s&.at("./title")&.text
end

.standardidentifier(doc) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/isodoc/nist/render.rb', line 88

def self.standardidentifier(doc)
  ret = []
  doc.xpath("./docidentifier").each do |id|
    next if %w(nist-mr nist-long metanorma rfc-anchor).include? id["type"]
    ret << standardidentifier1(id)
  end
  ret.join(". ")
end

.standardidentifier1(id) ⇒ Object



97
98
99
100
101
102
# File 'lib/isodoc/nist/render.rb', line 97

def self.standardidentifier1(id)
  r = ""
  r += "#{id['type']} " if id["type"] and !%w(ISO IEC NIST).include? id["type"]
  r += id.text
  r
end

.status_print(status) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/isodoc/nist/render.rb', line 185

def self.status_print(status)
  case status
  when "draft-internal" then "Internal Draft"
  when "draft-wip" then "Work-in-Progress Draft"
  when "draft-prelim" then "Preliminary Draft"
  when "draft-public" then "Public Draft"
  when "draft-approval" then "Approval Draft"
  when "final" then "Final"
  when "final-review" then "Under Review"
  end
end

.title(doc) ⇒ Object



13
14
15
# File 'lib/isodoc/nist/render.rb', line 13

def self.title(doc)
  doc&.at("./title")&.text
end

.type(doc) ⇒ Object



123
124
125
126
127
# File 'lib/isodoc/nist/render.rb', line 123

def self.type(doc)
  type = doc.at("./@type") and return type&.text
  doc.at("./includedIn") and return "inbook"
  "book"
end

.uri(doc) ⇒ Object



104
105
106
107
# File 'lib/isodoc/nist/render.rb', line 104

def self.uri(doc)
  uri = doc.at("./uri[@type = 'doi']") || doc.at("./uri[@type = 'uri']") || doc.at("./uri")
  uri&.text
end

.wrap(text, startdelim = " ", enddelim = ".") ⇒ Object



118
119
120
121
# File 'lib/isodoc/nist/render.rb', line 118

def self.wrap(text, startdelim = " ", enddelim = ".")
  return "" if blank?(text)
  "#{startdelim}#{text}#{enddelim}"
end

.year(date) ⇒ Object



24
25
26
27
# File 'lib/isodoc/nist/render_dates.rb', line 24

def self.year(date)
  return nil if date.nil?
  date.sub(/^(\d\d\d\d).*$/, "\\1")
end