From c46d64c67ab667b41f096412083f85278a076f89 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Tue, 20 Feb 2024 21:13:25 +1100 Subject: [PATCH 1/3] gemspec --- metanorma-itu.gemspec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metanorma-itu.gemspec b/metanorma-itu.gemspec index 74d48b35..e0f90d83 100644 --- a/metanorma-itu.gemspec +++ b/metanorma-itu.gemspec @@ -25,7 +25,8 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0") - spec.add_dependency "metanorma-standoc", "~> 2.7.0" + spec.add_dependency "metanorma-standoc", "~> 2.8.2" + spec.add_dependency "pubid-itu", "~> 0.1.1" spec.add_dependency "ruby-jing" spec.add_dependency "twitter_cldr", ">= 3.0.0" spec.add_dependency "tzinfo-data" # we need this for windows only From ac721e10cddace7de119d133c6dfb08f2a3e9d27 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 25 Feb 2024 00:48:06 +1100 Subject: [PATCH 2/3] capitalise table titles and column titles: https://github.com/metanorma/metanorma-itu/issues/496 --- lib/isodoc/itu/presentation_ref.rb | 96 ++++++++++ lib/isodoc/itu/presentation_xml_convert.rb | 114 +++--------- spec/isodoc/blocks_spec.rb | 207 +++++++++++++++------ 3 files changed, 279 insertions(+), 138 deletions(-) create mode 100644 lib/isodoc/itu/presentation_ref.rb diff --git a/lib/isodoc/itu/presentation_ref.rb b/lib/isodoc/itu/presentation_ref.rb new file mode 100644 index 00000000..03fdf630 --- /dev/null +++ b/lib/isodoc/itu/presentation_ref.rb @@ -0,0 +1,96 @@ +require_relative "init" +require "roman-numerals" +require "isodoc" +require_relative "../../relaton/render/general" +require_relative "presentation_bibdata" +require_relative "presentation_preface" + +module IsoDoc + module ITU + class PresentationXMLConvert < IsoDoc::PresentationXMLConvert + def bibrenderer + ::Relaton::Render::ITU::General.new(language: @lang) + end + + def bibrender_formattedref(formattedref, _xml) + formattedref << "." unless /\.$/.match?(formattedref.text) + id = reference_format_start(formattedref.parent) and + formattedref.children.first.previous = id + end + + def bibrender_relaton(xml, renderings) + f = renderings[xml["id"]][:formattedref] + ids = reference_format_start(xml) + f &&= "#{ids}#{f}" + # retain date in order to generate reference tag + keep = "./docidentifier | ./uri | ./note | ./date | ./biblio-tag" + xml.children = "#{f}#{xml.xpath(ns(keep)).to_xml}" + end + + def multi_bibitem_ref_code(bib) + skip = IsoDoc::Function::References::SKIP_DOCID + skip1 = "@type = 'metanorma' or @type = 'metanorma-ordinal'" + prim = "[@primary = 'true']" + id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip} or #{skip1})]")) + id.empty? and id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip1})]")) + id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip} or #{skip1})]")) + id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip1})]")) + id.empty? and return id + id.sort_by { |i| i["type"] == "ITU" ? 0 : 1 } + end + + def render_multi_identifiers(ids) + ids.map do |id| + if id["type"] == "ITU" then doctype_title(id) + else + docid_prefix(id["type"], id.text.sub(/^\[/, "").sub(/\]$/, "")) + end + end.join(" | ") + end + + def reference_format_start(bib) + id = multi_bibitem_ref_code(bib) + id1 = render_multi_identifiers(id) + out = id1 + date = bib.at(ns("./date[@type = 'published']/on | " \ + "./date[@type = 'published']/from")) and + out << " (#{date.text.sub(/-.*$/, '')})" + out += ", " if date || !id1.empty? + out + end + + def bibliography_bibitem_number1(bib, idx) + mn = bib.at(ns(".//docidentifier[@type = 'metanorma']")) and + /^\[?\d+\]?$/.match?(mn.text) and + mn["type"] = "metanorma-ordinal" + if (mn = bib.at(ns(".//docidentifier[@type = 'metanorma-ordinal']"))) && + !bibliography_bibitem_number_skip(bib) + idx += 1 + mn.children = "[#{idx}]" + end + idx + end + + def bibliography_bibitem_number_skip(bibitem) + @xrefs.klass.implicit_reference(bibitem) || + bibitem["hidden"] == "true" || bibitem.parent["hidden"] == "true" + end + + def norm_ref_entry_code(_ordinal, idents, _ids, _standard, datefn, _bib) + ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s + /^\[.+\]$/.match?(ret) or ret = "[#{ret}]" + ret += datefn + ret.empty? and return ret + ret.gsub("-", "‑").gsub(/ /, " ") + end + + def biblio_ref_entry_code(_ordinal, idents, _id, _standard, datefn, _bib) + ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s + /^\[.+\]$/.match?(ret) or ret = "[#{ret}]" + ret += datefn + ret.empty? and return ret + ret.gsub("-", "‑").gsub(/ /, " ") + end + end + end +end diff --git a/lib/isodoc/itu/presentation_xml_convert.rb b/lib/isodoc/itu/presentation_xml_convert.rb index 79bfa691..5054f856 100644 --- a/lib/isodoc/itu/presentation_xml_convert.rb +++ b/lib/isodoc/itu/presentation_xml_convert.rb @@ -4,6 +4,18 @@ require_relative "../../relaton/render/general" require_relative "presentation_bibdata" require_relative "presentation_preface" +require_relative "presentation_ref" + +module Nokogiri + module XML + class Node + def traverse_topdown(&block) + yield(self) + children.each { |j| j.traverse_topdown(&block) } + end + end + end +end module IsoDoc module ITU @@ -39,6 +51,24 @@ def note1(elem) super end + def table1(elem) + elem.xpath(ns("./name | ./thead/tr/th")).each do |n| + capitalise_unless_text_transform(n) + end + super + end + + def capitalise_unless_text_transform(elem) + css = nil + elem.traverse_topdown do |n| + n.name == "span" && /text-transform:/.match?(n["style"]) and + css = n + n.text? && /\S/.match?(n.text) or next + css && n.ancestors.include?(css) or n.replace(n.text.capitalize) + break + end + end + def get_eref_linkend(node) non_locality_elems(node).select do |c| !c.text? || /\S/.match(c) @@ -52,57 +82,6 @@ def get_eref_linkend(node) node.add_child(link) end - def bibrenderer - ::Relaton::Render::ITU::General.new(language: @lang) - end - - def bibrender_formattedref(formattedref, _xml) - formattedref << "." unless /\.$/.match?(formattedref.text) - id = reference_format_start(formattedref.parent) and - formattedref.children.first.previous = id - end - - def bibrender_relaton(xml, renderings) - f = renderings[xml["id"]][:formattedref] - ids = reference_format_start(xml) - f &&= "#{ids}#{f}" - # retain date in order to generate reference tag - keep = "./docidentifier | ./uri | ./note | ./date | ./biblio-tag" - xml.children = "#{f}#{xml.xpath(ns(keep)).to_xml}" - end - - def multi_bibitem_ref_code(bib) - skip = IsoDoc::Function::References::SKIP_DOCID - skip1 = "@type = 'metanorma' or @type = 'metanorma-ordinal'" - prim = "[@primary = 'true']" - id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip} or #{skip1})]")) - id.empty? and id = bib.xpath(ns("./docidentifier#{prim}[not(#{skip1})]")) - id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip} or #{skip1})]")) - id.empty? and id = bib.xpath(ns("./docidentifier[not(#{skip1})]")) - id.empty? and return id - id.sort_by { |i| i["type"] == "ITU" ? 0 : 1 } - end - - def render_multi_identifiers(ids) - ids.map do |id| - if id["type"] == "ITU" then doctype_title(id) - else - docid_prefix(id["type"], id.text.sub(/^\[/, "").sub(/\]$/, "")) - end - end.join(" | ") - end - - def reference_format_start(bib) - id = multi_bibitem_ref_code(bib) - id1 = render_multi_identifiers(id) - out = id1 - date = bib.at(ns("./date[@type = 'published']/on | " \ - "./date[@type = 'published']/from")) and - out << " (#{date.text.sub(/-.*$/, '')})" - out += ", " if date || !id1.empty? - out - end - def titlecase(str) str.gsub(/ |_|-/, " ").split(/ /).map(&:capitalize).join(" ") end @@ -165,39 +144,6 @@ def info(isoxml, out) super end - def bibliography_bibitem_number1(bib, idx) - mn = bib.at(ns(".//docidentifier[@type = 'metanorma']")) and - /^\[?\d+\]?$/.match?(mn.text) and - mn["type"] = "metanorma-ordinal" - if (mn = bib.at(ns(".//docidentifier[@type = 'metanorma-ordinal']"))) && - !bibliography_bibitem_number_skip(bib) - idx += 1 - mn.children = "[#{idx}]" - end - idx - end - - def bibliography_bibitem_number_skip(bibitem) - @xrefs.klass.implicit_reference(bibitem) || - bibitem["hidden"] == "true" || bibitem.parent["hidden"] == "true" - end - - def norm_ref_entry_code(_ordinal, idents, _ids, _standard, datefn, _bib) - ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s - /^\[.+\]$/.match?(ret) or ret = "[#{ret}]" - ret += datefn - ret.empty? and return ret - ret.gsub("-", "‑").gsub(/ /, " ") - end - - def biblio_ref_entry_code(_ordinal, idents, _id, _standard, datefn, _bib) - ret = (idents[:metanorma] || idents[:ordinal] || idents[:sdo]).to_s - /^\[.+\]$/.match?(ret) or ret = "[#{ret}]" - ret += datefn - ret.empty? and return ret - ret.gsub("-", "‑").gsub(/ /, " ") - end - def toc_title(docxml) @doctype == "resolution" and return super diff --git a/spec/isodoc/blocks_spec.rb b/spec/isodoc/blocks_spec.rb index 98553589..0ce56d54 100644 --- a/spec/isodoc/blocks_spec.rb +++ b/spec/isodoc/blocks_spec.rb @@ -148,7 +148,8 @@ OUTPUT - expect(xmlpp(strip_guid(IsoDoc::ITU::PresentationXMLConvert.new(presxml_options) + expect(xmlpp(strip_guid(IsoDoc::ITU::PresentationXMLConvert + .new(presxml_options) .convert("test", input, true)))).to be_equivalent_to xmlpp(presxml) expect(xmlpp(IsoDoc::ITU::WordConvert.new({}) .convert("test", presxml, true) @@ -159,59 +160,59 @@ it "processes tables (Word)" do input = <<~INPUT - - - - Table 1 — Repeatability and reproducibility of husked rice yield - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
Drago
-
A type of rice
-
- NOTE

