From 7159cde2c275e6d1b78cca695325a02aa0c90d4f Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Fri, 29 Nov 2024 01:06:36 +1100 Subject: [PATCH] Presentation XML caption refactor: https://github.com/metanorma/isodoc/issues/617 --- Gemfile.devel | 5 + lib/isodoc/ribose/base_convert.rb | 8 +- lib/isodoc/ribose/presentation_xml_convert.rb | 14 +- lib/isodoc/ribose/xref.rb | 37 +- spec/isodoc/html_convert_spec.rb | 790 ++++++++++++------ spec/metanorma/processor_spec.rb | 4 +- spec/spec_helper.rb | 1 + 7 files changed, 580 insertions(+), 279 deletions(-) create mode 100644 Gemfile.devel diff --git a/Gemfile.devel b/Gemfile.devel new file mode 100644 index 0000000..7e54eb7 --- /dev/null +++ b/Gemfile.devel @@ -0,0 +1,5 @@ +gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "feature/presxml-autonum" +gem "metanorma-generic", git: "https://github.com/metanorma/metanorma-generic", branch: "feature/presxml-autonum" +gem "isodoc-i18n", git: "https://github.com/metanorma/isodoc-i18n", branch: "fix/markup-connectives" +gem "mn-requirements", git: "https://github.com/metanorma/mn-requirements", branch: "feature/presxml-autonum" + diff --git a/lib/isodoc/ribose/base_convert.rb b/lib/isodoc/ribose/base_convert.rb index 2319f18..4c7b698 100644 --- a/lib/isodoc/ribose/base_convert.rb +++ b/lib/isodoc/ribose/base_convert.rb @@ -5,13 +5,17 @@ def executivesummary(clause, out) title_attr = { class: "IntroTitle" } page_break(out) out.div class: "Section3", id: clause["id"] do |div| - clause_name(clause, clause&.at(ns("./title")), div, title_attr) + clause_name(clause, clause&.at(ns("./fmt-title")), div, title_attr) clause.elements.each do |e| - parse(e, div) unless e.name == "title" + parse(e, div) unless e.name == "fmt-title" end end end + def sections_names + super << "executivesummary" + end + def is_clause?(name) return true if name == "executivesummary" diff --git a/lib/isodoc/ribose/presentation_xml_convert.rb b/lib/isodoc/ribose/presentation_xml_convert.rb index 7a0985a..0b08698 100644 --- a/lib/isodoc/ribose/presentation_xml_convert.rb +++ b/lib/isodoc/ribose/presentation_xml_convert.rb @@ -5,16 +5,19 @@ module IsoDoc module Ribose class PresentationXMLConvert < IsoDoc::Generic::PresentationXMLConvert - def annex1(elem) + # KILL + def annex1x(elem) lbl = @xrefs.anchor(elem["id"], :label) prefix_name(elem, "

", lbl, "title") end + def annex_delim(_elem) + "

