Module: IsoDoc::Function::References

Included in:
Common
Defined in:
lib/isodoc/function/references.rb

Constant Summary collapse

SKIP_DOCID =
<<~XPATH.strip.freeze
  @type = 'DOI' or @type = 'doi' or @type = 'ISSN' or @type = 'issn' or @type = 'ISBN' or @type = 'isbn' or starts-with(@type, 'ISSN.') or starts-with(@type, 'ISBN.') or starts-with(@type, 'issn.') or starts-with(@type, 'isbn.')
XPATH

Instance Method Summary collapse

Instance Method Details

#bibitem_ref_code(bib) ⇒ Object

returns [metanorma, non-metanorma, DOI/ISSN/ISBN] identifiers



53
54
55
56
57
58
# File 'lib/isodoc/function/references.rb', line 53

def bibitem_ref_code(bib)
  id, id1, id2, id3 = bibitem_ref_code_prep(bib)
  id || id1 || id2 || id3 and return [id, id1, id2, id3]
  bib["suppress_identifier"] == "true" and return [nil, nil, nil, nil]
  [nil, no_identifier(bib), nil, nil]
end

#bibitem_ref_code_prep(bib) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/isodoc/function/references.rb', line 60

def bibitem_ref_code_prep(bib)
  id = bib.at(ns("./docidentifier[@type = 'metanorma']"))
  id1 = pref_ref_code(bib)
  id2 = bib.at(ns("./docidentifier[#{SKIP_DOCID}]"))
  id3 = bib.at(ns("./docidentifier[@type = 'metanorma-ordinal']"))
  [id, id1, id2, id3]
end

#biblio_list(refs, div, biblio) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/isodoc/function/references.rb', line 142

def biblio_list(refs, div, biblio)
  i = 0
  refs.children.each do |b|
    if b.name == "bibitem"
      next if implicit_reference(b)

      i += 1 unless b["hidden"]
      if standard?(b) then std_bibitem_entry(div, b, i, biblio)
      else nonstd_bibitem(div, b, i, biblio)
      end
    else
      parse(b, div) unless %w(title).include? b.name
    end
  end
end

#bibliography(node, out) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/isodoc/function/references.rb', line 182

def bibliography(node, out)
  node["hidden"] != "true" or return
  page_break(out)
  out.div do |div|
    div.h1 class: "Section3" do |h1|
      node.at(ns("./title"))&.children&.each { |c2| parse(c2, h1) }
    end
    biblio_list(node, div, true)
  end
end

#bibliography_parse(node, out) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/isodoc/function/references.rb', line 193

def bibliography_parse(node, out)
  node["hidden"] != true or return
  out.div do |div|
    clause_parse_title(node, div, node.at(ns("./title")), out,
                       { class: "Section3" })
    biblio_list(node, div, true)
  end
end

#bibliography_xpathObject



176
177
178
179
180
# File 'lib/isodoc/function/references.rb', line 176

def bibliography_xpath
  "//bibliography/clause[.//references]" \
    "[not(.//references[@normative = 'true'])] | " \
    "//bibliography/references[@normative = 'false']"
end

#bracket_if_num(num) ⇒ Object



75
76
77
78
79
80
# File 'lib/isodoc/function/references.rb', line 75

def bracket_if_num(num)
  num.nil? and return nil
  num = num.text.sub(/^\[/, "").sub(/\]$/, "")
  /^\d+$/.match?(num) and return "[#{num}]"
  num
end

#docid_l10n(text) ⇒ Object

This is highly specific to ISO, but it’s not a bad precedent for references anyway; keeping here instead of in IsoDoc::Iso for now



6
7
8
9
10
11
# File 'lib/isodoc/function/references.rb', line 6

def docid_l10n(text)
  text.nil? and return text
  @i18n.all_parts and text.gsub!(/All Parts/i, @i18n.all_parts.downcase)
  text.size < 20 and text.gsub!(/ /, "&#xa0;")
  text
end

#docid_prefix(prefix, docid) ⇒ Object



103
104
105
106
107
# File 'lib/isodoc/function/references.rb', line 103

def docid_prefix(prefix, docid)
  docid = "#{prefix} #{docid}" if prefix && !omit_docid_prefix(prefix) &&
    !/^#{prefix}\b/.match(docid)
  docid_l10n(docid)
end

#implicit_reference(bib) ⇒ Object

reference not to be rendered because it is deemed implicit in the standards environment



122
123
124
# File 'lib/isodoc/function/references.rb', line 122

def implicit_reference(bib)
  bib["hidden"] == "true"
end

#iso_bibitem_entry_attrs(bib, biblio) ⇒ Object



116
117
118
# File 'lib/isodoc/function/references.rb', line 116

def iso_bibitem_entry_attrs(bib, biblio)
  { id: bib["id"], class: biblio ? "Biblio" : "NormRef" }
