Skip to content

Commit

Permalink
use Asciidoc for termdef boilerplate: metanorma/metanorma-iec#339
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Aug 29, 2024
1 parent 51b76df commit 2d356ea
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 121 deletions.
1 change: 1 addition & 0 deletions Gemfile.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "fix/yaml-no-xml"
2 changes: 1 addition & 1 deletion lib/metanorma/standoc/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def cleanup(xmldoc)
normref_cleanup(xmldoc)
biblio_cleanup(xmldoc)
reference_names(xmldoc)
boilerplate_cleanup(xmldoc) # feeds: xref_cleanup
asciimath_cleanup(xmldoc) # feeds: mathml_cleanup, termdef_cleanup,
# symbols_cleanup
symbols_cleanup(xmldoc) # feeds: termdef_cleanup
Expand All @@ -79,7 +80,6 @@ def cleanup(xmldoc)
ext_dochistory_cleanup(xmldoc) # feeds: bibdata_cleanup
bibdata_cleanup(xmldoc)
svgmap_cleanup(xmldoc) # feeds: img_cleanup
boilerplate_cleanup(xmldoc)
toc_cleanup(xmldoc)
smartquotes_cleanup(xmldoc)
linebreak_cleanup(xmldoc)
Expand Down
101 changes: 9 additions & 92 deletions lib/metanorma/standoc/cleanup_boilerplate.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
require_relative "cleanup_terms_boilerplate"

module Metanorma
module Standoc
module Cleanup
def external_terms_boilerplate(sources)
e = @i18n.external_terms_boilerplate
@i18n.l10n(e.gsub(/%(?=\p{P}|\p{Z}|$)/, sources || "???"),
@lang, @script, @locale)
end

def internal_external_terms_boilerplate(sources)
e = @i18n.internal_external_terms_boilerplate
@i18n.l10n(e.gsub(/%(?=\p{P}|\p{Z}|$)/, sources || "??"),
@lang, @script)
end

def term_defs_boilerplate(div, source, term, _preface, isodoc)
a = @i18n.term_def_boilerplate and div.next = a
source.each do |s|
@anchors[s["bibitemid"]] or
@log.add("Crossreferences", nil,
"term source #{s['bibitemid']} not referenced", severity: 1)
end
a = if source.empty? && term.nil? then @i18n.no_terms_boilerplate
else term_defs_boilerplate_cont(source, term, isodoc)
end and div.next = a
end

def term_defs_boilerplate_cont(src, term, isodoc)
sources = isodoc.sentence_join(src.map do |s|
%{<eref bibitemid="#{s['bibitemid']}"/>}
end)
if src.empty? then @i18n.internal_terms_boilerplate
elsif term.nil? then external_terms_boilerplate(sources)
else
internal_external_terms_boilerplate(sources)
end
end

def norm_ref_preface(ref)
ins = norm_ref_boilerplate_insert_location(ref)
ins2 = norm_ref_process_boilerplate_note(ref)
Expand Down Expand Up @@ -69,16 +36,15 @@ def norm_ref_boilerplate_insert_location(ref)
ref.at("./title")
end

TERM_CLAUSE =
"//sections//terms[not(.//ancestor::clause[@type = 'terms'])] | " \
"//sections/clause[descendant::terms][@type = 'terms'] | " \
"//sections/clause[not(@type = 'terms')]//terms".freeze

NORM_REF =
"//bibliography/references[@normative = 'true'][not(@hidden)] | " \
"//bibliography/clause[.//references[@normative = 'true']]".freeze

def boilerplate_isodoc(xmldoc)
# prevent infinite recursion of asciidoc boilerplate processing
# in termdef_boilerplate_insert and initial_boilerplate
xmldoc.at("//metanorma-extension/semantic-metadata/" \
"headless[text() = 'true']") and return nil
x = xmldoc.dup
x.root.add_namespace(nil, self.class::XML_NAMESPACE)
xml = Nokogiri::XML(x.to_xml)
Expand All @@ -87,14 +53,6 @@ def boilerplate_isodoc(xmldoc)
@isodoc
end

def termdef_boilerplate_cleanup(xmldoc)
# termdef_remove_initial_paras(xmldoc)
end

def termdef_remove_initial_paras(xmldoc)
xmldoc.xpath("//terms/p | //terms/ul").each(&:remove)
end