" + end + def middle_title(docxml); end def termsource1(elem) - mod = elem.at(ns("./modification")) and - termsource_modification(mod) elem.children = l10n("#{@i18n.source}: " \ "#{to_xml(elem.children).strip}") elem&.next_element&.name == "termsource" and elem.next = "; " @@ -33,6 +36,11 @@ def preface_rearrange(doc) %w(), doc) end + def clause(docxml) + super + docxml.xpath(ns("//executivesummary | //appendix")).each { |x| clause1(x) } + end + include Init end end diff --git a/lib/isodoc/ribose/xref.rb b/lib/isodoc/ribose/xref.rb index 6ef7feb..7cbdc8f 100644 --- a/lib/isodoc/ribose/xref.rb +++ b/lib/isodoc/ribose/xref.rb @@ -6,7 +6,8 @@ class Counter < IsoDoc::XrefGen::Counter end class Xref < IsoDoc::Generic::Xref - def annex_name_lbl(clause, num) + # KILL + def annex_name_lblx(clause, num) obl = l10n("(#{@labels['inform_annex']})") clause["obligation"] == "normative" and obl = l10n("(#{@labels['norm_annex']})") @@ -28,7 +29,8 @@ def initial_anchor_names(doc) end end - def section_names1(clause, num, level) + # KILL + def section_names1x(clause, num, level) @anchors[clause["id"]] = { label: num, level: level, xref: num } # subclauses are not prefixed with "Clause" @@ -39,8 +41,25 @@ def section_names1(clause, num, level) end end + # subclauses are not prefixed with "Clause" + # retaining subtype for the semantics + def section_name_anchors(clause, num, level) + if clause["type"] == "section" + xref = labelled_autonum(@labels["section"], num) + label = labelled_autonum(@labels["section"], num) + @anchors[clause["id"]] = + { label:, xref:, elem: @labels["section"], + title: clause_title(clause), level: level, type: "clause" } + elsif level > 1 + #num = semx(clause, num) + @anchors[clause["id"]] = + { label: num, level: level, xref: num, subtype: "clause" } + else super end + end + # we can reference 0-number clauses in introduction - def introduction_names(clause) + # # KILL + def introduction_namesx(clause) clause.nil? and return clause.at(ns("./clause")) and @anchors[clause["id"]] = { label: "0", level: 1, type: "clause", @@ -49,6 +68,18 @@ def introduction_names(clause) clause.xpath(ns("./clause")).each do |c| section_names1(c, i.increment(c).print, 2) end + end + # we can reference 0-number clauses in introduction + def introduction_names(clause) + clause.nil? and return + clause.at(ns("./clause")) and + @anchors[clause["id"]] = { label: nil, level: 1, type: "clause", + xref: clause.at(ns("./title"))&.text } + #i = Counter.new(0, prefix: "0") + i = Counter.new(0) + clause.xpath(ns("./clause")).each do |c| + section_names1(c, semx(clause, "0"), i.increment(c).print, 2) + end end end end diff --git a/spec/isodoc/html_convert_spec.rb b/spec/isodoc/html_convert_spec.rb index b479eb1..2d6d402 100644 --- a/spec/isodoc/html_convert_spec.rb +++ b/spec/isodoc/html_convert_spec.rb @@ -192,91 +192,333 @@ presxml = <<~OUTPUT - - - Contents - - - Foreword -

This is a preamble

-
- - Executive Summary - - - Introduction - - 0.1.<tab/>Introduction Subsection - - -
- - - 1.<tab/>Scope -

Text

-
- - 3.<tab/>Terms, definitions, symbols and abbreviated terms - - 3.1.<tab/>Normal Terms - 3.1.1. - Term2 - - - - - 3.2. - <tab/> - Symbols - -
-
Symbol
-
Definition
-
-
-
- - - 4. - <tab/> - Symbols - -
-
Symbol
-
Definition
-
-
- - 5.<tab/>Clause 4 - - 5.1.<tab/>Introduction - - - 5.2.<tab/>Clause 4.2 - - - - 2.<tab/>Normative References - -
- - Annex A<br/>(normative)<br/><br/>Annex - - A.1.<tab/>Annex A.1 - - A.1.1.<tab/>Annex A.1a - - - - - - Bibliography - - Bibliography Subsection - - - -
+ + + Contents + + + Foreword + + Foreword + +

This is a preamble

+
+ + Executive Summary + + Executive Summary + + + + Introduction + + Introduction + + + Introduction Subsection + + + 0 + . + 1 + . + + + + + Introduction Subsection + + + 0 + . + 1 + + + +
+ + + Scope + + + 1 + . + + + + + Scope + + + Clause + 1 + +

Text

+
+ + Terms, definitions, symbols and abbreviated terms + + + 3 + . + + + + + Terms, definitions, symbols and abbreviated terms + + + Clause + 3 + + + Normal Terms + + + 3 + . + 1 + . + + + + + Normal Terms + + + 3 + . + 1 + + + + + 3 + . + 1 + . + 1 + . + + + + 3 + . + 1 + . + 1 + + Term2 + + + + Symbols + + + 3 + . + 2 + . + + + + + Symbols + + + 3 + . + 2 + +
+
Symbol
+
Definition
+
+
+
+ + Symbols + + + 4 + . + + + + + Symbols + + + Clause + 4 + +
+
Symbol
+
Definition
+
+
+ + Clause 4 + + + 5 + . + + + + + Clause 4 + + + Clause + 5 + + + Introduction + + + 5 + . + 1 + . + + + + + Introduction + + + 5 + . + 1 + + + + Clause 4.2 + + + 5 + . + 2 + . + + + + + Clause 4.2 + + + 5 + . + 2 + + + + + Normative References + + + 2 + . + + + + + Normative References + + + Clause + 2 + + +
+ + + <strong>Annex</strong> + + + + + Annex + A + + +
+ (normative) + +
+
+
+ + Annex + +
+ + Annex + A + + + Annex A.1 + + + A + . + 1 + . + + + + + Annex A.1 + + + Annex + A + . + 1 + + + Annex A.1a + + + A + . + 1 + . + 1 + . + + + + + Annex A.1a + + + Annex + A + . + 1 + . + 1 + + + +
+ + + Bibliography + + Bibliography + + + Bibliography Subsection + + Bibliography Subsection + + + + + OUTPUT output = Xml::C14n.format(<<~"OUTPUT") @@ -316,11 +558,7 @@

