require "date"
require "htmlentities"
require "json"
require "open-uri"
require "mathml2asciimath"
module Asciidoctor
module Standoc
module Cleanup
def make_preface(xml, s)
if xml.at("//foreword | //introduction | //acknowledgements | "\
"//*[@preface]")
preface = s.add_previous_sibling("").first
f = xml.at("//foreword") and preface.add_child f.remove
f = xml.at("//introduction") and preface.add_child f.remove
move_clauses_into_preface(xml, preface)
f = xml.at("//acknowledgements") and preface.add_child f.remove
end
make_abstract(xml, s)
end
def move_clauses_into_preface(xml, preface)
xml.xpath("//*[@preface]").each do |c|
c.delete("preface")
preface.add_child c.remove
end
end
def make_abstract(xml, s)
if xml.at("//abstract[not(ancestor::bibitem)]")
preface = s.at("//preface") ||
s.add_previous_sibling("").first
abstract = xml.at("//abstract[not(ancestor::bibitem)]").remove
preface.prepend_child abstract.remove
bibabstract = bibabstract_location(xml)
dupabstract = abstract.dup
dupabstract.traverse { |n| n.remove_attribute("id") }
dupabstract.remove_attribute("language")
dupabstract.remove_attribute("script")
dupabstract&.at("./title")&.remove
bibabstract.next = dupabstract
end
end
def bibabstract_location(xml)
xml.at("//bibdata/script") || xml.at("//bibdata/language") ||
xml.at("//bibdata/contributor[not(following-sibling::contributor)]") ||
xml.at("//bibdata/date[not(following-sibling::date)]") ||
xml.at("//docnumber") ||
xml.at("//bibdata/docidentifier"\
"[not(following-sibling::docidentifier)]") ||
xml.at("//bibdata/uri[not(following-sibling::uri)]") ||
xml.at("//bibdata/title[not(following-sibling::title)]")
end
def make_bibliography(xml, s)
if xml.at("//sections/references")
biblio = s.add_next_sibling("").first
xml.xpath("//sections/references").each do |r|
biblio.add_child r.remove
end
end
end
def make_indexsect(xml, s)
xml.xpath("//sections/indexsect").reverse_each { |r| s.next = r.remove }
end
def sections_order_cleanup(xml)
s = xml.at("//sections")
make_preface(xml, s)
make_annexes(xml)
make_indexsect(xml, s)
make_bibliography(xml, s)
xml.xpath("//sections/annex").reverse_each { |r| s.next = r.remove }
end
def make_annexes(xml)
xml.xpath("//*[@annex]").each do |y|
y.delete("annex")
next if y.name == "annex" || !y.ancestors("annex").empty?
y.wrap("")
y.parent["id"] = "_#{UUIDTools::UUID.random_create}"
y.parent["obligation"] = y["obligation"]
y.parent["language"] = y["language"]
y.parent["script"] = y["script"]
end
end
def maxlevel(xml)
max = 5
xml.xpath("//clause[@level]").each do |c|
max = c["level"].to_i if max < c["level"].to_i
end
max
end
def sections_level_cleanup(xml)
m = maxlevel(xml)
return if m < 6
m.downto(6).each do |l|
xml.xpath("//clause[@level = '#{l}']").each do |c|
c.delete("level")
c.previous_element << c.remove
end
end
end
def sections_cleanup(xml)
sections_order_cleanup(xml)
sections_level_cleanup(xml)
sections_names_cleanup(xml)
change_clauses(xml)
end
def obligations_cleanup(xml)
obligations_cleanup_info(xml)
obligations_cleanup_norm(xml)
obligations_cleanup_inherit(xml)
end
def obligations_cleanup_info(xml)
s = xml.at("//foreword") and s["obligation"] = "informative"
s = xml.at("//introduction") and s["obligation"] = "informative"
s = xml.at("//acknowledgements") and s["obligation"] = "informative"
xml.xpath("//references").each { |r| r["obligation"] = "informative" }
xml.xpath("//preface//clause").each do |r|
r["obligation"] = "informative"
end
end
def obligations_cleanup_norm(xml)
s = xml.at("//clause[@type = 'scope']") and
s["obligation"] = "normative"
xml.xpath("//terms").each { |r| r["obligation"] = "normative" }
xml.xpath("//definitions").each { |r| r["obligation"] = "normative" }
end
def obligations_cleanup_inherit(xml)
xml.xpath("//annex | //clause[not(ancestor::boilerplate)]").each do |r|
r["obligation"] = "normative" unless r["obligation"]
end
xml.xpath(Utils::SUBCLAUSE_XPATH).each do |r|
o = r&.at("./ancestor::*/@obligation")&.text and r["obligation"] = o
end
end
def clausebefore_cleanup(xmldoc)
preface_clausebefore_cleanup(xmldoc)
sections_clausebefore_cleanup(xmldoc)
end
def preface_clausebefore_cleanup(xmldoc)
return unless xmldoc.at("//preface")
unless ins = xmldoc.at("//preface").children.first
xmldoc.at("//preface") << " "
ins = xmldoc.at("//preface").children.first
end
xmldoc.xpath("//preface//*[@beforeclauses = 'true']").each do |x|
x.delete("beforeclauses")
ins.previous = x.remove
end
end
def sections_clausebefore_cleanup(xmldoc)
return unless xmldoc.at("//sections")
unless ins = xmldoc.at("//sections").children.first
xmldoc.at("//sections") << " "
ins = xmldoc.at("//sections").children.first
end
xmldoc.xpath("//sections//*[@beforeclauses = 'true']").each do |x|
x.delete("beforeclauses")
ins.previous = x.remove
end
end
def get_or_make_title(node)
unless node.at("./title")
if node.children.empty?
node << "
"
else
node.children.first.previous = ""
end
end
node.at("./title")
end
def replace_title(doc, xpath, text, first = false)
return unless text
doc.xpath(xpath).each_with_index do |node, i|
next if first && !i.zero?
title = get_or_make_title(node)
fn = title.xpath("./fn")
fn.each(&:remove)
title.content = text
fn.each { |n| title << n }
end
end
def sections_names_cleanup(xml)
replace_title(xml, "//clause[@type = 'scope']", @i18n&.scope)
replace_title(xml, "//preface//abstract", @i18n&.abstract)
replace_title(xml, "//foreword", @i18n&.foreword)
replace_title(xml, "//introduction", @i18n&.introduction)
replace_title(xml, "//acknowledgements", @i18n&.acknowledgements)
section_names_refs_cleanup(xml)
section_names_terms_cleanup(xml)
end
def section_names_refs_cleanup(xml)
replace_title(xml, "//references[@normative = 'true']",
@i18n&.normref, true)
replace_title(xml, "//references[@normative = 'false']",
@i18n&.bibliography, true)
end
NO_SYMABBR = "[.//definitions[not(@type)]]".freeze
SYMABBR = "[.//definitions[@type = 'symbols']]"\
"[.//definitions[@type = 'abbreviated_terms']]".freeze
SYMnoABBR = "[.//definitions[@type = 'symbols']]"\
"[not(.//definitions[@type = 'abbreviated_terms'])]".freeze
ABBRnoSYM = "[.//definitions[@type = 'abbreviated_terms']]"\
"[not(.//definitions[@type = 'symbols'])]".freeze
def section_names_terms_cleanup(x)
replace_title(x, "//definitions[@type = 'symbols']", @i18n&.symbols)
replace_title(x, "//definitions[@type = 'abbreviated_terms']",
@i18n&.abbrev)
replace_title(x, "//definitions[not(@type)]", @i18n&.symbolsabbrev)
replace_title(x, "//terms#{SYMnoABBR} | //clause[.//terms]#{SYMnoABBR}",
@i18n&.termsdefsymbols, true)
replace_title(x, "//terms#{ABBRnoSYM} | //clause[.//terms]#{ABBRnoSYM}",
@i18n&.termsdefabbrev, true)
replace_title(x, "//terms#{SYMABBR} | //clause[.//terms]#{SYMABBR}",
@i18n&.termsdefsymbolsabbrev, true)
replace_title(x, "//terms#{NO_SYMABBR} | //clause[.//terms]#{NO_SYMABBR}",
@i18n&.termsdefsymbolsabbrev, true)
replace_title(
x,
"//terms[not(.//definitions)] | //clause[.//terms][not(.//definitions)]",
@i18n&.termsdef, true
)
end
end
end
end