module Metanorma::Standoc::Section
Constants
- TERM_REFERENCE_RE
- TERM_REFERENCE_RE_STR
Public Instance Methods
abstract_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 140 def abstract_parse(attrs, xml, node) xml.abstract **attr_code(attrs) do |xml_section| xml_section << node.content end end
acknowledgements_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 186 def acknowledgements_parse(attrs, xml, node) xml.acknowledgements **attr_code(attrs) do |xml_section| xml_section.title { |t| (t << node.title) || @i18n.acknowledgements } content = node.content xml_section << content end end
add_term_source(node, xml_t, seen_xref, match)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 102 def add_term_source(node, xml_t, seen_xref, match) if seen_xref.children[0].name == "concept" xml_t.origin { |o| o << seen_xref.children[0].to_xml } else attrs = term_source_attrs(node, seen_xref) attrs.delete(:text) xml_t.origin **attr_code(attrs) do |o| o << seen_xref.children[0].children.to_xml end end add_term_source_mod(xml_t, match) end
add_term_source_mod(xml_t, match)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 115 def add_term_source_mod(xml_t, match) match[:text] && xml_t.modification do |mod| mod.p { |p| p << match[:text].sub(/^\s+/, "") } end end
annex_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 161 def annex_parse(attrs, xml, node) attrs[:"inline-header"] = node.option? "inline-header" set_obligation(attrs, node) xml.annex **attr_code(attrs) do |xml_section| xml_section.title { |name| name << node.title } xml_section << node.content end end
bibitem_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 23 def bibitem_parse(attrs, xml, node) norm_ref = @norm_ref biblio = @biblio @biblio = false @norm_ref = false ret = clause_parse(attrs, xml, node) @biblio = biblio @norm_ref = norm_ref ret end
biblio_prep(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 45 def biblio_prep(attrs, xml, node) if node.option? "bibitem" bibitem_parse(attrs, xml, node) else node.attr("style") == "bibliography" or @log.add("AsciiDoc Input", node, "Section not marked up as [bibliography]!") nil end end
bibliography_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 12 def bibliography_parse(attrs, xml, node) x = biblio_prep(attrs, xml, node) and return x @biblio = true attrs = attrs.merge(normative: node.attr("normative") || false) xml.references **attr_code(attrs) do |xml_section| xml_section.title { |t| t << node.title } xml_section << node.content end @biblio = false end
clause_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 150 def clause_parse(attrs, xml, node) attrs[:"inline-header"] = node.option? "inline-header" attrs[:bibitem] = true if node.option? "bibitem" attrs[:level] = node.attr("level") set_obligation(attrs, node) xml.send "clause", **attr_code(attrs) do |xml_section| xml_section.title { |n| n << node.title } unless node.title.nil? xml_section << node.content end end
emend_biblio(xml, code, title, usrlbl)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 93 def emend_biblio(xml, code, title, usrlbl) unless xml.at("/bibitem/docidentifier[not(@type = 'DOI')][text()]") @log.add("Bibliography", nil, "ERROR: No document identifier retrieved for #{code}") xml.root << "<docidentifier>#{code}</docidentifier>" end unless xml.at("/bibitem/title[text()]") @log.add("Bibliography", nil, "ERROR: No title retrieved for #{code}") xml.root << "<title>#{title || '(MISSING TITLE)'}</title>" end usrlbl and xml.at("/bibitem/docidentifier").next = "<docidentifier type='metanorma'>#{mn_code(usrlbl)}</docidentifier>" end
extract_termsource_refs(text, node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 130 def extract_termsource_refs(text, node) matched = TERM_REFERENCE_RE.match text matched.nil? and @log.add("AsciiDoc Input", node, "term reference not in expected format:"\ "#{text}") matched end
fetch_ref(xml, code, year, **opts)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 68 def fetch_ref(xml, code, year, **opts) return nil if opts[:no_year] code = code.sub(/^\([^)]+\)/, "") hit = @bibdb&.fetch(code, year, opts) return nil if hit.nil? xml.parent.add_child(smart_render_xml(hit, code, opts)) xml rescue RelatonBib::RequestError @log.add("Bibliography", nil, "Could not retrieve #{code}: "\ "no access to online site") nil end
fetch_ref_async(ref, idx, res)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 83 def fetch_ref_async(ref, idx, res) if ref[:code].nil? || ref[:no_year] || @bibdb.nil? res << [ref, idx, nil] else @bibdb.fetch_async(ref[:code], ref[:year], ref) do |doc| res << [ref, idx, doc] end end end
floating_title(node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 200 def floating_title(node) noko do |xml| xml.floating_title **floating_title_attrs(node) do |xml_t| xml_t << node.title end end.join("\n") end
floating_title_attrs(node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 194 def floating_title_attrs(node) attr_code(id_attr(node).merge(align: node.attr("align"), depth: node.level, type: "floating-title")) end
foreword_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 178 def foreword_parse(attrs, xml, node) xml.foreword **attr_code(attrs) do |xml_section| xml_section.title { |t| t << node.title } content = node.content xml_section << content end end
global_ievcache_name()
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 56 def global_ievcache_name "#{Dir.home}/.iev/cache" end
in_biblio?()
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 4 def in_biblio? @biblio end
in_norm_ref?()
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 8 def in_norm_ref? @norm_ref end
in_terms?()
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 4 def in_terms? @term_def end
indexsect_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 133 def indexsect_parse(attrs, xml, node) xml.indexsect **attr_code(attrs) do |xml_section| xml_section.title { |name| name << node.title } xml_section << node.content end end
init_bib_caches(node)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 125 def init_bib_caches(node) return if @no_isobib global = !@no_isobib_cache && !node.attr("local-cache-only") local = node.attr("local-cache") || node.attr("local-cache-only") local = nil if @no_isobib_cache @bibdb = Relaton::DbCache.init_bib_caches( local_cache: local, flush_caches: node.attr("flush-caches"), global_cache: global, ) end
init_iev_caches(node)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 138 def init_iev_caches(node) unless @no_isobib_cache || @no_isobib node.attr("local-cache-only") or @iev_globalname = global_ievcache_name @iev_localname = local_ievcache_name(node.attr("local-cache") || node.attr("local-cache-only")) if node.attr("flush-caches") FileUtils.rm_f @iev_globalname unless @iev_globalname.nil? FileUtils.rm_f @iev_localname unless @iev_localname.nil? end end # @iev = Iev::Db.new(globalname, localname) unless @no_isobib end
introduction_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 170 def introduction_parse(attrs, xml, node) xml.introduction **attr_code(attrs) do |xml_section| xml_section.title { |t| t << @i18n.introduction } content = node.content xml_section << content end end
local_ievcache_name(cachename)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 60 def local_ievcache_name(cachename) return nil if cachename.nil? cachename += "_iev" unless cachename.empty? cachename = "iev" if cachename.empty? "#{cachename}/cache" end
nonterm_symbols_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 8 def nonterm_symbols_parse(attrs, xml, node) defs = @definitions @definitions = false clause_parse(attrs, xml, node) @definitions = defs end
nonterm_term_def_subclause_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 39 def nonterm_term_def_subclause_parse(attrs, xml, node) defs = @term_def @term_def = false clause_parse(attrs, xml, node) @term_def = defs end
norm_ref_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 34 def norm_ref_parse(attrs, xml, node) x = biblio_prep(attrs, xml, node) and return x @norm_ref = true attrs = attrs.merge(normative: node.attr("normative") || true) xml.references **attr_code(attrs) do |xml_section| xml_section.title { |t| t << node.title } xml_section << node.content end @norm_ref = false end
preamble(node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 121 def preamble(node) noko do |xml| xml.foreword **attr_code(section_attributes(node)) do |xml_abstract| xml_abstract.title do |t| t << (node.blocks[0].title || @i18n.foreword) end content = node.content xml_abstract << content end end.join("\n") end
scope_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 146 def scope_parse(attrs, xml, node) clause_parse(attrs.merge(type: "scope"), xml, node) end
section(node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 71 def section(node) a = section_attributes(node) noko do |xml| case sectiontype(node) when "introduction" then introduction_parse(a, xml, node) when "foreword" then foreword_parse(a, xml, node) when "scope" then scope_parse(a, xml, node) when "normative references" then norm_ref_parse(a, xml, node) when "terms and definitions" @term_def = true term_def_parse(a, xml, node, true) @term_def = false when "symbols and abbreviated terms" symbols_parse(symbols_attrs(node, a), xml, node) when "acknowledgements" acknowledgements_parse(a, xml, node) when "bibliography" bibliography_parse(a, xml, node) else if @term_def then term_def_subclause_parse(a, xml, node) elsif @definitions then symbols_parse(a, xml, node) elsif @norm_ref || (node.attr("style") == "bibliography" && sectiontype(node, false) == "normative references") norm_ref_parse(a, xml, node) elsif @biblio || node.attr("style") == "bibliography" bibliography_parse(a, xml, node) elsif node.attr("style") == "abstract" abstract_parse(a, xml, node) elsif node.attr("style") == "index" indexsect_parse(a, xml, node) elsif node.attr("style") == "appendix" && node.level == 1 annex_parse(a, xml, node) else clause_parse(a, xml, node) end end end.join("\n") end
section_attributes(node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 46 def section_attributes(node) ret = { id: Metanorma::Utils::anchor_or_uuid(node), language: node.attributes["language"], script: node.attributes["script"], number: node.attributes["number"], type: node.attributes["type"], annex: (if (node.attr("style") == "appendix" || node.role == "appendix") && node.level == 1 true end), tag: node&.attr("tag"), "multilingual-rendering": node&.attr("multilingual-rendering"), preface: (if node.role == "preface" || node.attr("style") == "preface" true end) } return ret unless node.attributes["change"] ret.merge(change: node.attributes["change"], path: node.attributes["path"], path_end: node.attributes["path_end"], title: node.attributes["title"]) end
sectiontype(node, level = true)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 18 def sectiontype(node, level = true) ret = sectiontype1(node) ret1 = sectiontype_streamline(ret) return ret1 if ret1 == "symbols and abbreviated terms" return nil unless !level || node.level == 1 return nil if @seen_headers.include? ret @seen_headers << ret ret1 end
sectiontype1(node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 12 def sectiontype1(node) node&.attr("heading")&.downcase || node.title.gsub(%r{<index>.*?</index>}m, "").gsub(/<[^>]+>/, "") .strip.downcase end
sectiontype_streamline(ret)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 29 def sectiontype_streamline(ret) case ret when "terms and definitions", "terms, definitions, symbols and abbreviated terms", "terms, definitions, symbols and abbreviations", "terms, definitions and symbols", "terms, definitions and abbreviations", "terms, definitions and abbreviated terms" "terms and definitions" when "symbols and abbreviated terms", "symbols", "abbreviated terms", "abbreviations" "symbols and abbreviated terms" else ret end end
set_obligation(attrs, node)
click to toggle source
# File lib/metanorma/standoc/section.rb, line 111 def set_obligation(attrs, node) attrs[:obligation] = if node.attributes.has_key?("obligation") node.attr("obligation") elsif node.parent.attributes.has_key?("obligation") node.parent.attr("obligation") else "normative" end end
smart_render_xml(xml, code, opts)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 108 def smart_render_xml(xml, code, opts) xml.respond_to? :to_xml or return nil xml = Nokogiri::XML(xml.to_xml(lang: opts[:lang])) emend_biblio(xml, code, opts[:title], opts[:usrlbl]) xml.xpath("//date").each { |d| Metanorma::Utils::endash_date(d) } xml.traverse do |n| n.text? and n.replace(Metanorma::Utils::smartformat(n.text)) end xml.to_xml.sub(/<\?[^>]+>/, "") end
symbols_attrs(node, attr)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 15 def symbols_attrs(node, attr) case sectiontype1(node) when "symbols" then attr.merge(type: "symbols") when "abbreviated terms", "abbreviations" attr.merge(type: "abbreviated_terms") else attr end end
symbols_parse(attr, xml, node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 25 def symbols_parse(attr, xml, node) node.role == "nonterm" and return nonterm_symbols_parse(attr, xml, node) xml.definitions **attr_code(attr) do |xml_section| xml_section.title { |t| t << node.title } defs = @definitions termdefs = @term_def @definitions = true @term_def = false xml_section << node.content @definitions = defs @term_def = termdefs end end
term_def_parse(attrs, xml, node, _toplevel)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 77 def term_def_parse(attrs, xml, node, _toplevel) xml.terms **attr_code(attrs) do |section| section.title { |t| t << node.title } (s = node.attr("source")) && s.split(",").each do |s1| section.termdocsource(nil, **attr_code(bibitemid: s1)) end section << node.content end end
term_def_subclause_parse(attrs, xml, node)
click to toggle source
subclause contains subclauses
# File lib/metanorma/standoc/terms.rb, line 54 def term_def_subclause_parse(attrs, xml, node) node.role == "nonterm" and return nonterm_term_def_subclause_parse(attrs, xml, node) node.role == "boilerplate" and return terms_boilerplate_parse(attrs, xml, node) st = sectiontype(node, false) return symbols_parse(attrs, xml, node) if @definitions sub = node.find_by(context: :section) { |s| s.level == node.level + 1 } sub.empty? || (return term_def_parse(attrs, xml, node, false)) st == "symbols and abbreviated terms" and return symbols_parse(attrs, xml, node) st == "terms and definitions" and return clause_parse(attrs, xml, node) term_def_subclause_parse1(attrs, xml, node) end
term_def_subclause_parse1(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 70 def term_def_subclause_parse1(attrs, xml, node) xml.term **attr_code(attrs) do |xml_section| term_designation(xml_section, node, "preferred", node.title) xml_section << node.content end end
term_designation(xml, _node, tag, text)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 87 def term_designation(xml, _node, tag, text) xml.send tag do |p| p.expression do |e| e.name { |name| name << text } end end end
term_source_attrs(_node, seen_xref)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 95 def term_source_attrs(_node, seen_xref) { case: seen_xref.children[0]["case"], droploc: seen_xref.children[0]["droploc"], bibitemid: seen_xref.children[0]["target"], format: seen_xref.children[0]["format"], type: "inline" } end
termdefinition(node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 151 def termdefinition(node) noko do |xml| xml.definition do |d| d << node.content end end.join("\n") end
terms_boilerplate_parse(attrs, xml, node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 46 def terms_boilerplate_parse(attrs, xml, node) defs = @term_def @term_def = false clause_parse(attrs.merge(type: "boilerplate"), xml, node) @term_def = defs end
termsource(node)
click to toggle source
# File lib/metanorma/standoc/terms.rb, line 138 def termsource(node) matched = extract_termsource_refs(node.content, node) || return noko do |xml| status = node.attr("status") || (matched[:text] ? "modified" : "identical") attrs = { status: status, type: node.attr("type") || "authoritative" } xml.termsource **attrs do |xml_t| seen_xref = Nokogiri::XML.fragment(matched[:xref]) add_term_source(node, xml_t, seen_xref, matched) end end.join("\n") end
use_retrieved_relaton(item, xml)
click to toggle source
# File lib/metanorma/standoc/ref_sect.rb, line 119 def use_retrieved_relaton(item, xml) xml.parent.add_child(smart_render_xml(item[:doc], item[:ref][:code], item[:ref])) use_my_anchor(xml, item[:ref][:match][:anchor], item.dig(:ref, :analyse_code, :hidden)) end