Module: Asciidoctor::Standoc::Lists
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/standoc/ref.rb,
lib/asciidoctor/standoc/lists.rb
Constant Summary collapse
- ISO_REF =
%r{^<ref\sid="(?<anchor>[^"]+)"> \[(?<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>[^"]+)"> \[(?<code>(ISO|IEC)[^0-9]*\s[0-9-]+):--\]</ref>,?\s? <fn[^>]*>\s*<p>(?<fn>[^\]]+)</p>\s*</fn>,?\s?(?<text>.*)$}xm
- ISO_REF_ALL_PARTS =
%r{^<ref\sid="(?<anchor>[^"]+)"> \[(?<code>(ISO|IEC)[^0-9]*\s[0-9]+)(:(?<year>--|[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>[^"]+)"> \[(?<code>[^\]]+?)([:-](?<year>(19|20)[0-9][0-9]))?\]</ref>,?\s (?<text>.*)$}xm
Instance Method Summary collapse
- #colist(node) ⇒ Object
- #conditional_date(t, m, noyr) ⇒ Object
- #dd(dd, xml_dl) ⇒ Object
- #dlist(node) ⇒ Object
- #docid(t, code) ⇒ Object
- #dt(terms, xml_dl) ⇒ Object
- #fetch_ref(xml, code, year, **opts) ⇒ Object
- #global_bibliocache_name ⇒ Object
- #id_and_year(id, year) ⇒ Object
- #ievcache_name(global) ⇒ Object
- #iso_publisher(t, code) ⇒ Object
- #isorefmatches(xml, m) ⇒ Object
- #isorefmatches2(xml, m) ⇒ Object
- #isorefmatches3(xml, m) ⇒ Object
- #li(xml_ul, item) ⇒ Object
- #local_bibliocache_name(cachename) ⇒ Object
- #olist(node) ⇒ Object
- #olist_style(style) ⇒ Object
- #plaintxt ⇒ Object
- #ref_attributes(m) ⇒ Object
- #ref_normalise(ref) ⇒ Object
- #ref_normalise_no_format(ref) ⇒ Object
- #reference(node) ⇒ Object
- #reference1(node, item, xml) ⇒ Object
- #reference1_matches(item) ⇒ Array<MatchData>
-
#refitem(xml, item, node) ⇒ Object
TODO: alternative where only title is available.
- #refitem_render(xml, m) ⇒ Object
- #set_date_range(date, text) ⇒ Object
- #ulist(node) ⇒ Object
- #use_my_anchor(ref, id) ⇒ Object
Instance Method Details
#colist(node) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/asciidoctor/standoc/lists.rb', line 76 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
76 77 78 79 80 81 82 83 84 |
# File 'lib/asciidoctor/standoc/ref.rb', line 76 def conditional_date(t, m, noyr) m.names.include?("year") and !m[:year].nil? and t.date(**{ type: "published" }) do |d| if noyr then d.on "--" else set_date_range(d, m[:year]) end end end |
#dd(dd, xml_dl) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/asciidoctor/standoc/lists.rb', line 54 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 |
#dlist(node) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/asciidoctor/standoc/lists.rb', line 65 def dlist(node) noko do |xml| xml.dl **id_attr(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
44 45 46 47 |
# File 'lib/asciidoctor/standoc/ref.rb', line 44 def docid(t, code) type, code1 = @bibdb&.docid_type(code) unless /^\[\d+\]$/.match(code) t.docidentifier (code1 || code), **attr_code(type: type) end |
#dt(terms, xml_dl) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/asciidoctor/standoc/lists.rb', line 45 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 |
#fetch_ref(xml, code, year, **opts) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/asciidoctor/standoc/ref.rb', line 102 def fetch_ref(xml, code, year, **opts) return nil if opts[:no_year] hit = @bibdb&.fetch(code, year, opts) return nil if hit.nil? xml.parent.add_child(Utils::smart_render_xml(hit)) xml rescue Algolia::AlgoliaProtocolError nil # Render reference without an Internet connection. end |
#global_bibliocache_name ⇒ Object
197 198 199 |
# File 'lib/asciidoctor/standoc/ref.rb', line 197 def global_bibliocache_name "#{Dir.home}/.relaton/cache" end |
#id_and_year(id, year) ⇒ Object
39 40 41 42 |
# File 'lib/asciidoctor/standoc/ref.rb', line 39 def id_and_year(id, year) return id unless year "#{id}:#{year}" end |
#ievcache_name(global) ⇒ Object
207 208 209 210 |
# File 'lib/asciidoctor/standoc/ref.rb', line 207 def ievcache_name(global) global ? "#{Dir.home}/.iev.pstore" : "#{@filename}.iev.pstore" 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
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/asciidoctor/standoc/ref.rb', line 49 def isorefmatches(xml, m) ref = fetch_ref xml, m[:code], m[:year] return use_my_anchor(ref, m[:anchor]) if ref xml.bibitem **attr_code(ref_attributes(m)) do |t| t.title(**plaintxt) { |i| i << ref_normalise(m[:text]) } docid(t, id_and_year(m[:code], m[:year])) m[:year] and t.date **{ type: "published" } do |d| set_date_range(d, m[:year]) end iso_publisher(t, m[:code]) end end |
#isorefmatches2(xml, m) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/asciidoctor/standoc/ref.rb', line 62 def isorefmatches2(xml, m) ref = fetch_ref xml, m[:code], nil, no_year: true, note: m[:fn] return use_my_anchor(ref, m[:anchor]) if ref xml.bibitem **attr_code(ref_attributes(m)) do |t| t.title(**plaintxt) { |i| i << ref_normalise(m[:text]) } docid(t, id_and_year(m[:code], "--")) t.date **{ type: "published" } do |d| d.on "--" end iso_publisher(t, m[:code]) t.note(**plaintxt) { |p| p << "ISO DATE: #{m[:fn]}" } end end |
#isorefmatches3(xml, m) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/asciidoctor/standoc/ref.rb', line 86 def isorefmatches3(xml, m) hasyr = m.names.include?("year") && m[:year] != "--" noyr = m.names.include?("year") && m[:year] == "--" ref = fetch_ref xml, m[:code], hasyr ? m[:year] : nil, all_parts: true, no_year: noyr return use_my_anchor(ref, m[:anchor]) if ref xml.bibitem(**attr_code(ref_attributes(m))) do |t| t.title(**plaintxt) { |i| i << ref_normalise(m[:text]) } docid(t, id_and_year(m[:code], m[:year]) + " (all parts)") conditional_date(t, m, noyr) iso_publisher(t, m[:code]) t.note(**plaintxt) { |p| p << "ISO DATE: #{m[:fn]}" } if m.names.include?("fn") && m[:fn] t.allparts "true" end end |
#li(xml_ul, item) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/asciidoctor/standoc/lists.rb', line 6 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 |
#local_bibliocache_name(cachename) ⇒ Object
201 202 203 204 205 |
# File 'lib/asciidoctor/standoc/ref.rb', line 201 def local_bibliocache_name(cachename) return nil if cachename.nil? cachename = "relaton" if cachename.empty? "#{cachename}/cache" end |
#olist(node) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/asciidoctor/standoc/lists.rb', line 36 def olist(node) noko do |xml| xml.ol **attr_code(id: Utils::anchor_or_uuid(node), type: olist_style(node.style)) do |xml_ol| node.items.each { |item| li(xml_ol, item) } end end.join("\n") end |
#olist_style(style) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/asciidoctor/standoc/lists.rb', line 28 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 |
#plaintxt ⇒ Object
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
135 136 137 138 139 140 |
# File 'lib/asciidoctor/standoc/ref.rb', line 135 def ref_normalise(ref) ref. # gsub(/ — /, " -- "). gsub(/&amp;/, "&"). gsub(%r{^<em>(.*)</em>}, "\\1") end |
#ref_normalise_no_format(ref) ⇒ Object
142 143 144 145 146 |
# File 'lib/asciidoctor/standoc/ref.rb', line 142 def ref_normalise_no_format(ref) ref. # gsub(/ — /, " -- "). gsub(/&amp;/, "&") end |
#reference(node) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/asciidoctor/standoc/ref.rb', line 189 def reference(node) noko do |xml| node.items.each do |item| reference1(node, item.text, xml) end end.join("\n") end |
#reference1(node, item, xml) ⇒ Object
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/asciidoctor/standoc/ref.rb', line 178 def reference1(node, item, xml) matched, matched2, matched3 = reference1_matches(item) if matched3.nil? && matched2.nil? && matched.nil? refitem(xml, item, node) # elsif fetch_ref(matched3 || matched2 || matched, xml) 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) ⇒ Array<MatchData>
168 169 170 171 172 173 |
# File 'lib/asciidoctor/standoc/ref.rb', line 168 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
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/asciidoctor/standoc/ref.rb', line 122 def refitem(xml, item, node) unless m = NON_ISO_REF.match(item) Utils::warning(node, "no anchor on reference", item) return end unless m[:code] && /^\d+$/.match(m[:code]) ref = fetch_ref xml, m[:code], m.names.include?("year") ? m[:year] : nil, {} return use_my_anchor(ref, m[:anchor]) if ref end refitem_render(xml, m) end |
#refitem_render(xml, m) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/asciidoctor/standoc/ref.rb', line 112 def refitem_render(xml, m) 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 docid(t, /^\d+$/.match(m[:code]) ? "[#{m[:code]}]" : m[:code]) end 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 |
#ulist(node) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/asciidoctor/standoc/lists.rb', line 17 def ulist(node) return reference(node) if in_norm_ref? || in_biblio? noko do |xml| xml.ul **id_attr(node) do |xml_ul| node.items.each do |item| 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 |