Module: Asciidoctor::Standoc::Refs

Included in:
Converter
Defined in:
lib/asciidoctor/standoc/ref.rb,
lib/asciidoctor/standoc/ref_date_id.rb

Constant Summary collapse

MALFORMED_REF =
"no anchor on reference, markup may be malformed: see "\
"https://www.metanorma.com/author/topics/document-format/bibliography/ , "\
"https://www.metanorma.com/author/iso/topics/markup/#bibliographies".freeze
ISO_REF =
%r{^<ref\sid="(?<anchor>[^"]+)">
\[(?<usrlbl>\([^)]+\))?(?<code>(ISO|IEC)[^0-9]*\s[0-9-]+|IEV)
(:(?<year>[0-9][0-9-]+))?\]</ref>,?\s*(?<text>.*)$}xm.freeze
ISO_REF_NO_YEAR =
%r{^<ref\sid="(?<anchor>[^"]+)">
      \[(?<usrlbl>\([^)]+\))?(?<code>(ISO|IEC)[^0-9]*\s[0-9-]+):
      (--|&\#821[12];)\]</ref>,?\s*
(<fn[^>]*>\s*<p>(?<fn>[^\]]+)</p>\s*</fn>)?,?\s?(?<text>.*)$}xm
.freeze
ISO_REF_ALL_PARTS =
%r{^<ref\sid="(?<anchor>[^"]+)">
      \[(?<usrlbl>\([^)]+\))?(?<code>(ISO|IEC)[^0-9]*\s[0-9]+)
      (:(?<year>--|&\#821[12];|[0-9][0-9-]+))?\s
      \(all\sparts\)\]</ref>,?\s*
(<fn[^>]*>\s*<p>(?<fn>[^\]]+)</p>\s*</fn>,?\s?)?(?<text>.*)$}xm.freeze
NON_ISO_REF =
%r{^<ref\sid="(?<anchor>[^"]+)">
      \[(?<usrlbl>\([^)]+\))?(?<code>[^\]]+?)
      ([:-](?<year>(19|20)[0-9][0-9][0-9-]*))?\]</ref>,?\s*(?<text>.*)$}xm
.freeze

Instance Method Summary collapse

Instance Method Details

#analyse_ref_code(code) ⇒ Object

ref id = (usrlbl)codeyear code = nofetch(code) | (repo|path):(key,code) | [? number ]? | ident



145
146
147
148
149
150
# File 'lib/asciidoctor/standoc/ref.rb', line 145

def analyse_ref_code(code)
  ret = { id: code }
  return ret if code.blank?

  analyse_ref_nofetch(analyse_ref_repo_path(analyse_ref_numeric(ret)))
end

#analyse_ref_nofetch(ret) ⇒ Object



123
124
125
126
127
# File 'lib/asciidoctor/standoc/ref.rb', line 123

def analyse_ref_nofetch(ret)
  return ret unless m = /^nofetch\((?<id>.+)\)$/.match(ret[:id])

  ret.merge(id: m[:id], nofetch: true)
end

#analyse_ref_numeric(ret) ⇒ Object



137
138
139
140
141
# File 'lib/asciidoctor/standoc/ref.rb', line 137

def analyse_ref_numeric(ret)
  return ret unless /^\d+$/.match?(ret[:id])

  ret.merge(numeric: true)
end

#analyse_ref_repo_path(ret) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/asciidoctor/standoc/ref.rb', line 129

def analyse_ref_repo_path(ret)
  return ret unless m =
    /^(?<type>repo|path):\((?<key>[^,]+),?(?<id>.*)\)$/.match(ret[:id])

  id = m[:id].empty? ? m[:key].sub(%r{^[^/]+/}, "") : m[:id]
  ret.merge(id: id, type: m[:type], key: m[:key], nofetch: true)
end

#conditional_date(bib, match, noyr) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 26

def conditional_date(bib, match, noyr)
  if match.names.include?("year") && !match[:year].nil?
    bib.date(**{ type: "published" }) do |d|
      noyr and d.on "--" or set_date_range(d, norm_year(match[:year]))
    end
  end
end

#docid(bib, code) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 39

def docid(bib, code)
  type, code1 = if /^\[\d+\]$|^\([^)]+\).*$/.match?(code)
                  ["metanorma", mn_code(code)]
                else
                  @bibdb&.docid_type(code) || [nil, code]
                end
  code1.sub!(/^nofetch\((.+)\)$/, "\\1")
  bib.docidentifier **attr_code(type: type) do |d|
    d << code1
  end
end

#docnumber(bib, code) ⇒ Object



