Skip to content

Commit

Permalink
refactor display_order metanorma/metanorma-bsi#419
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Aug 26, 2023
1 parent 1c814ee commit 5eeb98b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 89 deletions.
2 changes: 2 additions & 0 deletions Gemfile.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "features/refactor-display-order"

2 changes: 2 additions & 0 deletions lib/isodoc/un/presentation_xml_convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def toc_title(docxml)
super
end

def move_norm_ref_to_sections(docxml); end

include Init
end
end
Expand Down
95 changes: 36 additions & 59 deletions lib/isodoc/un/xref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,36 @@
module IsoDoc
module UN
class Xref < IsoDoc::Xref
def initial_anchor_names(doc)
if @parse_settings.empty? || @parse_settings[:clauses]
preface_names(doc.at(ns("//preface/abstract")))
preface_names(doc.at(ns("//foreword")))
preface_names(doc.at(ns("//introduction")))
doc.xpath(ns("//preface/clause")).each do |c|
preface_names(c)
end
preface_names(doc.at(ns("//acknowledgements")))
clause_names(doc, 0)
end
if @parse_settings.empty?
sequential_asset_names(
doc.xpath(ns("//preface/abstract | //foreword | //introduction | "\
"//preface/clause | //acknowledgements")),
)
middle_section_asset_names(doc)
termnote_anchor_names(doc)
termexample_anchor_names(doc)
end
def clause_order_main(_docxml)
[{ path: "//sections/clause | //sections/terms | //sections/definitions", multi: true }]
end

def asset_anchor_names(doc)
super
@parse_settings.empty? or return
sequential_asset_names(
doc.xpath(ns("//preface/abstract | //foreword | //introduction | "\
doc.xpath(ns("//preface/abstract | //foreword | //introduction | " \
"//preface/clause | //acknowledgements")),
)
end

def clause_names(docxml, _sect_num)
q = "//clause[parent::sections]"
def main_anchor_names(xml)
@paranumber = 0
i = 0
docxml.xpath(ns(q)).each do |c|
section_names(c, i, 1)
i += 1
end
super
end

def levelnumber(num, lvl)
case lvl % 3
when 1 then RomanNumerals.to_roman(num)
when 2 then ("A".ord + num - 1).chr
when 1 then RomanNumerals.to_roman(num.to_i)
when 2 then ("A".ord + num.to_i - 1).chr
when 0 then num.to_s
end
end

def annex_levelnum(num, lvl)
case lvl % 3
when 0 then RomanNumerals.to_roman(num)
when 1 then ("A".ord + num - 1).chr
when 0 then RomanNumerals.to_roman(num.to_i)
when 1 then ("A".ord + num.to_i - 1).chr
when 2 then num.to_s
end
end
Expand All @@ -70,6 +47,7 @@ def leaf_section?(clause)
end

def label_leaf_section(clause, lvl)
@paranumber.nil? and @paranumber = 0
@paranumber += 1
@anchors[clause["id"]] =
{ label: @paranumber.to_s, elem: @labels["paragraph"],
Expand All @@ -86,11 +64,9 @@ def label_annex_leaf_section(clause, num, lvl)
end

def section_names(clause, num, lvl)
return num if clause.nil?

clause.nil? and return num
leaf_section?(clause) and label_leaf_section(clause, lvl) and return
num = num + 1
lbl = levelnumber(num, 1)
lbl = levelnumber(num.increment(clause).print, 1)
@anchors[clause["id"]] = { label: lbl, level: lvl, type: "clause",
elem: @labels["clause"],
xref: l10n("#{@labels['clause']} #{lbl}") }
Expand All @@ -116,6 +92,16 @@ def section_names1(clause, num, level)
end
end

def annex_anchor_names(xml)
i = ::IsoDoc::XrefGen::Counter.new
clause_order_annex(xml).each do |a|
xml.xpath(ns(a[:path])).each do |c|
annex_names(c, i.increment(c).print)
a[:multi] or break
end
end
end

def annex_name_lbl(_clause, num)
l10n("<strong>#{@labels['annex']} #{num}</strong>")
end
Expand All @@ -130,7 +116,9 @@ def annex_name_anchors(clause, num)
xref: "#{@labels['annex']} #{num}" }
end

def annex_names(clause, num)
def annex_names(clause, number)
@paranumber = 0
num = annex_levelnum(number, 0)
hierarchical_asset_names(clause, num)
leaf_section?(clause) and
label_annex_leaf_section(clause, num, 1) and return
Expand All @@ -141,8 +129,7 @@ def annex_names(clause, num)
else
i = 1
clause.xpath(ns(SUBCLAUSES)).each do |c|
next if c["unnumbered"] == "true"

c["unnumbered"] == "true" and next
annex_names1(c, "#{num}.#{annex_levelnum(i, 2)}", 2)
i += 1 if !leaf_section?(c)
end
Expand All @@ -157,32 +144,24 @@ def annex_names1(clause, num, level)
level: level, type: "clause", elem: @labels["annex"] }
i = 1
clause.xpath(ns("./clause | ./references")).each do |c|
next if c["unnumbered"] == "true"

c["unnumbered"] == "true" and next
annex_names1(c, "#{num}.#{annex_levelnum(i, level + 1)}", level + 1)
i += 1 if !leaf_section?(c)
end
end

def back_anchor_names(docxml)
docxml.xpath(ns("//annex")).each_with_index do |c, i|
@paranumber = 0
annex_names(c, RomanNumerals.to_roman(i + 1))
end
docxml.xpath(ns("//bibliography/clause |"\
"//bibliography/references")).each do |b|
preface_names(b)
end
docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |ref|
reference_names(ref)
end
def clause_order_back(_docxml)
[
{ path: "//bibliography/references | //bibliography/clause",
multi: true },
{ path: "//indexsect", multi: true },
{ path: "//colophon/*", multi: true },
]
end

def sequential_admonition_names(clause)
i = 0
clause.xpath(ns(".//admonition")).each do |t|
next if t["id"].nil? || t["id"].empty?

clause.xpath(ns(".//admonition")).noblank.each do |t|
i += 1 unless t["unnumbered"] == "true"
@anchors[t["id"]] = anchor_struct(i.to_s, nil, @labels["admonition"],
"box", t["unnumbered"])
Expand All @@ -191,9 +170,7 @@ def sequential_admonition_names(clause)

def hierarchical_admonition_names(clause, num)
i = 0
clause.xpath(ns(".//admonition")).each do |t|
next if t["id"].nil? || t["id"].empty?

clause.xpath(ns(".//admonition")).noblank.each do |t|
i += 1 unless t["unnumbered"] == "true"
@anchors[t["id"]] =
anchor_struct("#{num}.#{i}", nil, @labels["admonition"], "box",
Expand Down
5 changes: 4 additions & 1 deletion lib/metanorma/un/isodoc.rng
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
these elements; we just want one namespace for any child grammars
of this.
-->
<!-- VERSION v1.2.3 -->
<!-- VERSION v1.2.4 -->
<grammar xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<include href="reqt.rng"/>
<include href="basicdoc.rng">
Expand Down Expand Up @@ -2068,6 +2068,9 @@
</define>
<define name="termdefinition">
<element name="definition">
<optional>
<attribute name="type"/>
</optional>
<choice>
<ref name="verbaldefinition"/>
<ref name="nonverbalrep"/>
Expand Down
Loading

0 comments on commit 5eeb98b

Please sign in to comment.