lib/asciidoctor/itu/cleanup.rb in metanorma-itu-1.1.3 vs lib/asciidoctor/itu/cleanup.rb in metanorma-itu-1.2.0
- old
+ new
@@ -2,10 +2,11 @@
module ITU
class Converter < Standoc::Converter
def sections_cleanup(x)
super
insert_missing_sections(x) unless @no_insert_missing_sections
+ insert_empty_clauses(x)
end
def table_cleanup(xmldoc)
super
xmldoc.xpath("//thead/tr[1]/th | //thead/tr[1]/td").each do |t|
@@ -20,64 +21,81 @@
insert_terms(x)
insert_symbols(x)
insert_conventions(x)
end
+ def add_id
+ %(id="_#{UUIDTools::UUID.random_create}")
+ end
+
def insert_scope(x)
x.at("./*/sections") or
x.at("./*/preface | ./*/boilerplate | ./*/bibdata").next =
"<sections><sentinel/></sections>"
x.at("./*/sections/*") or x.at("./*/sections") << "<sentinel/>"
ins = x.at("//sections").elements.first
- unless x.at("//sections/clause/title[text() = 'Scope']")
- ins.previous = "<clause><title>Scope</title><p>"\
- "#{@labels['clause_empty']}</p></clause>"
+ unless x.at("//sections/clause[@type = 'scope']")
+ ins.previous =
+ "<clause type='scope' #{add_id}><title>#{@i18n.scope}</title><p>"\
+ "#{@i18n.clause_empty}</p></clause>"
end
x&.at("//sentinel")&.remove
end
def insert_norm_ref(x)
x.at("//bibliography") or
x.at("./*/annex[last()] | ./*/sections").next =
"<bibliography><sentinel/></bibliography>"
ins = x.at("//bibliography").elements.first
unless x.at("//bibliography/references[@normative = 'true']")
- #ins.previous = "<references normative='true'><title>References</title><p>"\
- # "#{@labels['clause_empty']}</p></references>"
- ins.previous = "<references normative='true'><title>References</title>"\
- "</references>"
+ ins.previous = "<references #{add_id} normative='true'>"\
+ "<title>#{@i18n.normref}</title></references>"
end
x&.at("//sentinel")&.remove
end
def insert_terms(x)
- ins = x.at("//sections/clause/title[text() = 'Scope']/..")
+ ins = x.at("//sections/clause[@type = 'scope']")
unless x.at("//sections//terms")
- ins.next = "<terms><title>Definitions</title><p>"\
- "#{@labels['clause_empty']}</p></terms>"
+ ins.next = "<terms #{add_id}><title>#{@i18n.termsdef}</title></terms>"
end
end
def insert_symbols(x)
ins = x.at("//sections/terms") ||
x.at("//sections/clause[descendant::terms]")
unless x.at("//sections//definitions")
- ins.next = "<definitions><title>Abbreviations and acronyms</title><p>"\
- "#{@labels['clause_empty']}</p></definitions>"
+ ins.next = "<definitions #{add_id}>"\
+ "<title>#{@i18n.symbolsabbrev}</title></definitions>"
end
end
def insert_conventions(x)
ins = x.at("//sections//definitions") ||
x.at("//sections/clause[descendant::definitions]")
- unless x.at("//sections/clause/title[text() = 'Conventions']")
- ins.next = "<clause id='_#{UUIDTools::UUID.random_create}'>"\
- "<title>Conventions</title><p>"\
- "#{@labels['clause_empty']}</p></clause>"
+ unless x.at("//sections/clause[@type = 'conventions']")
+ ins.next = "<clause #{add_id} type='conventions'>"\
+ "<title>#{@i18n.conventions}</title><p>"\
+ "#{@i18n.clause_empty}</p></clause>"
end
end
+ def insert_empty_clauses(x)
+ x.xpath("//terms[not(./term)][not(.//terms)]").each do |c|
+ insert_empty_clauses1(c, @i18n.clause_empty)
+ end
+ x.xpath("//definitions[not(./dl)]").each do |c|
+ insert_empty_clauses1(c, @i18n.clause_empty)
+ end
+ end
+
+ def insert_empty_clauses1(c, text)
+ c.at("./p") and return
+ ins = c.at("./title") or return
+ ins.next = "<p>#{text}</p>"
+ end
+
def cleanup(xmldoc)
symbols_cleanup(xmldoc)
super
obligations_cleanup(xmldoc)
xmldoc
@@ -92,28 +110,50 @@
gsub(/\u201c|\u201d|\u201e|\u201f/, '"'), :basic))
end
xmldoc
end
- def termdef_cleanup(xmldoc)
- xmldoc.xpath("//term/preferred").each do |p|
- if ["terms defined elsewhere",
- "terms defined in this recommendation"].include? p.text.downcase
- p.name = "title"
- p.parent.name = "terms"
- end
- end
- super
+ def termdef_boilerplate_cleanup(xmldoc)
end
- def termdef_boilerplate_cleanup(xmldoc)
+ def terms_extract(div)
+ internal = div.at("./terms[@type = 'internal']/title")
+ external = div.at("./terms[@type = 'external']/title")
+ [internal, external]
end
+ def term_defs_boilerplate(div, source, term, preface, isodoc)
+ internal, external = terms_extract(div.parent)
+ internal&.next_element&.name == "term" and
+ internal.next = "<p>#{@i18n.internal_terms_boilerplate}</p>"
+ internal and internal&.next_element == nil and
+ internal.next = "<p>#{@i18n.no_terms_boilerplate}</p>"
+ external&.next_element&.name == "term" and
+ external.next = "<p>#{@i18n.external_terms_boilerplate}</p>"
+ external and external&.next_element == nil and
+ external.next = "<p>#{@i18n.no_terms_boilerplate}</p>"
+ !internal and !external and
+ %w(term terms).include? div&.next_element&.name and
+ div.next = "<p>#{@i18n.term_def_boilerplate}</p>"
+ end
+
+ def section_names_terms_cleanup(x)
+ super
+ replace_title(
+ x, "//terms[@type = 'internal'] | "\
+ "//clause[./terms[@type = 'internal']][not(./terms[@type = 'external'])]",
+ @i18n&.internal_termsdef)
+ replace_title(
+ x, "//terms[@type = 'external'] | "\
+ "//clause[./terms[@type = 'external']][not(./terms[@type = 'internal'])]",
+ @i18n&.external_termsdef)
+ end
+
def symbols_cleanup(xmldoc)
sym = xmldoc.at("//definitions/title")
sym and sym&.next_element&.name == "dl" and
- sym.next = "<p>#{@symbols_boilerplate}</p>"
+ sym.next = "<p>#{@i18n.symbols_boilerplate}</p>"
end
PUBLISHER = "./contributor[role/@type = 'publisher']/organization".freeze
def pub_class(bib)
@@ -157,16 +197,9 @@
def biblio_reorder(xmldoc)
xmldoc.xpath("//references").each do |r|
biblio_reorder1(r)
end
- end
-
- def normref_cleanup(xmldoc)
- super
- r = xmldoc.at(NORM_REF) || return
- title = r.at("./title") and
- title.content = "References"
end
end
end
end