end

#no_identifier(bib) ⇒ Object



68
69
70
71
72
73
# File 'lib/isodoc/function/references.rb', line 68

def no_identifier(bib)
  @i18n.no_identifier or return nil
  id = Nokogiri::XML::Node.new("docidentifier", bib.document)
  id << @i18n.no_identifier
  id
end

#nonstd_bibitem(list, bib, _ordinal, biblio) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/isodoc/function/references.rb', line 13

def nonstd_bibitem(list, bib, _ordinal, biblio)
  list.p **attr_code(iso_bibitem_entry_attrs(bib, biblio)) do |ref|
    tag = bib.at(ns("./biblio-tag"))
    tag&.children&.each { |n| parse(n, ref) }
    reference_format(bib, ref)
  end
end

#norm_ref(node, out) ⇒ Object



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

def norm_ref(node, out)
  node["hidden"] != "true" or return
  out.div do |div|
    clause_name(node, node.at(ns("./title")), div, nil)
    if node.name == "clause"
      node.elements.each { |e| parse(e, div) unless e.name == "title" }
    else biblio_list(node, div, false)
    end
  end
end

#norm_ref_xpathObject



158
159
160
161
162
163
# File 'lib/isodoc/function/references.rb', line 158

def norm_ref_xpath
  "//bibliography/references[@normative = 'true'] | " \
    "//bibliography/clause[.//references[@normative = 'true']] | " \
    "//sections/references[@normative = 'true'] | " \
    "//sections/clause[not(@type)][.//references[@normative = 'true']]"
end

#omit_docid_prefix(prefix) ⇒ Object



109
110
111
112
113
114
# File 'lib/isodoc/function/references.rb', line 109

def omit_docid_prefix(prefix)
  return true if prefix.nil? || prefix.empty?

  %w(ISO IEC IEV ITU W3C BIPM csd metanorma repository metanorma-ordinal)
    .include? prefix
end

#pref_ref_code(bib) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/isodoc/function/references.rb', line 33

def pref_ref_code(bib)
  bib["suppress_identifier"] == "true" and return nil
  ret = bib.xpath(ns("./docidentifier[@scope = 'biblio-tag']"))
  ret.empty? or return ret.map(&:text)
  ret = pref_ref_code_parse(bib) or return nil
  ins = bib.at(ns("./docidentifier[last()]"))
  ret.reverse.each do |r|
    ins.next = "<docidentifier scope='biblio-tag'>#{docid_l10n(r)}</docidentifier>"
  end
  ret
end

#pref_ref_code_parse(bib) ⇒ Object



45
46
47
48
49
50
# File 'lib/isodoc/function/references.rb', line 45

def pref_ref_code_parse(bib)
  data, = @bibrender.parse(bib)
  ret = data[:authoritative_identifier] or return nil
  ret.empty? and return nil
  ret
end

#reference_format(bib, out) ⇒ Object



126
127
128
129
# File 'lib/isodoc/function/references.rb', line 126

def reference_format(bib, out)
  ftitle = bib.at(ns("./formattedref"))
  ftitle&.children&.each { |n| parse(n, out) }
end

#render_identifier(ident) ⇒ Object



96
97
98
99
100
101
# File 'lib/isodoc/function/references.rb', line 96

def render_identifier(ident)
  { metanorma: bracket_if_num(ident[0]),
    sdo: unbracket(ident[1]),
    doi: unbracket(ident[2]),
    ordinal: bracket_if_num(ident[3]) }
end

#standard?(bib) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
# File 'lib/isodoc/function/references.rb', line 131

def standard?(bib)
  ret = false
  bib.xpath(ns("./docidentifier")).each do |id|
    next if id["type"].nil? ||
      id.at(".//self::*[#{SKIP_DOCID} or @type = 'metanorma']")

    ret = true
  end
  ret
end

#std_bibitem_entry(list, bib, _ordinal, biblio) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/isodoc/function/references.rb', line 21

def std_bibitem_entry(list, bib, _ordinal, biblio)
  list.p **attr_code(iso_bibitem_entry_attrs(bib, biblio)) do |ref|
    tag = bib.at(ns("./biblio-tag"))
    tag&.children&.each { |n| parse(n, ref) }
    reference_format(bib, ref)
  end
end

#unbracket(ident) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/isodoc/function/references.rb', line 88

def unbracket(ident)
  if ident.respond_to?(:size)
    ident.map { |x| unbracket1(x) }.join("&#xA0;/ ")
  else
    unbracket1(ident)
  end
end

#unbracket1(ident) ⇒ Object



82
83
84
85
86
# File 'lib/isodoc/function/references.rb', line 82

def unbracket1(ident)
  ident.nil? and return nil
  ident.is_a?(String) or ident = ident.text
  ident.sub(/^\[/, "").sub(/\]$/, "")
end