Skip to content

Commit

Permalink
Merge pull request #548 from metanorma/fix/recursive-prefix-container
Browse files Browse the repository at this point in the history
allow recursive container prefixes for cross-references: https://gith…
  • Loading branch information
opoudjis authored Nov 28, 2023
2 parents bbf59f4 + 41721bd commit 7c87979
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 53 deletions.
15 changes: 6 additions & 9 deletions lib/isodoc/html_function/postprocess_cover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,21 @@ def html_toc_init(docxml)
end

def html_toc_entries(docxml, path)
xml = html_toc_entries_prep(docxml, path)
headers = html_toc_entries_prep(docxml, path)
path.each_with_index.with_object([]) do |(p, i), m|
xml.xpath(p.join(" | ")).each do |h|
docxml.xpath(p.join(" | ")).each do |h|
m << { entry: html_toc_entry("h#{i + 1}", h),
line: h.line }
line: headers[h["id"]] }
end
end.sort_by { |k| k[:line] }
end

def html_toc_entries_prep(docxml, path)
path.each do |p|
docxml.xpath(p.join(" | ")).each do |h|
docxml.xpath(path.join(" | "))
.each_with_index.with_object({}) do |(h, i), m|
h["id"] ||= "_#{UUIDTools::UUID.random_create}"
m[h["id"]] = i
end
end
xml = Nokogiri::XML(docxml.to_xml, &:noblanks)
xml.remove_namespaces!
xml
end