def unwrap_boilerplate_clauses(xmldoc, xpath)
xmldoc.xpath(xpath).each do |f|
f.xpath(".//clause[@type = 'boilerplate'] | " \
Expand All @@ -105,51 +63,9 @@ def unwrap_boilerplate_clauses(xmldoc, xpath)
end
end

def termdef_boilerplate_insert(xmldoc, isodoc, once = false)
if once
f = termdef_boilerplate_insert_location(xmldoc) and
termdef_boilerplate_insert1(f, xmldoc, isodoc)
else
xmldoc.xpath(self.class::TERM_CLAUSE).each do |f|
termdef_boilerplate_insert1(f, xmldoc, isodoc)
end
end
end

def termdef_boilerplate_insert_location(xmldoc)
f = xmldoc.at(self.class::TERM_CLAUSE)
root = xmldoc.at("//sections/terms | //sections/clause[@type = 'terms']")
if f && root && f["id"] != root["id"]
f = termdef_boilerplate_climb_up(f, root)
elsif !f && root then f = root
end
f
end

def termdef_boilerplate_climb_up(clause, container)
container.at(".//*[@id = '#{clause['id']}']") or return clause
while (n = clause.parent)
n.at(".//definitions") and break
clause = n
n["id"] == container["id"] and break
end
clause
end

def termdef_boilerplate_insert1(sect, xmldoc, isodoc)
ins = sect.at("./title")
if (ins2 = sect.at("./clause[@type = 'boilerplate'] | " \
"./note[@type = 'boilerplate']"))
ins2.text.strip.downcase == "(default)" or return
ins2.children = " "
ins = ins2.children.first
end
term_defs_boilerplate(ins, xmldoc.xpath(".//termdocsource"),
sect.at(".//term"), sect.at(".//p"), isodoc)
end

def boilerplate_cleanup(xmldoc)
isodoc = boilerplate_isodoc(xmldoc)
isodoc = boilerplate_isodoc(xmldoc) or return
term_children_cleanup(xmldoc)
termdef_boilerplate_cleanup(xmldoc)
termdef_boilerplate_insert(xmldoc, isodoc)
unwrap_boilerplate_clauses(xmldoc, self.class::TERM_CLAUSE)
Expand Down Expand Up @@ -245,6 +161,7 @@ def boilerplate_xml_cleanup(xml)
/^_\d+$/.match?(n["id"]) and
n["id"] = "_#{UUIDTools::UUID.random_create}"
end
xml
end

def boilerplate_top_elements(xml)
Expand Down
2 changes: 1 addition & 1 deletion lib/metanorma/standoc/cleanup_terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def termdef_cleanup(xmldoc)
termdefinition_cleanup(xmldoc)
termdomain1_cleanup(xmldoc)
termnote_example_cleanup(xmldoc)
term_children_cleanup(xmldoc)
#term_children_cleanup(xmldoc)
end

def term_index_cleanup(xmldoc)
Expand Down
106 changes: 106 additions & 0 deletions lib/metanorma/standoc/cleanup_terms_boilerplate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module Metanorma
module Standoc
module Cleanup
def external_terms_boilerplate(sources)
e = @i18n.external_terms_boilerplate
e.gsub(/%(?=\p{P}|\p{Z}|$)/, sources || "???")
end

def internal_external_terms_boilerplate(sources)
e = @i18n.internal_external_terms_boilerplate
e.gsub(/%(?=\p{P}|\p{Z}|$)/, sources || "??")
end

def boilerplate_snippet_convert(adoc)
ret = boilerplate_xml_cleanup(adoc2xml(adoc, backend.to_sym))
@i18n.l10n(ret.children.to_xml, @lang, @script)
end

def term_defs_boilerplate(div, source, term, _preface, isodoc)
verify_term_defs_source(source)
a = @i18n.term_def_boilerplate and
div.next = boilerplate_snippet_convert(a)
a = if source.empty? && term.nil? then @i18n.no_terms_boilerplate
else term_defs_boilerplate_cont(source, term, isodoc)
end
a and div.next = boilerplate_snippet_convert(a)
end

def verify_term_defs_source(source)
source.each do |s|
@anchors[s["bibitemid"]] or
@log.add("Crossreferences", nil,
"term source #{s['bibitemid']} not referenced",
severity: 1)
end
end

def term_defs_boilerplate_cont(src, term, isodoc)
sources = isodoc.sentence_join(src.map do |s|
%{<<#{s['bibitemid']}>>}
end)
if src.empty? then @i18n.internal_terms_boilerplate
elsif term.nil? then external_terms_boilerplate(sources)
else
internal_external_terms_boilerplate(sources)
end
end

TERM_CLAUSE =
"//sections//terms[not(.//ancestor::clause[@type = 'terms'])] | " \
"//sections/clause[descendant::terms][@type = 'terms'] | " \
"//sections/clause[not(@type = 'terms')]//terms".freeze

def termdef_boilerplate_cleanup(xmldoc)
# termdef_remove_initial_paras(xmldoc)
end

def termdef_remove_initial_paras(xmldoc)
xmldoc.xpath("//terms/p | //terms/ul").each(&:remove)
end

def termdef_boilerplate_insert(xmldoc, isodoc, once = false)
if once
f = termdef_boilerplate_insert_location(xmldoc) and
termdef_boilerplate_insert1(f, xmldoc, isodoc)
else
xmldoc.xpath(self.class::TERM_CLAUSE).each do |f|
termdef_boilerplate_insert1(f, xmldoc, isodoc)
end
end
end

def termdef_boilerplate_insert_location(xmldoc)
f = xmldoc.at(self.class::TERM_CLAUSE)
root = xmldoc.at("//sections/terms | //sections/clause[@type = 'terms']")
if f && root && f["id"] != root["id"]
f = termdef_boilerplate_climb_up(f, root)
elsif !f && root then f = root
end
f
end

def termdef_boilerplate_climb_up(clause, container)
container.at(".//*[@id = '#{clause['id']}']") or return clause
while (n = clause.parent)
n.at(".//definitions") and break
clause = n
n["id"] == container["id"] and break
end
clause
end

def termdef_boilerplate_insert1(sect, xmldoc, isodoc)
ins = sect.at("./title")
if (ins2 = sect.at("./clause[@type = 'boilerplate'] | " \
"./note[@type = 'boilerplate']"))
ins2.text.strip.downcase == "(default)" or return
ins2.children = " "
ins = ins2.children.first
end
term_defs_boilerplate(ins, xmldoc.xpath(".//termdocsource"),
sect.at(".//term"), sect.at(".//p"), isodoc)
end
end
end
end
Loading

0 comments on commit 2d356ea

Please sign in to comment.