From 6f6586a67579597f7bab7b9cb4ad0ab1c05a144c Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Tue, 14 Nov 2023 00:50:49 +1100 Subject: [PATCH] branch-number override of section numbering: https://github.com/metanorma/metanorma-iso/issues/1044 --- lib/isodoc/xref/xref_counter.rb | 26 ++++-- lib/isodoc/xref/xref_sect_gen.rb | 18 ++-- spec/isodoc/xref_numbering_spec.rb | 133 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 17 deletions(-) diff --git a/lib/isodoc/xref/xref_counter.rb b/lib/isodoc/xref/xref_counter.rb index db75a03f..24bf27f9 100644 --- a/lib/isodoc/xref/xref_counter.rb +++ b/lib/isodoc/xref/xref_counter.rb @@ -15,15 +15,17 @@ def increment(label, node) end class Counter + attr_accessor :prefix_override + def initialize(num = 0, opts = { numerals: :arabic }) @unnumbered = false @num = num @letter = "" @subseq = "" - @letter_override = nil - @number_override = nil + reset_overrides @style = opts[:numerals] @skip_i = opts[:skip_i] + @prefix = opts[:prefix] @base = "" if num.is_a? String if /^\d+$/.match?(num) @@ -36,6 +38,12 @@ def initialize(num = 0, opts = { numerals: :arabic }) end end + def reset_overrides + @letter_override = nil + @number_override = nil + @prefix_override = nil + end + def new_subseq_increment(node) @subseq = node["subsequence"] @num += 1 unless @num.nil? @@ -57,7 +65,9 @@ def new_subseq_increment1(node) end def sequence_increment(node) - if node["number"] + if node["branch-number"] + @prefix_override = node["branch-number"] + elsif node["number"] @base = @letter_override = @number_override = "" /^(?.*?)(?\d+)$/ =~ node["number"] if blank?(n) @@ -130,9 +140,7 @@ def blank?(str) def increment(node) @unnumbered = (node["unnumbered"] || node["hidden"]) and return self - - @letter_override = nil - @number_override = nil + reset_overrides if node["subsequence"] != @subseq && !(blank?(node["subsequence"]) && blank?(@subseq)) new_subseq_increment(node) @@ -143,11 +151,11 @@ def increment(node) end def print - return nil if @unnumbered - + @unnumbered and return nil + @prefix_override and return @prefix_override num = @number_override || @num out = @style == :roman && !num.nil? ? RomanNumerals.to_roman(num) : num - "#{@base}#{out}#{@letter_override || @letter}" + "#{@prefix}#{@base}#{out}#{@letter_override || @letter}" end def ol_type(list, depth) diff --git a/lib/isodoc/xref/xref_sect_gen.rb b/lib/isodoc/xref/xref_sect_gen.rb index a5d92bbd..28c773ef 100644 --- a/lib/isodoc/xref/xref_sect_gen.rb +++ b/lib/isodoc/xref/xref_sect_gen.rb @@ -169,17 +169,18 @@ def section_names(clause, num, lvl) clause.nil? and return num num.increment(clause) section_name_anchors(clause, num.print, lvl) - clause.xpath(ns(SUBCLAUSES)).each_with_object(Counter.new) do |c, i| - section_names1(c, "#{num.print}.#{i.increment(c).print}", lvl + 1) + clause.xpath(ns(SUBCLAUSES)) + .each_with_object(Counter.new(0, prefix: "#{num.print}.")) do |c, i| + section_names1(c, i.increment(c).print, lvl + 1) end num end def section_names1(clause, num, level) section_name_anchors(clause, num, level) - i = Counter.new + i = Counter.new(0, prefix: "#{num}.") clause.xpath(ns(SUBCLAUSES)).each do |c| - section_names1(c, "#{num}.#{i.increment(c).print}", level + 1) + section_names1(c, i.increment(c).print, level + 1) end end @@ -217,8 +218,9 @@ def annex_names(clause, num) annex_names1(clause.at(ns("./references | ./terms | ./definitions")), num.to_s, 1) else - clause.xpath(ns(SUBCLAUSES)).each_with_object(Counter.new) do |c, i| - annex_names1(c, "#{num}.#{i.increment(c).print}", 2) + clause.xpath(ns(SUBCLAUSES)) + .each_with_object(Counter.new(0, prefix: "#{num}.")) do |c, i| + annex_names1(c, i.increment(c).print, 2) end end hierarchical_asset_names(clause, num) @@ -226,9 +228,9 @@ def annex_names(clause, num) def annex_names1(clause, num, level) annex_name_anchors(clause, num, level) - i = Counter.new + i = Counter.new(0, prefix: "#{num}.") clause.xpath(ns(SUBCLAUSES)).each do |c| - annex_names1(c, "#{num}.#{i.increment(c).print}", level + 1) + annex_names1(c, i.increment(c).print, level + 1) end end diff --git a/spec/isodoc/xref_numbering_spec.rb b/spec/isodoc/xref_numbering_spec.rb index 7d8c9515..f9a14cf6 100644 --- a/spec/isodoc/xref_numbering_spec.rb +++ b/spec/isodoc/xref_numbering_spec.rb @@ -1,6 +1,7 @@ require "spec_helper" RSpec.describe IsoDoc do +=begin it "realises subsequences" do input = <<~INPUT @@ -282,6 +283,138 @@ .at("//xmlns:foreword").to_xml)) .to be_equivalent_to xmlpp(output) end +=end + it "realises branch-numbering overrides" do + input = <<~INPUT + + + +

+ + + + + + + + + + + + + + + + + + + + + + + + + + +

+
+
+ + + Scope +

Text

+ Subclause 1 + Subclause 2 + Subsubclause 1 +
+ Subclause 3 + Subclause 2 + Subclause 2 + + Subclause 2 + + Clause 3 + Subclause 2 + Subclause 2 + + + + Terms and definitions +

For the purposes of this document, the following terms and definitions apply.

+ + Term1 + + + Term1 + + + Term1 + +
+ + Abbreviated terms + +
+
+ + First Annex + Subclause 1 + Subclause 2 + Subsubclause 1 + + + Annex + Subclause 1 + Subclause 2 + Subsubclause 1 + + + + Normative references +

There are no normative references in this document.

+
+
+
+ INPUT + output = <<~OUTPUT + +

+ Clause 1 + Clause 1.1 + Clause 123 + Clause 1.2 + Clause 3 + Clause 124 + Clause 124.1 + Clause 3.1 + Clause 125 + Clause 126 + Clause 2 + Clause 125.1 + Clause 3bis + Clause 4bis + Clause 3bis.4bis + Clause 3bis.4bit + Clause 12bis + Annex A + Annex A.1 + Annex 123 + Annex A.2 + Annex B + Annex B.1 + Annex 123 + Annex B.2 + Clause 2bis +

+
+ OUTPUT + expect(xmlpp(Nokogiri::XML(IsoDoc::PresentationXMLConvert + .new(presxml_options) + .convert("test", input, true)) + .at("//xmlns:foreword").to_xml)) + .to be_equivalent_to xmlpp(output) + end + it "realises roman counter for xrefs" do a = IsoDoc::XrefGen::Counter.new(0, numerals: :roman)