Skip to content

Commit

Permalink
Merge pull request #203 from metanorma/features/2023updates
Browse files Browse the repository at this point in the history
Features/2023updates
  • Loading branch information
opoudjis authored Jan 31, 2024
2 parents 9be8b3e + 5833d27 commit d6aa9b7
Show file tree
Hide file tree
Showing 22 changed files with 930 additions and 710 deletions.
52 changes: 20 additions & 32 deletions lib/isodoc/ietf/blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class RfcConvert < ::IsoDoc::Convert
def para_attrs(node)
{ keepWithNext: node["keep-with-next"],
keepWithPrevious: node["keep-with-previous"],
indent: node["indent"],
anchor: node["id"] }
end

Expand All @@ -18,10 +19,9 @@ def para_parse(node, out)
node.xpath(ns("./note")).each { |n| parse(n, out) }
end

# NOTE ignoring "bare" attribute, which is tantamount to "empty"
def ul_attrs(node)
{ anchor: node["id"],
empty: node["nobullet"],
{ anchor: node["id"], empty: node["nobullet"],
indent: node["indent"], bare: node["bare"],
spacing: node["spacing"] }
end

Expand All @@ -45,10 +45,9 @@ def ol_style(type)

def ol_attrs(node)
{ anchor: node["id"],
spacing: node["spacing"],
spacing: node["spacing"], indent: node["indent"],
type: ol_style(node["type"]),
group: node["group"],
start: node["start"] }
group: node["group"], start: node["start"] }
end

def ol_parse(node, out)
Expand All @@ -58,25 +57,20 @@ def ol_parse(node, out)
end

def dl_attrs(node)
attr_code(anchor: node["id"],
newline: node["newline"],
indent: node["indent"],
spacing: node["spacing"])
attr_code(anchor: node["id"], newline: node["newline"],
indent: node["indent"], spacing: node["spacing"])
end

def dt_parse(dterm, term)
if dterm.elements.empty?
term << dterm.text
else
dterm.children.each { |n| parse(n, term) }
if dterm.elements.empty? then term << dterm.text
else dterm.children.each { |n| parse(n, term) }
end
end

def note_label(node)
n = @xrefs.get[node["id"]]
return l10n("#{@i18n.note}: ") if n.nil? || n[:label].nil? ||
n[:label].empty?

n.nil? || n[:label].nil? || n[:label].empty? and
return l10n("#{@i18n.note}: ")
l10n("#{@i18n.note} #{n[:label]}: ")
end

Expand All @@ -103,8 +97,7 @@ def example_label(node, div, name)
div.t **attr_code(anchor: node["id"], keepWithNext: "true") do |p|
lbl = if n.nil? || n[:label].nil? || n[:label].empty?
@i18n.example
else
l10n("#{@i18n.example} #{n[:label]}")
else l10n("#{@i18n.example} #{n[:label]}")
end
p << lbl
name and !lbl.nil? and p << ": "
Expand Down Expand Up @@ -155,8 +148,7 @@ def annotation_parse1(ann, dlist)
end

def formula_where(dlist, out)
return unless dlist

dlist or return
out.t { |p| p << @i18n.where }
parse(dlist, out)
end
Expand All @@ -174,15 +166,14 @@ def formula_parse(node, out)
formula_parse1(node, out)
formula_where(node.at(ns("./dl")), out)
node.children.each do |n|
next if %w(stem dl).include? n.name

%w(stem dl).include? n.name and next
parse(n, out)
end
end

def quote_attribution(node)
author = node&.at(ns("./author"))&.text
source = node&.at(ns("./source/@uri"))&.text
author = node.at(ns("./author"))&.text
source = node.at(ns("./source/@uri"))&.text
attr_code(quotedFrom: author, cite: source)
end

Expand All @@ -197,8 +188,7 @@ def quote_parse(node, out)
def admonition_name(node, type)
name = node&.at(ns("./name")) and return name
name = Nokogiri::XML::Node.new("name", node.document)
return unless type && @i18n.admonition[type]

type && @i18n.admonition[type] or return
name << @i18n.admonition[type]&.upcase
name
end
Expand Down Expand Up @@ -230,8 +220,7 @@ def review_note_parse(node, out)
end

def figure_name_parse(_node, div, name)
return if name.nil?

name.nil? and return
div.name do |_n|
name.children.each { |n| parse(n, div) }
end
Expand All @@ -242,9 +231,8 @@ def pseudocode_parse(node, out)
end

def figure_parse(node, out)
return pseudocode_parse(node, out) if node["class"] == "pseudocode" ||
node["type"] == "pseudocode"

node["class"] == "pseudocode" || node["type"] == "pseudocode" and
return pseudocode_parse(node, out)
@in_figure = true
out.figure **attr_code(anchor: node["id"]) do |div|
figure_name_parse(node, div, node.at(ns("./name")))
Expand Down
191 changes: 16 additions & 175 deletions lib/isodoc/ietf/cleanup.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "cleanup_inline"
require_relative "cleanup_blocks"

module IsoDoc
module Ietf
Expand All @@ -21,20 +22,32 @@ def cleanup(docxml)
end

def biblio_cleanup(xmldoc)
biblio_referencegroup_cleanup(xmldoc)
biblio_abstract_cleanup(xmldoc)
biblio_date_cleanup(xmldoc)
biblio_refcontent_cleanup(xmldoc)
annotation_cleanup(xmldoc)
end

def biblio_referencegroup_cleanup(xmldoc)
xmldoc.xpath("//reference[ref-included]").each do |r|
r.name = "referencegroup"
r.elements.each do |e|
if e.name == "ref-included"
e.name = "reference"
e["anchor"] ||= "_#{UUIDTools::UUID.random_create}"
else e.remove
end
end
end
end

