require "pp" module Asciidoctor module ISO module Lists 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 def plaintxt { format: "text/plain" } end def ref_attributes(m) { id: m[:anchor], type: "standard" } end def set_date_range(date, text) matched = /^(?[0-9]+)(-+(?[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 def use_my_anchor(ref, id) ref.parent.elements.last["id"] = id ref end 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]) } t.docidentifier m[:code] m[:year] and t.date **{ type: "published" } do |d| set_date_range(d, m[:year]) end iso_publisher(t, m[:code]) end end 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]) } t.docidentifier 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 def isorefmatches3(xml, m) hasyr = m.names.include?("year") ref = fetch_ref xml, m[:code], hasyr ? m[:year] : nil, all_parts: true 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]) } t.docidentifier "#{m[:code]}" hasyr and t.date(**{ type: "published" }) { |d| set_date_range(d, m[:year]) } iso_publisher(t, m[:code]) t.allParts "true" end end def fetch_ref(xml, code, year, **opts) hit = @bibdb&.fetch(code, year, opts) return nil if hit.nil? xml.parent.add_child(hit.to_xml) xml rescue Algolia::AlgoliaProtocolError nil # Render reference without an Internet connection. end 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 t.docidentifier(/^\d+$/.match(m[:code]) ? "[#{m[:code]}]" : m[:code]) end end # TODO: alternative where only title is available 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 def ref_normalise(ref) ref. # gsub(/ — /, " -- "). gsub(/&amp;/, "&"). gsub(%r{^(.*)}, "\\1") end def ref_normalise_no_format(ref) ref. # gsub(/ — /, " -- "). gsub(/&amp;/, "&") end ISO_REF = %r{^[^"]+)"> \[(?(ISO|IEC)[^0-9]*\s[0-9-]+|IEV) (:(?[0-9][0-9-]+))?\],?\s (?.*)$}xm ISO_REF_NO_YEAR = %r{^[^"]+)"> \[(?(ISO|IEC)[^0-9]*\s[0-9-]+):--\],?\s? ]*>\s*

(?[^\]]+)

\s*,?\s?(?.*)$}xm ISO_REF_ALL_PARTS = %r{^[^"]+)"> \[(?(ISO|IEC)[^0-9]*\s[0-9]+)(:(?[0-9][0-9-]+))?\s \(all\sparts\)\],?\s (?.*)$}xm NON_ISO_REF = %r{^[^"]+)"> \[(?[^\]]+?)([:-](?(19|20)[0-9][0-9]))?\],?\s (?.*)$}xm # @param item [String] # @return [Array] 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 # @param node [Asciidoctor::List] # @param item [String] # @param xml [Nokogiri::XML::Builder] 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 def reference(node) noko do |xml| node.items.each do |item| reference1(node, item.text, xml) end end.join("\n") end def bibliocache_name(global) global ? "#{Dir.home}/.relaton-bib.pstore" : "#{@filename}.relaton.pstore" end def ievcache_name(global) global ? "#{Dir.home}/.iev.pstore" : "#{@filename}.iev.pstore" end end end end