51
52
53
54
55
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 51

def docnumber(bib, code)
  bib.docnumber do |d|
    d << HTMLEntities.new.decode(code).sub(/^[^\d]*/, "")
  end
end

#id_and_year(id, year) ⇒ Object



16
17
18
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 16

def id_and_year(id, year)
  year ? "#{id}:#{year}" : id
end

#iso_publisher(bib, code) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/asciidoctor/standoc/ref.rb', line 6

def iso_publisher(bib, code)
  code.sub(/ .*$/, "").split(/\//).each do |abbrev|
    bib.contributor do |c|
      c.role **{ type: "publisher" }
      c.organization do |org|
        organization(org, abbrev, true)
      end
    end
  end
end

#isorefmatches(xml, m) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asciidoctor/standoc/ref.rb', line 32

def isorefmatches(xml, m)
  yr = norm_year(m[:year])
  ref = fetch_ref xml, m[:code], yr, title: m[:text], usrlbl: m[:usrlbl],
    lang: (@lang || :all)
  return use_my_anchor(ref, m[:anchor]) if ref

  xml.bibitem **attr_code(ref_attributes(m)) do |t|
    isorefrender1(t, m, yr)
    yr and t.date **{ type: "published" } do |d|
      set_date_range(d, yr)
    end
    iso_publisher(t, m[:code])
  end
end

#isorefmatches2(xml, m) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/asciidoctor/standoc/ref.rb', line 47

def isorefmatches2(xml, m)
  ref = fetch_ref xml, m[:code], nil, no_year: true, note: m[:fn],
    title: m[:text], usrlbl: m[:usrlbl], lang: (@lang || :all)
  return use_my_anchor(ref, m[:anchor]) if ref

  isorefmatches2_1(xml, m)
end

#isorefmatches2_1(xml, m) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/asciidoctor/standoc/ref.rb', line 55

def isorefmatches2_1(xml, m)
  xml.bibitem **attr_code(ref_attributes(m)) do |t|
    isorefrender1(t, m, "--")
    t.date **{ type: "published" } do |d|
      d.on "--"
    end
    iso_publisher(t, m[:code])
    unless m[:fn].nil?
      t.note(**plaintxt.merge(type: "Unpublished-Status")) do |p|
      p << (m[:fn]).to_s
      end
    end
  end
end

#isorefmatches3(xml, m) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/asciidoctor/standoc/ref.rb', line 70

def isorefmatches3(xml, m)
  yr = norm_year(m[:year])
  hasyr = !yr.nil? && yr != "--"
  ref = fetch_ref(xml, m[:code], hasyr ? yr : nil,
                  all_parts: true,
                  no_year: yr == "--", text: m[:text], usrlbl: m[:usrlbl],
                  lang: (@lang || :all))
  return use_my_anchor(ref, m[:anchor]) if ref

  isorefmatches3_1(xml, m, yr, hasyr, ref)
end

#isorefmatches3_1(xml, m, yr, _hasyr, _ref) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/asciidoctor/standoc/ref.rb', line 82

def isorefmatches3_1(xml, m, yr, _hasyr, _ref)
  xml.bibitem(**attr_code(ref_attributes(m))) do |t|
    isorefrender1(t, m, yr, " (all parts)")
    conditional_date(t, m, yr == "--")
    iso_publisher(t, m[:code])
    if m.names.include?("fn") && m[:fn]
      t.note(**plaintxt.merge(type: "Unpublished-Status")) do |p|
        p << (m[:fn]).to_s
      end
    end
    t.extent **{ type: "part" } do |e|
      e.referenceFrom "all"
    end
  end
end

#isorefrender1(bib, m, yr, allp = "") ⇒ Object



25
26
27
28
29
30
# File 'lib/asciidoctor/standoc/ref.rb', line 25

def isorefrender1(bib, m, yr, allp = "")
  bib.title(**plaintxt) { |i| i << ref_normalise(m[:text]) }
  docid(bib, m[:usrlbl]) if m[:usrlbl]
  docid(bib, id_and_year(m[:code], yr) + allp)
  docnumber(bib, m[:code])
end

#mn_code(code) ⇒ Object



57
58
59
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 57

def mn_code(code)
  code.sub(/^\(/, "[").sub(/\).*$/, "]").sub(/^nofetch\((.+)\)$/, "\\1")
end

#norm_year(year) ⇒ Object



20
21
22
23
24
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 20

def norm_year(year)
  /^&\#821[12];$/.match(year) and return "--"
  /^\d\d\d\d-\d\d\d\d$/.match(year) and return year
  year&.sub(/(?<=[0-9])-.*$/, "")
end

#plaintxtObject



17
18
19
# File 'lib/asciidoctor/standoc/ref.rb', line 17

def plaintxt
  { format: "text/plain" }
end

#ref_attributes(m) ⇒ Object



21
22
23
# File 'lib/asciidoctor/standoc/ref.rb', line 21

def ref_attributes(m)
  { id: m[:anchor], type: "standard" }
end

#ref_normalise(ref) ⇒ Object



171
172
173
# File 'lib/asciidoctor/standoc/ref.rb', line 171

def ref_normalise(ref)
  ref.gsub(/&amp;amp;/, "&amp;").gsub(%r{^<em>(.*)</em>}, "\\1")
end

#ref_normalise_no_format(ref) ⇒ Object



175
176
177
# File 'lib/asciidoctor/standoc/ref.rb', line 175

def ref_normalise_no_format(ref)
  ref.gsub(/&amp;amp;/, "&amp;")
end

#reference1(node, item, xml) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/asciidoctor/standoc/ref.rb', line 210

def reference1(node, item, xml)
  matched, matched2, matched3 = reference1_matches(item)
  if matched3.nil? && matched2.nil? && matched.nil?
    refitem(xml, item, node)
  elsif !matched.nil? then isorefmatches(xml, matched)
  elsif !matched2.nil? then isorefmatches2(xml, matched2)
  elsif !matched3.nil? then isorefmatches3(xml, matched3)
  end
end

#reference1_matches(item) ⇒ Object



203
204
205
206
207
208
# File 'lib/asciidoctor/standoc/ref.rb', line 203

def reference1_matches(item)
  matched = ISO_REF.match item
  matched2 = ISO_REF_NO_YEAR.match item
  matched3 = ISO_REF_ALL_PARTS.match item
  [matched, matched2, matched3]
end

#refitem(xml, item, node) ⇒ Object

TODO: alternative where only title is available



153
154
155
156
157
# File 'lib/asciidoctor/standoc/ref.rb', line 153

def refitem(xml, item, node)
  m = NON_ISO_REF.match(item) and return refitem1(xml, item, m)
  @log.add("AsciiDoc Input", node, "#{MALFORMED_REF}: #{item}")
  nil
end

#refitem1(xml, _item, m) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/asciidoctor/standoc/ref.rb', line 159

def refitem1(xml, _item, m)
  code = analyse_ref_code(m[:code])
  unless code[:id] && code[:numeric] || code[:nofetch]
    ref = fetch_ref(xml, code[:id],
                    m.names.include?("year") ? m[:year] : nil,
                    title: m[:text],
                    usrlbl: m[:usrlbl], lang: (@lang || :all))
    return use_my_anchor(ref, m[:anchor]) if ref
  end
  refitem_render(xml, m, code)
end

#refitem_render(xml, m, code) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/asciidoctor/standoc/ref.rb', line 109

def refitem_render(xml, m, code)
  xml.bibitem **attr_code(id: m[:anchor]) do |t|
    t.formattedref **{ format: "application/x-isodoc+xml" } do |i|
      i << ref_normalise_no_format(m[:text])
    end
    refitem_render1(m, code, t)
    docnumber(t, code[:id]) unless /^\d+$|^\(.+\)$/.match?(code[:id])
  end
end

#refitem_render1(m, code, bib) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/asciidoctor/standoc/ref.rb', line 98

def refitem_render1(m, code, bib)
  if code[:type] == "path"
    bib.uri code[:key].sub(/\.[a-zA-Z0-9]+$/, ""), **{ type: "URI" }
    bib.uri code[:key].sub(/\.[a-zA-Z0-9]+$/, ""), **{ type: "citation" }
  end
  docid(bib, m[:usrlbl]) if m[:usrlbl]
  docid(bib, /^\d+$/.match?(code[:id]) ? "[#{code[:id]}]" : code[:id])
  code[:type] == "repo" and
    bib.docidentifier code[:key], **{ type: "repository" }
end

#set_date_range(date, text) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 4

def set_date_range(date, text)
  matched = /^(?<from>[0-9]+)(-+(?<to>[0-9]+))?$/.match text
  return unless matched[:from]

  if matched[:to]
    date.from matched[:from]
    date.to matched[:to]
  else
    date.on matched[:from]
  end
end

#use_my_anchor(ref, id) ⇒ Object



34
35
36
37
# File 'lib/asciidoctor/standoc/ref_date_id.rb', line 34

def use_my_anchor(ref, id)
  ref.parent.elements.last["id"] = id
  ref
end