From cc2d6660c1a03eb52c27d9e49a689ec9cbfc255b Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Wed, 23 Nov 2022 00:31:43 +1100 Subject: [PATCH] refactor to_xml: https://github.com/metanorma/metanorma-ieee/issues/170 --- .gitignore | 1 - Gemfile.devel | 1 + lib/isodoc/ietf/cleanup.rb | 10 +++++----- lib/isodoc/ietf/cleanup_inline.rb | 2 +- lib/isodoc/ietf/footnotes.rb | 18 +++++++++++------- lib/isodoc/ietf/inline.rb | 2 +- 6 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 Gemfile.devel diff --git a/.gitignore b/.gitignore index 55537d91..97711703 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,6 @@ .rubocop-https--* -/Gemfile.devel /Gemfile.lock /test.err /test.rfc.xml.err diff --git a/Gemfile.devel b/Gemfile.devel new file mode 100644 index 00000000..69d8cb93 --- /dev/null +++ b/Gemfile.devel @@ -0,0 +1 @@ +gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "main" diff --git a/lib/isodoc/ietf/cleanup.rb b/lib/isodoc/ietf/cleanup.rb index a48fb0f6..13b5784e 100644 --- a/lib/isodoc/ietf/cleanup.rb +++ b/lib/isodoc/ietf/cleanup.rb @@ -99,9 +99,9 @@ def make_postamble(docxml) c = a&.xpath("./following-sibling::*")&.remove a = a.remove name and f << name - b.empty? or f << "#{b.to_xml}" + b.empty? or f << "#{to_xml(b)}" a and f << a - c.empty? or f << "#{c.to_xml}" + c.empty? or f << "#{to_xml(c)}" end end @@ -127,11 +127,11 @@ def move_preamble(docxml) # for markup in pseudocode def sourcecode_cleanup(docxml) docxml.xpath("//sourcecode").each do |s| - s.children = s.children.to_xml.gsub(%r{
\n}, "\n") + s.children = to_xml(s.children).gsub(%r{
\n}, "\n") .gsub(%r{\s+(])}, "\\1").gsub(%r{\s+}, "") sourcecode_remove_markup(s) - s.children = "" + s.children = "" end end diff --git a/lib/isodoc/ietf/cleanup_inline.rb b/lib/isodoc/ietf/cleanup_inline.rb index 897c9136..8624e1f0 100644 --- a/lib/isodoc/ietf/cleanup_inline.rb +++ b/lib/isodoc/ietf/cleanup_inline.rb @@ -14,7 +14,7 @@ def u_cleanup(xmldoc) def footnote_cleanup(docxml) fn = footnote_refs_cleanup(docxml) endnotes = make_endnotes(docxml) - docxml.xpath("//section[descendant::fn] | "\ + docxml.xpath("//section[descendant::fn] | " \ "//abstract[descendant::fn]").each do |s| s.xpath(".//fn").each do |f| ref = f.at(".//ref") and ref.replace("[#{fn[ref.text]}] ") diff --git a/lib/isodoc/ietf/footnotes.rb b/lib/isodoc/ietf/footnotes.rb index e77bc2cd..4fc9beb9 100644 --- a/lib/isodoc/ietf/footnotes.rb +++ b/lib/isodoc/ietf/footnotes.rb @@ -2,6 +2,7 @@ module IsoDoc::Ietf class RfcConvert < ::IsoDoc::Convert def footnote_parse(node, out) return table_footnote_parse(node, out) if @in_table || @in_figure + fn = node["reference"] out.fnref fn make_local_footnote(node, fn, out) @@ -9,6 +10,7 @@ def footnote_parse(node, out) def make_local_footnote(node, fn, out) return if @seen_footnote.include?(fn) + @in_footnote = true out << make_generic_footnote_text(node, fn) @in_footnote = false @@ -18,13 +20,13 @@ def make_local_footnote(node, fn, out) def make_generic_footnote_text(node, fnref) first = node.first_element_child noko do |xml| - xml.fn do |div| + xml.fn do |_div| xml.t **attr_code(anchor: first ? first["id"] : nil) do |div| div.ref fnref first.name == "p" and first.children.each { |n| parse(n, div) } end - first.name == "p" and - node.elements.drop(1).each { |n| parse(n, xml) } or + (first.name == "p" and + node.elements.drop(1).each { |n| parse(n, xml) }) or node.children.each { |n| parse(n, xml) } end end.join @@ -36,6 +38,7 @@ def table_footnote_parse(node, out) make_table_footnote_link(out, tid + fn, fn) # do not output footnote text if we have already seen it for this table return if @seen_footnote.include?(tid + fn) + @in_footnote = true out.fn do |a| a << make_table_footnote_text(node, tid + fn, fn) @@ -44,19 +47,19 @@ def table_footnote_parse(node, out) @seen_footnote << (tid + fn) end - def make_table_footnote_link(out, fnid, fnref) + def make_table_footnote_link(out, _fnid, fnref) out << " [#{fnref}]" end - def make_table_footnote_text(node, fnid, fnref) + def make_table_footnote_text(node, _fnid, fnref) first = node.first_element_child noko do |xml| xml.t **attr_code(anchor: first ? first["id"] : nil) do |div| div << "[#{fnref}] " first.name == "p" and first.children.each { |n| parse(n, div) } end - first.name == "p" and - node.elements.drop(1).each { |n| parse(n, xml) } or + (first.name == "p" and + node.elements.drop(1).each { |n| parse(n, xml) }) or node.children.each { |n| parse(n, xml) } end.join end @@ -64,6 +67,7 @@ def make_table_footnote_text(node, fnid, fnref) def get_table_ancestor_id(node) table = node.ancestors("table") || node.ancestors("figure") return UUIDTools::UUID.random_create.to_s if table.empty? + table.last["id"] end end diff --git a/lib/isodoc/ietf/inline.rb b/lib/isodoc/ietf/inline.rb index 052defe8..a131f8e6 100644 --- a/lib/isodoc/ietf/inline.rb +++ b/lib/isodoc/ietf/inline.rb @@ -112,7 +112,7 @@ def get_linkend(node) end contents = no_loc_contents.select { |c| !c.text? || /\S/.match(c) } !contents.empty? and - return Nokogiri::XML::NodeSet.new(node.document, contents).to_xml + return to_xml(Nokogiri::XML::NodeSet.new(node.document, contents)) "" end