This is a table about rice

-
DescriptionRice sample
ArborioDrago -

Parboiled rice.

-
Balilla -

Parboiled rice.

-
Thaibonnet
Number of laboratories retained after eliminating outliers - 13111313
Mean value, g/100 g81,282,081,877,7
Reproducibility limit, R (= 2,83 s_R)2,890,572,266,06
-
-
+ + + + Table 1 — Repeatability and reproducibility of husked rice yield + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
Drago
+
A type of rice
+
+ NOTE

This is a table about rice

+
DescriptionRice sample
ArborioDrago +

Parboiled rice.

+
Balilla +

Parboiled rice.

+
Thaibonnet
Number of laboratories retained after eliminating outliers + 13111313
Mean value, g/100 g81,282,081,877,7
Reproducibility limit, R (= 2,83 s_R)2,890,572,266,06
+
+
INPUT output = <<~OUTPUT
@@ -420,6 +421,104 @@ .to be_equivalent_to xmlpp(doc) end + it "capitalises table titles" do + input = <<~INPUT + + + title title + + + + + + +
title title1title title2title title3
title title4title title5title title6
+
+
+ INPUT + presxml = <<~OUTPUT + + + + Table of Contents + + + + Table — Title title + + + + + + + + + + + + + + +
Title title1Title title2title title3
title title4title title5title title6
+
+
+
+ OUTPUT + expect(xmlpp(strip_guid(IsoDoc::ITU::PresentationXMLConvert + .new(presxml_options) + .convert("test", input, true) + .gsub(%r{.*}m, "")))) + .to be_equivalent_to xmlpp(presxml) + + input = <<~INPUT + + + title title + + + + + + +
title title1title title2title title3
title title4title title5title title6
+
+
+ INPUT + presxml = <<~OUTPUT + + + + Table of Contents + + + + Table — title title + + + + + + + + + + + + + + +
title title1title title2title title3
title title4title title5title title6
+
+
+
+ OUTPUT + expect(xmlpp(strip_guid(IsoDoc::ITU::PresentationXMLConvert + .new(presxml_options) + .convert("test", input, true) + .gsub(%r{.*}m, "")))) + .to be_equivalent_to xmlpp(presxml) + end + it "post-processes steps class of ordered lists (Word)" do FileUtils.rm_f "test.doc" FileUtils.rm_f "test.html" From 6d95d8e2cfc0fbe1702edf979b479564c94c7188 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 25 Feb 2024 01:15:04 +1100 Subject: [PATCH 3/3] centre table column titles: https://github.com/metanorma/metanorma-itu/issues/496 --- lib/isodoc/itu/presentation_xml_convert.rb | 3 ++- lib/metanorma/itu/cleanup.rb | 7 +++++++ lib/metanorma/itu/converter.rb | 14 +++++++------- spec/isodoc/blocks_spec.rb | 4 ++-- spec/metanorma/base_spec.rb | 17 +++++++++-------- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/isodoc/itu/presentation_xml_convert.rb b/lib/isodoc/itu/presentation_xml_convert.rb index 5054f856..15a5b2ca 100644 --- a/lib/isodoc/itu/presentation_xml_convert.rb +++ b/lib/isodoc/itu/presentation_xml_convert.rb @@ -64,7 +64,8 @@ def capitalise_unless_text_transform(elem) n.name == "span" && /text-transform:/.match?(n["style"]) and css = n n.text? && /\S/.match?(n.text) or next - css && n.ancestors.include?(css) or n.replace(n.text.capitalize) + css && n.ancestors.include?(css) or + n.replace(::Metanorma::Utils.strict_capitalize_first(n.text)) break end end diff --git a/lib/metanorma/itu/cleanup.rb b/lib/metanorma/itu/cleanup.rb index 95a8783b..adc5cf40 100644 --- a/lib/metanorma/itu/cleanup.rb +++ b/lib/metanorma/itu/cleanup.rb @@ -24,6 +24,13 @@ def table_cleanup(xmldoc) end end + def header_rows_cleanup(xmldoc) + super + xmldoc.xpath("//table/thead/tr/th").each do |x| + x["align"] = "center" + end + end + def insert_missing_sections(xml) xml.at("//metanorma-extension/semantic-metadata/" \ "headless[text() = 'true']") and return nil diff --git a/lib/metanorma/itu/converter.rb b/lib/metanorma/itu/converter.rb index fd61c42c..4a8ccf7d 100644 --- a/lib/metanorma/itu/converter.rb +++ b/lib/metanorma/itu/converter.rb @@ -36,7 +36,8 @@ def makexml(node) end def doctype(node) - ret = node.attr("doctype")&.gsub(/\s+/, "-")&.downcase || "recommendation" + ret = node.attr("doctype")&.gsub(/\s+/, "-")&.downcase || + "recommendation" ret = "recommendation" if ret == "article" ret end @@ -87,7 +88,7 @@ def sectiontype_streamline(ret) def sectiontype(node, level = true) ret = super - hdr = sectiontype_streamline(node&.attr("heading")&.downcase) + hdr = sectiontype_streamline(node.attr("heading")&.downcase) return nil if ret == "terms and definitions" && hdr != "terms and definitions" && node.level > 1 return nil if ret == "symbols and abbreviated terms" && @@ -120,10 +121,8 @@ def clause_parse(attrs, xml, node) node.option?("unnumbered") and attrs[:unnumbered] = true case sectiontype1(node) when "conventions" then attrs = attrs.merge(type: "conventions") - when "history" - attrs[:preface] and attrs = attrs.merge(type: "history") - when "source" - attrs[:preface] and attrs = attrs.merge(type: "source") + when "history", "source" + attrs[:preface] and attrs = attrs.merge(type: sectiontype1(node)) end super end @@ -141,7 +140,8 @@ def doc_extract_attributes(node) def presentation_xml_converter(node) IsoDoc::ITU::PresentationXMLConvert .new(html_extract_attributes(node) - .merge(output_formats: ::Metanorma::ITU::Processor.new.output_formats)) + .merge(output_formats: ::Metanorma::ITU::Processor.new + .output_formats)) end def html_converter(node) diff --git a/spec/isodoc/blocks_spec.rb b/spec/isodoc/blocks_spec.rb index 0ce56d54..e09ec8ff 100644 --- a/spec/isodoc/blocks_spec.rb +++ b/spec/isodoc/blocks_spec.rb @@ -427,7 +427,7 @@ title title - + @@ -448,7 +448,7 @@ - + diff --git a/spec/metanorma/base_spec.rb b/spec/metanorma/base_spec.rb index 2ef4d695..8596a8cb 100644 --- a/spec/metanorma/base_spec.rb +++ b/spec/metanorma/base_spec.rb @@ -666,7 +666,8 @@ OUTPUT - xml.xpath("//xmlns:boilerplate | //xmlns:metanorma-extension | //xmlns:fetched") + xml.xpath("//xmlns:boilerplate | //xmlns:metanorma-extension | " \ + "//xmlns:fetched") .each(&:remove) expect(xmlpp(xml.to_xml)) .to be_equivalent_to xmlpp(output) @@ -1838,7 +1839,7 @@ .to be_equivalent_to xmlpp(output) end - it "capitalises table header" do + it "capitalises and centers table header" do input = <<~INPUT #{ASCIIDOC_BLANK_HDR} [headerrows=2] @@ -1856,14 +1857,14 @@
title title1title title2title title3
title title1title Title2title title3
title title4title title5title title6
Title title1Title title2Title Title2 title title3
- - - + + + - - - + + +
A bB cCA bB cC
abcabc