Term2

-

- 3.2. -   - Symbols -

+

3.2.  Symbols

@@ -332,11 +570,7 @@
-

- 4. -   - Symbols -

+

4.  Symbols

@@ -357,7 +591,7 @@

-

Annex A
(normative)

Annex

+

Annex A
(normative)

Annex

A.1.  Annex A.1

@@ -376,21 +610,21 @@ OUTPUT - expect(Xml::C14n.format(strip_guid(IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) + pres_output = IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) .convert("test", input, true) + expect(Xml::C14n.format(strip_guid(pres_output))) + .to be_equivalent_to Xml::C14n.format(presxml) + expect(Xml::C14n.format(strip_guid(IsoDoc::Ribose::HtmlConvert.new({}) + .convert("test", pres_output, true) .gsub(%r{^.*.*}m, "")))).to be_equivalent_to Xml::C14n.format(presxml) - expect(Xml::C14n.format(IsoDoc::Ribose::HtmlConvert.new({}) - .convert("test", presxml, true) - .gsub(%r{^.*.*}m, ""))).to be_equivalent_to output + .gsub(%r{.*}m, "")))).to be_equivalent_to output end it "processes introduction with no subsections" do input = <<~INPUT - + Foreword

This is a preamble before

@@ -401,23 +635,31 @@
INPUT presxml = <<~OUTPUT - - - - Contents - - - Foreword -

- This is a preamble before - Introduction -

-
- - Introduction - -
-
+ + + + Contents + + + Foreword + + Foreword + +

+ This is a preamble before + + Introduction + +

+
+ + Introduction + + Introduction + + +
+
OUTPUT output = <<~OUTPUT #{HTML_HDR} @@ -426,7 +668,7 @@

Contents


-
+

Foreword

This is a preamble before @@ -440,14 +682,14 @@

OUTPUT - expect(Xml::C14n.format(strip_guid(IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) - .convert("test", input, true) - .gsub(%r{^.*.*}m, "")))).to be_equivalent_to Xml::C14n.format(presxml) - expect(Xml::C14n.format(IsoDoc::Ribose::HtmlConvert.new({}) - .convert("test", presxml, true) + pres_output = IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) + .convert("test", input, true) + expect(Xml::C14n.format(strip_guid(pres_output))) + .to be_equivalent_to Xml::C14n.format(presxml) + expect(Xml::C14n.format(strip_guid(IsoDoc::Ribose::HtmlConvert.new({}) + .convert("test", pres_output, true) .gsub(%r{^.*.*}m, ""))).to be_equivalent_to Xml::C14n.format(output) + .gsub(%r{.*}m, "")))).to be_equivalent_to output end it "injects JS into blank html" do @@ -569,115 +811,97 @@ INPUT - expect(Xml::C14n.format(strip_guid(IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) - .convert("test", input, true) - .gsub(%r{^.*.*}m, "")))).to be_equivalent_to Xml::C14n.format(<<~OUTPUT) - - - - Contents - - - Foreword -

This is a preamble - 0.1 - 0.2 - Clause 1 - Clause 3 - 3.1 - 3.1.1 - 3.2 - Clause 4 - Clause 5 - 5.1 - 5.2 - Annex A - Annex A.1 - Annex A.1.1 - [Q2] - Clause 2 -

-
- - Introduction - - 0.1.<tab/>Introduction Subsection - - 0.2.Text - -
- - - 1.<tab/>Scope -

Text