def toc_exclude_class
Expand Down
12 changes: 8 additions & 4 deletions lib/isodoc/presentation_function/xrefs.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module IsoDoc
class PresentationXMLConvert < ::IsoDoc::Convert
def prefix_container(container, linkend, node, _target)
l10n(@i18n.nested_xref.sub("%1", anchor_xref(node, container))
def prefix_container(container, linkend, node, target)
prefix_container?(container, node) or return linkend
container_container = @xrefs.anchor(container, :container, false)
container_label =
prefix_container(container_container, anchor_xref(node, container),
node, target)
l10n(@i18n.nested_xref.sub("%1", container_label)
.sub("%2", linkend))
end

Expand Down Expand Up @@ -31,8 +36,7 @@ def anchor_id_postproc(node)
def anchor_linkend1(node)
linkend = anchor_xref(node, node["target"])
container = @xrefs.anchor(node["target"], :container, false)
prefix_container?(container, node) and
linkend = prefix_container(container, linkend, node, node["target"])
linkend = prefix_container(container, linkend, node, node["target"])
capitalise_xref(node, linkend, anchor_value(node["target"]))
end

Expand Down
73 changes: 39 additions & 34 deletions lib/isodoc/xref/xref_gen_seq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ def subfigure_increment(idx, counter, elem)
.//figure[not(@class)] | .//figure[@class = 'pseudocode'] | .//sourcecode[not(ancestor::example)]
XPATH

def sequential_figure_names(clause)
def sequential_figure_names(clause, container: false)
c = Counter.new
j = 0
clause.xpath(ns(FIGURE_NO_CLASS)).noblank.each do |t|
j = subfigure_increment(j, c, t)
sequential_figure_body(j, c, t, "figure")
sequential_figure_body(j, c, t, "figure", container: container)
end
sequential_figure_class_names(clause)
sequential_figure_class_names(clause, container: container)
end

def sequential_figure_class_names(clause)
def sequential_figure_class_names(clause, container: false)
c = {}
j = 0
clause.xpath(ns(".//figure[@class][not(@class = 'pseudocode')]"))
.each do |t|
c[t["class"]] ||= Counter.new
j = subfigure_increment(j, c[t["class"]], t)
sequential_figure_body(j, c[t["class"]], t, t["class"])
sequential_figure_body(j, c[t["class"]], t, t["class"],
container: container)
end
end

Expand All @@ -50,27 +51,28 @@ def subfigure_label(subfignum)
"-#{subfignum}"
end

def sequential_figure_body(subfignum, counter, block, klass)
def sequential_figure_body(subfig, counter, elem, klass, container: false)
label = counter.print
label &&= label + subfigure_label(subfignum)
@anchors[block["id"]] = anchor_struct(
label, nil, @labels[klass] || klass.capitalize, klass,
block["unnumbered"]
label &&= label + subfigure_label(subfig)
@anchors[elem["id"]] = anchor_struct(
label, container ? elem : nil,
@labels[klass] || klass.capitalize, klass,
elem["unnumbered"]
)
end

def sequential_table_names(clause)
def sequential_table_names(clause, container: false)
c = Counter.new
clause.xpath(ns(".//table")).noblank.each do |t|
labelled_ancestor(t) and next
@anchors[t["id"]] = anchor_struct(
c.increment(t).print, nil,
c.increment(t).print, container ? t : nil,
@labels["table"], "table", t["unnumbered"]
)
end
end

def sequential_formula_names(clause)
def sequential_formula_names(clause, container: false)
c = Counter.new
clause.xpath(ns(".//formula")).noblank.each do |t|
@anchors[t["id"]] = anchor_struct(
Expand All @@ -91,60 +93,63 @@ def sequential_formula_names(clause)
./permission | ./requirement | ./recommendation
XPATH

def sequential_permission_names(clause)
def sequential_permission_names(clause, container: false)
c = ReqCounter.new
clause.xpath(ns(FIRST_LVL_REQ)).noblank.each do |t|
m = @reqt_models.model(t["model"])
klass, label = reqt2class_label(t, m)
id = c.increment(label, t).print
sequential_permission_body(id, t, label, klass, m)
sequential_permission_children(t, id)
sequential_permission_body(id, t, label, klass, m,
container: container)
sequential_permission_children(t, id, container: container)
end
end

def sequential_permission_children(block, lbl)
def sequential_permission_children(elem, lbl, container: false)
c = ReqCounter.new
block.xpath(ns(REQ_CHILDREN)).noblank.each do |t|
elem.xpath(ns(REQ_CHILDREN)).noblank.each do |t|
m = @reqt_models.model(t["model"])
klass, label = reqt2class_nested_label(t, m)
id = "#{lbl}#{hierfigsep}#{c.increment(label, t).print}"
sequential_permission_body(id, t, label, klass, m)
sequential_permission_children(t, id)
sequential_permission_body(id, t, label, klass, m,
container: container)
sequential_permission_children(t, id, container: container)
end
end

def sequential_permission_body(id, block, label, klass, model)
@anchors[block["id"]] = model.postprocess_anchor_struct(
block, anchor_struct(id, block,
label, klass, block["unnumbered"])
def sequential_permission_body(id, elem, label, klass, model, container: false)
@anchors[elem["id"]] = model.postprocess_anchor_struct(
elem, anchor_struct(id, elem,
label, klass, elem["unnumbered"])
)
model.permission_parts(block, id, label, klass).each do |n|
model.permission_parts(elem, id, label, klass).each do |n|
@anchors[n[:id]] = anchor_struct(n[:number], n[:elem], n[:label],
n[:klass], false)
end
end

def reqt2class_label(block, model)
def reqt2class_label(elem, model)
model.req_class_paths.each do |n|
v1 = ns("/#{n[:xpath]}").sub(%r{^/}, "")
block.at("./self::#{v1}") and return [n[:klass], n[:label]]
elem.at("./self::#{v1}") and return [n[:klass], n[:label]]
end
[nil, nil]
end

def reqt2class_nested_label(block, model)
def reqt2class_nested_label(elem, model)
model.req_nested_class_paths.each do |n|
v1 = ns("/#{n[:xpath]}").sub(%r{^/}, "")
block.at("./self::#{v1}") and return [n[:klass], n[:label]]
elem.at("./self::#{v1}") and return [n[:klass], n[:label]]
end
[nil, nil]
end

def sequential_asset_names(clause)
sequential_table_names(clause)
sequential_figure_names(clause)
sequential_formula_names(clause)
sequential_permission_names(clause)
# container makes numbering be prefixed with the parent clause reference
def sequential_asset_names(clause, container: false)
sequential_table_names(clause, container: container)
sequential_figure_names(clause, container: container)
sequential_formula_names(clause, container: container)
sequential_permission_names(clause, container: container)
end

def hierarchical_figure_names(clause, num)
Expand Down
8 changes: 6 additions & 2 deletions lib/isodoc/xref/xref_sect_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def preface_names(clause)

def back_names(clause)
unnumbered_names(clause)
sequential_asset_names(
Nokogiri::XML::NodeSet.new(clause.document, [clause]),
container: true,
)
end

def unnumbered_names(clause)
Expand Down Expand Up @@ -237,8 +241,8 @@ def annex_names1(clause, num, level)
def reference_names(ref)
ids = @klass.bibitem_ref_code(ref)
identifiers = @klass.render_identifier(ids)
reference = @klass.
docid_l10n(identifiers[:metanorma] || identifiers[:sdo] ||
reference = @klass
.docid_l10n(identifiers[:metanorma] || identifiers[:sdo] ||
identifiers[:ordinal] || identifiers[:doi])
@anchors[ref["id"]] = { xref: reference }
end
Expand Down
3 changes: 1 addition & 2 deletions spec/isodoc/xref_numbering_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -283,7 +282,7 @@
.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">
Expand Down
Loading

0 comments on commit 7c87979

Please sign in to comment.