Module: Asciidoctor::Standoc::Refs

Included in:
Converter
Defined in:
lib/asciidoctor/standoc/ref.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
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
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
NON_ISO_REF =
%r{^<ref\sid="(?<anchor>[^"]+)">
\[(?<usrlbl>\([^)]+\))?(?<code>[^\]]+?)
([:-](?<year>(19|20)[0-9][0-9][0-9-]*))?\]</ref>,?\s*(?<text>.*)$}xm

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



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

def analyse_ref_code(code)
  ret = {id: code}
  return ret if code.nil? || code.empty?
  analyse_ref_nofetch(analyse_ref_repo_path(analyse_ref_numeric(ret)))
end

#analyse_ref_nofetch(ret) ⇒ Object



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

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



168
169
170
171
# File 'lib/asciidoctor/standoc/ref.rb', line 168

def analyse_ref_numeric(ret)
  return ret unless /^\d+$/.match(ret[:id])
  ret.merge(numeric: true)
end

#analyse_ref_repo_path(ret) ⇒ Object



162
163
164
165
166
# File 'lib/asciidoctor/standoc/ref.rb', line 162

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(t, m, noyr) ⇒ Object



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

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

#docid(t, code) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/asciidoctor/standoc/ref.rb', line 43

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

#docnumber(t, code) ⇒ Object



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

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

#id_and_year(id, year) ⇒ Object



39
40
41
# File 'lib/asciidoctor/standoc/ref.rb', line 39

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

#iso_publisher(t, code) ⇒ Object



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

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

#isorefmatches(xml, m) ⇒ Object



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

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



84
85
86
87
88
89
# File 'lib/asciidoctor/standoc/ref.rb', line 84

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/asciidoctor/standoc/ref.rb', line 91

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])
    m[:fn].nil? or t.note(**plaintxt.merge(type: "Unpublished-Status")) do |p|
      p << "#{m[:fn]}"
    end
  end
end

#isorefmatches3(xml, m) ⇒ Object



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

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



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/asciidoctor/standoc/ref.rb', line 120

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])
    m.names.include?("fn") && m[:fn] and
      t.note(**plaintxt.merge(type: "Unpublished-Status")) { |p| p << "#{m[:fn]}" }
    t.extent **{ type: 'part' } do |e|
      e.referenceFrom "all"
    end
  end
end

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



64
65
66
67
68
69
# File 'lib/asciidoctor/standoc/ref.rb', line 64

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

#mn_code(code) ⇒ Object



241
242
243
# File 'lib/asciidoctor/standoc/ref.rb', line 241

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

#norm_year(yr) ⇒ Object



58
59
60
61
62
# File 'lib/asciidoctor/standoc/ref.rb', line 58

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

#plaintxtObject



15
16
17
# File 'lib/asciidoctor/standoc/ref.rb', line 15

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

#ref_attributes(m) ⇒ Object



19
20
21
# File 'lib/asciidoctor/standoc/ref.rb', line 19

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

#ref_normalise(ref) ⇒ Object



198
199
200
# File 'lib/asciidoctor/standoc/ref.rb', line 198

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

#ref_normalise_no_format(ref) ⇒ Object



202
203
204
# File 'lib/asciidoctor/standoc/ref.rb', line 202

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

#reference1(node, item, xml) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/asciidoctor/standoc/ref.rb', line 232

def reference1(node, item, xml)
  matched, matched2, matched3 = reference1_matches(item)
  if matched3.nil? && matched2.nil? && matched.nil? then 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



225
226
227
228
229
230
# File 'lib/asciidoctor/standoc/ref.rb', line 225

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



182
183
184
185
186
# File 'lib/asciidoctor/standoc/ref.rb', line 182

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



188
189
190
191
192
193
194
195
196
# File 'lib/asciidoctor/standoc/ref.rb', line 188

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



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

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



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

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

#set_date_range(date, text) ⇒ Object



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

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.rb', line 34

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