-
- - 3.<tab/>Terms, definitions, symbols and abbreviated terms - - 3.1.<tab/>Normal Terms - 3.1.1. - Term2 - - - - - 3.2. - <tab/> - Symbols - -
-
Symbol
-
Definition
-
-
-
- - - 4. - <tab/> - Symbols - -
-
Symbol
-
Definition
-
-
- - 5.<tab/>Clause 4 - - 5.1.<tab/>Introduction - - - 5.2.<tab/>Clause 4.2 - - - - 2.<tab/>Normative References - -
- - Annex A<br/>(normative)<br/><br/>Annex - - A.1.<tab/>Annex A.1 - - A.1.1.<tab/>Annex A.1a - - - - An Appendix - - - - - Bibliography - - Bibliography Subsection - - - -
+ output = <<~OUTPUT + + Foreword + + Foreword + +

+ This is a preamble + + 0 + . + 1 + + + 0 + . + 2 + + + Clause + 1 + + + Clause + 3 + + + 3 + . + 1 + + + 3 + . + 1 + . + 1 + + + 3 + . + 2 + + + Clause + 4 + + + Clause + 5 + + + 5 + . + 1 + + + 5 + . + 2 + + + Annex + A + + + Annex + A + . + 1 + + + Annex + A + . + 1 + . + 1 + + [Q2] + + Clause + 2 + +

+
OUTPUT + expect(Xml::C14n.format(strip_guid(Nokogiri::XML(IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) + .convert("test", input, true)) + .at("//xmlns:foreword").to_xml))) + .to be_equivalent_to Xml::C14n.format(output) end it "processes simple terms & definitions" do @@ -704,34 +928,62 @@ INPUT presxml = <<~INPUT - - - - Contents - - - - 1.<tab/>Terms, Definitions, Symbols and Abbreviated Terms - - 1.1. - Term2 - Term2A - Term2B - DEPRECATED: Term2C - DEPRECATED: Term2D - SOURCE: - - - 3.1 - - ISO 7301:2011, Clause 3.1 - , modified — The term "cargo rice" is shown as deprecated, and - Note 1 to entry is not included here - - - - - + + + + Contents + + + + + Terms, Definitions, Symbols and Abbreviated Terms + + + 1 + . + + + + + Terms, Definitions, Symbols and Abbreviated Terms + + + Clause + 1 + + + + + 1 + . + 1 + . + + + + 1 + . + 1 + + Term2 + Term2A + Term2B + DEPRECATED: Term2C + DEPRECATED: Term2D + + SOURCE: + + + 3.1 + + ISO 7301:2011, Clause 3.1 + + , modified — The term "cargo rice" is shown as deprecated, and Note 1 to entry is not included here + + + + + INPUT output = Xml::C14n.format(strip_guid(<<~OUTPUT)) @@ -743,19 +995,19 @@

DEPRECATED: Term2C

DEPRECATED: Term2D

SOURCE: - ISO 7301:2011, Clause 3.1 , modified — The term "cargo rice" is shown as deprecated, and Note 1 + ISO 7301:2011, Clause 3.1, modified — The term "cargo rice" is shown as deprecated, and Note 1 to entry is not included here

OUTPUT - expect(Xml::C14n.format(strip_guid(IsoDoc::Ribose::PresentationXMLConvert - .new(presxml_options) - .convert("test", input, true) + pres_output = IsoDoc::Ribose::PresentationXMLConvert.new(presxml_options) + .convert("test", input, true) + expect(Xml::C14n.format(strip_guid(pres_output .sub(%r{.*}m, "")))) .to be_equivalent_to Xml::C14n.format(presxml) IsoDoc::Ribose::HtmlConvert.new({ filename: "test" }) - .convert("test", presxml, false) + .convert("test", pres_output, false) expect(Xml::C14n.format(strip_guid( File.read("test.html") .gsub(%r{^.*
}m, '
') diff --git a/spec/metanorma/processor_spec.rb b/spec/metanorma/processor_spec.rb index 11fa786..e3b330c 100644 --- a/spec/metanorma/processor_spec.rb +++ b/spec/metanorma/processor_spec.rb @@ -43,9 +43,9 @@ - 1<tab/>Terms, Definitions, Symbols and Abbreviated Terms + 1Terms, Definitions, Symbols and Abbreviated Terms - 1.1 + 1.1 Term2 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1c8a682..265ea5a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -46,6 +46,7 @@ def presxml_options def strip_guid(str) str.gsub(%r{ id="_[^"]+"}, ' id="_"') .gsub(%r{ target="_[^"]+"}, ' target="_"') + .gsub(%r{ source="_[^"]+"}, ' source="_"') .gsub(%r{[^<]+}, "") .gsub(%r{ schema-version="[^"]+"}, "") end