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



148
149
150
151
152
153
# File 'lib/asciidoctor/standoc/ref.rb', line 148

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



126
127
128
129
130
# File 'lib/asciidoctor/standoc/ref.rb', line 126

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



140
141
142
143
144
# File 'lib/asciidoctor/standoc/ref.rb', line 140

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

  ret.merge(numeric: true)
end

#analyse_ref_repo_path(ret) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/asciidoctor/standoc/ref.rb', line 132

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, match) ⇒ Object



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

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

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

#isorefmatches2(xml, match) ⇒ Object



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

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

  isorefmatches2_1(xml, match)
end

#isorefmatches2_1(xml, match) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/asciidoctor/standoc/ref.rb', line 58

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

#isorefmatches3(xml, match) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/asciidoctor/standoc/ref.rb', line 73

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

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

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



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

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

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



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

def isorefrender1(bib, match, yr, allp = "")
  bib.title(**plaintxt) { |i| i << ref_normalise(match[:text]) }
  docid(bib, match[:usrlbl]) if match[:usrlbl]
  docid(bib, id_and_year(match[:code], yr) + allp)
  docnumber(bib, match[: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(match) ⇒ Object



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

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

#ref_normalise(ref) ⇒ Object



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

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

#ref_normalise_no_format(ref) ⇒ Object



179
180
181
# File 'lib/asciidoctor/standoc/ref.rb', line 179

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

#reference1(node, item, xml) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/asciidoctor/standoc/ref.rb', line 214

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



207
208
209
210
211
212
# File 'lib/asciidoctor/standoc/ref.rb', line 207

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



156
157
158
159
160
# File 'lib/asciidoctor/standoc/ref.rb', line 156

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, match) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/asciidoctor/standoc/ref.rb', line 162

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

  refitem_render(xml, match, code)
end

#refitem_render(xml, match, code) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/asciidoctor/standoc/ref.rb', line 112

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

#refitem_render1(match, code, bib) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/asciidoctor/standoc/ref.rb', line 101

def refitem_render1(match, 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, match[:usrlbl]) if match[: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