Module: Asciidoctor::Standoc::Lists

Included in:
Converter
Defined in:
lib/asciidoctor/standoc/ref.rb,
lib/asciidoctor/standoc/lists.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]))?\]</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 ]? | identifier



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

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

#analyse_ref_nofetch(ret) ⇒ Object



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

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

#analyse_ref_numeric(ret) ⇒ Object



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

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

#analyse_ref_repo_path(ret) ⇒ Object



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

def analyse_ref_repo_path(ret)
   if m = /^(?<type>repo|path):\((?<key>[^,]+),(?<id>.+)\)$/.match(ret[:id])
    ret[:id] = m[:id]
    ret[:type] = m[:type]
    ret[:key] = m[:key]
    ret[:nofetch] = true
  end
   ret
end

#colist(node) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/asciidoctor/standoc/lists.rb', line 104

def colist(node)
  noko do |xml|
    node.items.each_with_index do |item, i|
      xml.annotation **attr_code(id: i + 1) do |xml_li|
        xml_li.p { |p| p << item.text }
      end
    end
  end.join("\n")
end

#conditional_date(t, m, noyr) ⇒ Object



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

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

#dd(dd, xml_dl) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/asciidoctor/standoc/lists.rb', line 78

def dd(dd, xml_dl)
  if dd.nil?
    xml_dl.dd
    return
  end
  xml_dl.dd do |xml_dd|
    xml_dd.p { |t| t << dd.text } if dd.text?
    xml_dd << dd.content if dd.blocks?
  end
end

#dl_attrs(node) ⇒ Object



89
90
91
# File 'lib/asciidoctor/standoc/lists.rb', line 89

def dl_attrs(node)
  attr_code(id_attr(node).merge(keep_attrs(node)))
end

#dlist(node) ⇒ Object



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

def dlist(node)
  noko do |xml|
    xml.dl **dl_attrs(node) do |xml_dl|
      node.items.each do |terms, dd|
        dt(terms, xml_dl)
        dd(dd, xml_dl)
      end
    end
  end.join("\n")
end

#docid(t, code) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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



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

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

#dt(terms, xml_dl) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/asciidoctor/standoc/lists.rb', line 69

def dt(terms, xml_dl)
  terms.each_with_index do |dt, idx|
    xml_dl.dt { |xml_dt| xml_dt << dt.text }
    if idx < terms.size - 1
      xml_dl.dd
    end
  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



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

def isorefmatches(xml, m)
  yr = norm_year(m[:year])
  ref = fetch_ref xml, m[:code], yr, title: m[:text], usrlbl: m[:usrlbl]
  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



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

def isorefmatches2(xml, m)
  ref = fetch_ref xml, m[:code], nil, no_year: true, note: m[:fn],
    title: m[:text], usrlbl: m[:usrlbl]
  return use_my_anchor(ref, m[:anchor]) if ref
  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: "ISO DATE")) do |p|
      p << "#{m[:fn]}"
    end
  end
end

#isorefmatches3(xml, m) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/asciidoctor/standoc/ref.rb', line 107

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

  xml.bibitem(**attr_code(ref_attributes(m))) do |t|
    isorefrender1(t, m, yr, " (all parts)")
    conditional_date(t, m, noyr)
    iso_publisher(t, m[:code])
    m.names.include?("fn") && m[:fn] and
      t.note(**plaintxt.merge(type: "ISO DATE")) { |p| p << "#{m[:fn]}" }
    t.extent **{ type: 'part' } do |e|
      e.referenceFrom "all"
    end
  end
end

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



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

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

#li(xml_ul, item) ⇒ Object



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

def li(xml_ul, item)
  xml_ul.li do |xml_li|
    if item.blocks?
      xml_li.p(**id_attr(item)) { |t| t << item.text }
      xml_li << item.content
    else
      xml_li.p(**id_attr(item)) { |p| p << item.text }
    end
  end
end

#mn_code(code) ⇒ Object



253
254
255
# File 'lib/asciidoctor/standoc/ref.rb', line 253

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

#norm_year(yr) ⇒ Object



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

def norm_year(yr)
  /^\&\#821[12];$/.match(yr) ? "--" : yr
end

#ol_attrs(node) ⇒ Object



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

def ol_attrs(node)
  attr_code(keep_attrs(node).merge(id: Utils::anchor_or_uuid(node),
            type: olist_style(node.style)))
end

#olist(node) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/asciidoctor/standoc/lists.rb', line 61

def olist(node)
  noko do |xml|
    xml.ol **ol_attrs(node) do |xml_ol|
      node.items.each { |item| li(xml_ol, item) }
    end
  end.join("\n")
end

#olist_style(style) ⇒ Object



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

def olist_style(style)
  return "alphabet" if style == "loweralpha"
  return "roman" if style == "lowerroman"
  return "roman_upper" if style == "upperroman"
  return "alphabet_upper" if style == "upperalpha"
  style
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



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

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

#ref_normalise_no_format(ref) ⇒ Object



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

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

#reference(node) ⇒ Object



247
248
249
250
251
# File 'lib/asciidoctor/standoc/ref.rb', line 247

def reference(node)
  noko do |xml|
    node.items.each { |item| reference1(node, item.text, xml) }
  end.join
end

#reference1(node, item, xml) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/asciidoctor/standoc/ref.rb', line 237

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



230
231
232
233
234
235
# File 'lib/asciidoctor/standoc/ref.rb', line 230

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/asciidoctor/standoc/ref.rb', line 189

def refitem(xml, item, node)
  unless m = NON_ISO_REF.match(item)
    @log.add("AsciiDoc Input", node, "#{MALFORMED_REF}: #{item}")
    return
  end
  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]
    return use_my_anchor(ref, m[:anchor]) if ref
  end
  refitem_render(xml, m, code)
end

#refitem_render(xml, m, code) ⇒ Object



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

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



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

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

#ul_attrs(node) ⇒ Object



26
27
28
# File 'lib/asciidoctor/standoc/lists.rb', line 26

def ul_attrs(node)
  attr_code(id_attr(node).merge(keep_attrs(node)))
end

#ul_li(xml_ul, item) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/asciidoctor/standoc/lists.rb', line 15

def ul_li(xml_ul, item)
  xml_ul.li **ul_li_attrs(item) do |xml_li|
    if item.blocks?
      xml_li.p(**id_attr(item)) { |t| t << item.text }
      xml_li << item.content
    else
      xml_li.p(**id_attr(item)) { |p| p << item.text }
    end
  end
end

#ul_li_attrs(node) ⇒ Object



30
31
32
33
34
35
# File 'lib/asciidoctor/standoc/lists.rb', line 30

def ul_li_attrs(node)
  attr_code(
    uncheckedcheckbox: node.attr?("checkbox") ? !node.attr?("checked") : nil,
    checkedcheckbox: node.attr?("checkbox") ? node.attr?("checked") : nil,
  )
end

#ulist(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/asciidoctor/standoc/lists.rb', line 37

def ulist(node)
  return reference(node) if in_norm_ref? || in_biblio?
  noko do |xml|
    xml.ul **ul_attrs(node) do |xml_ul|
      node.items.each do |item|
        ul_li(xml_ul, item)
      end
    end
  end.join("\n")
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