def biblio_date_cleanup(xmldoc)
xmldoc.xpath("//date[@cleanme]").each do |a|
a.delete("cleanme")
d = @c.decode(a.text).gsub(/–/, "-").sub(/-\d\d\d\d.*$/, "")
if attrs = date_attr(d)
attrs.each do |k, v|
a[k] = v
end
attrs.each { |k, v| a[k] = v }
a.children.remove
else a.remove end
end
Expand Down Expand Up @@ -67,185 +80,13 @@ def reparse_abstract(abstract)
end.join
end

def li_cleanup(xmldoc)
xmldoc.xpath("//li[t]").each do |li|
next unless li.elements.size == 1

li.children = li.elements[0].children
end
end

def front_cleanup(xmldoc)
xmldoc.xpath("//title").each { |s| s.children = s.text }
xmldoc.xpath("//reference/front[not(author)]").each do |f|
insert = f.at("./seriesInfo[last()]") || f.at("./title")
insert.next = "<author surname='Unknown'/>"
end
end

def table_footnote_cleanup(docxml)
docxml.xpath("//table[descendant::fn]").each do |t|
t.xpath(".//fn").each do |a|
t << "<aside>#{a.remove.children}</aside>"
end
end
end

def figure_footnote_cleanup(docxml)
docxml.xpath("//figure[descendant::fn]").each do |t|
t.xpath(".//fn").each do |a|
t << "<aside>#{a.remove.children}</aside>"
end
end
end

def table_cleanup(docxml)
table_footnote_cleanup(docxml)
end

def figure_cleanup(docxml)
figure_postamble(docxml)
figure_unnest(docxml)
figure_footnote_cleanup(docxml)
figure_data_uri(docxml)
end

def figure_data_uri(docxml)
docxml.xpath("//artwork").each do |a|
%r{^data:image/svg\+xml;base64}.match?(a["src"]) or next
f = Vectory::Utils::save_dataimage(a["src"])
a.delete("src")
a.children = File.read(f).sub(%r{<\?.+\?>}, "")
end
end

def figure_unnest(docxml)
docxml.xpath("//figure[descendant::figure]").each do |f|
insert = f
f.xpath(".//figure").each do |a|
title = f.at("./name") and a.children.first.previous = title.remove
insert.next = a.remove
insert = insert.next_element
end
f.remove
end
end

def figure_postamble(docxml)
make_postamble(docxml)
move_postamble(docxml)
move_preamble(docxml)
end

def make_postamble(docxml)
docxml.xpath("//figure").each do |f|
a = f.at("./artwork | ./sourcecode") || next
name = f.at("./name")&.remove
b = a.xpath("./preceding-sibling::*")&.remove
c = a.xpath("./following-sibling::*")&.remove
a = a.remove
name and f << name
b.empty? or f << "<preamble>#{to_xml(b)}</preamble>"
a and f << a
c.empty? or f << "<postamble>#{to_xml(c)}</postamble>"
end
end

def move_postamble(docxml)
docxml.xpath("//postamble").each do |p|
insert = p.parent
p.remove.elements.each do |e|
insert.next = e
insert = insert.next_element
end
end
end

def move_preamble(docxml)
docxml.xpath("//preamble").each do |p|
insert = p.parent
p.remove.elements.each do |e|
insert.previous = e
end
end
end

# for markup in pseudocode
def sourcecode_cleanup(docxml)
docxml.xpath("//sourcecode").each do |s|
s.children = to_xml(s.children).gsub(%r{<br/>\n}, "\n")
.gsub(%r{\s+(<t[ >])}, "\\1").gsub(%r{</t>\s+}, "</t>")
sourcecode_remove_markup(s)
s.children = "<![CDATA[#{HTMLEntities.new.decode(to_xml(s
.children).sub(/\A\n+/, ''))}]]>"
end
end

def sourcecode_remove_markup(node)
node.traverse do |n|
next if n.text?
next if %w(name callout annotation note sourcecode).include? n.name

case n.name
when "br" then n.replace("\n")
when "t" then n.replace("\n\n#{n.children}")
else
n.replace(n.children)
end
end
end

def annotation_cleanup(docxml)
docxml.xpath("//reference").each do |r|
while r.next_element&.name == "aside"
annotation_cleanup1(r)
end
end
docxml.xpath("//references/aside").each(&:remove)
end

def annotation_cleanup1(ref)
aside = ref.next_element
aside.name = "annotation"
aside.traverse do |n|
n.name == "t" and n.replace(n.children)
end
ref << aside
end

def deflist_cleanup(docxml)
dt_cleanup(docxml)
dd_cleanup(docxml)
end

def dt_cleanup(docxml)
docxml.xpath("//dt").each do |d|
d.first_element_child&.name == "bookmark" and
d["anchor"] ||= d.first_element_child["anchor"]
d.xpath(".//t").each do |t|
d["anchor"] ||= t["anchor"]
t.replace(t.children)
end
end
end

def dd_cleanup(docxml)
docxml.xpath("//dd").each do |d|
d&.first_element_child&.name == "bookmark" and
d["anchor"] ||= d.first_element_child["anchor"]
end
end

def aside_cleanup(docxml)
docxml.xpath("//*[aside]").each do |p|
%w(section).include?(p.name) and next
insert = p
p.xpath("./aside").each do |a|
insert.next = a.remove
insert = insert.next_element
end
end
end
end
end
end
Loading

0 comments on commit d6aa9b7

Please sign in to comment.