Skip to content

Commit

Permalink
Merge pull request #544 from metanorma/features/branch-number
Browse files Browse the repository at this point in the history
branch-number override of section numbering: https://github.com/metan…
  • Loading branch information
opoudjis authored Nov 13, 2023
2 parents 6e8ddd7 + 6f6586a commit f6025d1
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 17 deletions.
26 changes: 17 additions & 9 deletions lib/isodoc/xref/xref_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand All @@ -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 = ""
/^(?<b>.*?)(?<n>\d+)$/ =~ node["number"]
if blank?(n)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions lib/isodoc/xref/xref_sect_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -217,18 +218,19 @@ 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)
end

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

Expand Down
133 changes: 133 additions & 0 deletions spec/isodoc/xref_numbering_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "spec_helper"

RSpec.describe IsoDoc do
=begin
it "realises subsequences" do
input = <<~INPUT
<iso-standard xmlns="http://riboseinc.com/isoxml">
Expand Down Expand Up @@ -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
<iso-standard xmlns="http://riboseinc.com/isoxml">
<preface>
<foreword id="fwd">
<p>
<xref target="S1"/>
<xref target="S2"/>
<xref target="S3"/>
<xref target="S4"/>
<xref target="S5"/>
<xref target="S6"/>
<xref target="S7"/>
<xref target="S7a"/>
<xref target="S8"/>
<xref target="S9"/>
<xref target="S10"/>
<xref target="S10a"/>
<xref target="S11"/>
<xref target="S12"/>
<xref target="S13"/>
<xref target="S14"/>
<xref target="S15"/>
<xref target="S16"/>
<xref target="S17"/>
<xref target="S18"/>
<xref target="S19"/>
<xref target="S20"/>
<xref target="S21"/>
<xref target="S22"/>
<xref target="S23"/>
<xref target="S24"/>
</p>
</foreword>
</preface>
<sections>
<clause id='S1' type='scope' inline-header='false' obligation='normative'>
<title>Scope</title>
<p id='_'>Text</p>
<clause id="S2"><title>Subclause 1</title></clause>
<clause branch-number="123" id="S3"><title>Subclause 2</title></clause>
<clause id="S4"><title>Subsubclause 1</title></clause>
</clause>
<clause id="S5"><title>Subclause 3</title>
<clause id="S6" branch-number="124"><title>Subclause 2</title>
<clause id="S7"><title>Subclause 2</title></clause>
</clause>
<clause id="S7a"><title>Subclause 2</title></clause>
</clause>
<clause id="S8" branch-number="125"><title>Clause 3</title>
<clause id="S9" branch-number="126"><title>Subclause 2</title></clause>
<clause id="S10"><title>Subclause 2</title></clause>
</clause>
<clause id="S10a">
<terms id='S11' branch-number='3bis' obligation='normative'>
<title>Terms and definitions</title>
<p id='_'>For the purposes of this document, the following terms and definitions apply.</p>
<term id='S12' branch-number='4bis'>
<preferred><expression><name>Term1</name></expression></preferred>
</term>
<term id='S13' number='4bis'>
<preferred><expression><name>Term1</name></expression></preferred>
</term>
<term id='S14'>
<preferred><expression><name>Term1</name></expression></preferred>
</term>
</terms>
<definitions id='S15' branch-number='12bis' type='abbreviated_terms' obligation='normative'>
<title>Abbreviated terms</title>
</definitions>
</clause>
</sections>
<annex id='S16' obligation='normative'>
<title>First Annex</title>
<clause id="S17"><title>Subclause 1</title></clause>
<clause branch-number="123" id="S18"><title>Subclause 2</title></clause>
<clause id="S19"><title>Subsubclause 1</title></clause>
</annex>
<annex id='S20' branch-number='17bis' inline-header='false' obligation='normative'>
<title>Annex</title>
<clause id="S21"><title>Subclause 1</title></clause>
<clause branch-number="123" id="S22"><title>Subclause 2</title></clause>
<clause id="S23"><title>Subsubclause 1</title></clause>
</annex>
<bibliography>
<references id='S24' branch-number='2bis' normative='true' obligation='informative'>
<title>Normative references</title>
<p id='_'>There are no normative references in this document.</p>
</references>
</bibliography>
</iso-standard>
INPUT
output = <<~OUTPUT
<foreword id="fwd" displayorder="2">
<p>
<xref target="S1">Clause 1</xref>
<xref target="S2">Clause 1.1</xref>
<xref target="S3">Clause 123</xref>
<xref target="S4">Clause 1.2</xref>
<xref target="S5">Clause 3</xref>
<xref target="S6">Clause 124</xref>
<xref target="S7">Clause 124.1</xref>
<xref target="S7a">Clause 3.1</xref>
<xref target="S8">Clause 125</xref>
<xref target="S9">Clause 126</xref>
<xref target="S10a">Clause 2</xref>
<xref target="S10">Clause 125.1</xref>
<xref target="S11">Clause 3bis</xref>
<xref target="S12">Clause 4bis</xref>
<xref target="S13">Clause 3bis.4bis</xref>
<xref target="S14">Clause 3bis.4bit</xref>
<xref target="S15">Clause 12bis</xref>
<xref target="S16">Annex A</xref>
<xref target="S17">Annex A.1</xref>
<xref target="S18">Annex 123</xref>
<xref target="S19">Annex A.2</xref>
<xref target="S20">Annex B</xref>
<xref target="S21">Annex B.1</xref>
<xref target="S22">Annex 123</xref>
<xref target="S23">Annex B.2</xref>
<xref target="S24">Clause 2bis</xref>
</p>
</foreword>
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)
Expand Down

0 comments on commit f6025d1

Please sign in to comment.