Skip to content

Commit

Permalink
Merge pull request #588 from metanorma/fix/asset-figure-label
Browse files Browse the repository at this point in the history
Fix/asset figure label
  • Loading branch information
opoudjis authored Jul 31, 2024
2 parents 384e337 + 7c7d1d4 commit ec02d6f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 36 deletions.
5 changes: 2 additions & 3 deletions lib/isodoc/function/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,9 @@ def image_localfile(img)
%w(example requirement recommendation permission
note table figure sourcecode).freeze

def labelled_ancestor(elem)
#!elem.path.gsub(/\[\d+\]/, "").split(%r{/})[1..-1]
def labelled_ancestor(elem, exceptions = [])
!elem.ancestors.map(&:name)
.intersection(LABELLED_ANCESTOR_ELEMENTS).empty?
.intersection(LABELLED_ANCESTOR_ELEMENTS - exceptions).empty?
end

def emf?(type)
Expand Down
8 changes: 4 additions & 4 deletions lib/isodoc/presentation_function/erefs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def eref_localities_conflated(refs, target, node)
ret = resolve_eref_connectives(eref_locality_stacks(refs, target,
node))
node.delete("droploc") unless droploc
eref_localities1({ target: target, number: "pl",
eref_localities1({ target:, number: "pl",
type: refs.first.at(ns("./locality/@type")).text,
from: l10n(ret[1..-1].join), node: node, lang: @lang })
from: l10n(ret[1..-1].join), node:, lang: @lang })
end

def can_conflate_eref_rendering?(refs)
Expand Down Expand Up @@ -143,9 +143,9 @@ def locality_delimiter(_loc)
def eref_localities0(ref, _idx, target, node)
if ref["type"] == "whole" then @i18n.wholeoftext
else
eref_localities1({ target: target, type: ref["type"], number: "sg",
eref_localities1({ target:, type: ref["type"], number: "sg",
from: ref.at(ns("./referenceFrom"))&.text,
upto: ref.at(ns("./referenceTo"))&.text, node: node,
upto: ref.at(ns("./referenceTo"))&.text, node:,
lang: @lang })
end
end
Expand Down
17 changes: 10 additions & 7 deletions lib/isodoc/presentation_function/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,26 @@ def svg_extract(elem)

svg = Base64.strict_decode64(elem["src"]
.sub(%r{^data:image/svg\+xml;(charset=[^;]+;)?base64,}, ""))
x = Nokogiri::XML.fragment(svg.sub(/<\?xml[^>]*>/, "")) do |config|
config.huge
end
x = Nokogiri::XML.fragment(svg.sub(/<\?xml[^>]*>/, ""), &:huge)
elem["src"] = ""
elem.children = x
end

def figure1(elem)
return sourcecode1(elem) if elem["class"] == "pseudocode" ||
elem["type"] == "pseudocode"
return if elem.at(ns("./figure")) && !elem.at(ns("./name"))

elem["class"] == "pseudocode" || elem["type"] == "pseudocode" and
return sourcecode1(elem)
figure_label?(elem) or return nil
lbl = @xrefs.anchor(elem["id"], :label, false) or return
# no xref, no label: this can be set in xref
prefix_name(elem, block_delim,
l10n("#{figure_label(elem)} #{lbl}"), "name")
end

def figure_label?(elem)
elem.at(ns("./figure")) && !elem.at(ns("./name")) and return false
true
end

def figure_label(elem)
klass = elem["class"] || "figure"
klasslbl = @i18n.get[klass] || klass
Expand Down
2 changes: 1 addition & 1 deletion lib/isodoc/presentation_function/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def attachments_extract(docxml)
end

def save_attachment(attachment, dir)
n = File.join(dir, attachment["name"])
n = File.join(dir, File.basename(attachment["name"]))
c = attachment.text.sub(%r{^data:[^;]+;(?:charset=[^;]+;)?base64,}, "")
File.open(n, "wb") { |f| f.write(Base64.strict_decode64(c)) }
end
Expand Down
5 changes: 4 additions & 1 deletion lib/isodoc/xref/xref_gen_seq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def sequential_figure_names(clause, container: false)
c = Counter.new
j = 0
clause.xpath(ns(self.class::FIGURE_NO_CLASS)).noblank.each do |t|
# labelled_ancestor(t, %w(figure)) and next # disable nested figure labelling
j = subfigure_increment(j, c, t)
sequential_figure_body(j, c, t, "figure", container:)
end
Expand All @@ -40,6 +41,7 @@ def sequential_figure_class_names(clause, container: false)
clause.xpath(ns(".//figure[@class][not(@class = 'pseudocode')]"))
.each do |t|
c[t["class"]] ||= Counter.new
# labelled_ancestor(t, %w(figure)) and next
j = subfigure_increment(j, c[t["class"]], t)
sequential_figure_body(j, c[t["class"]], t, t["class"],
container:)
Expand Down Expand Up @@ -157,7 +159,7 @@ def hierarchical_figure_names(clause, num)
c = Counter.new
j = 0
clause.xpath(ns(self.class::FIGURE_NO_CLASS)).noblank.each do |t|
# next if labelled_ancestor(t) && t.ancestors("figure").empty?
# labelled_ancestor(t, %w(figure)) and next
j = subfigure_increment(j, c, t)
hierarchical_figure_body(num, j, c, t, "figure")
end
Expand All @@ -169,6 +171,7 @@ def hierarchical_figure_class_names(clause, num)
j = 0
clause.xpath(ns(".//figure[@class][not(@class = 'pseudocode')]"))
.noblank.each do |t|
# labelled_ancestor(t, %w(figure)) and next
c[t["class"]] ||= Counter.new
j = subfigure_increment(j, c[t["class"]], t)
hierarchical_figure_body(num, j, c[t["class"]], t, t["class"])
Expand Down
3 changes: 2 additions & 1 deletion spec/isodoc/presentation_xml_maths_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
</preface>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(output)
Expand Down
89 changes: 70 additions & 19 deletions spec/isodoc/presentation_xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
</sections>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(output)
end

Expand Down Expand Up @@ -331,14 +332,17 @@
</body>
</html>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
expect(Xml::C14n.format(IsoDoc::HtmlConvert.new({})
.convert("test", presxml, true))).to be_equivalent_to Xml::C14n.format(html)
.convert("test", presxml, true)))
.to be_equivalent_to Xml::C14n.format(html)
expect(Xml::C14n.format(IsoDoc::WordConvert.new({})
.convert("test", presxml, true))).to be_equivalent_to Xml::C14n.format(doc)
.convert("test", presxml, true)))
.to be_equivalent_to Xml::C14n.format(doc)
end

it "duplicates EMF and SVG files" do
Expand Down Expand Up @@ -692,9 +696,10 @@
</sections>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
.convert("test", input, true)
.sub(%r{<localized-strings>.*</localized-strings>}m, "")
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true)
.sub(%r{<localized-strings>.*</localized-strings>}m, "")
.gsub(%r{src="[^"]+?\.emf"}, 'src="_.emf"')
.gsub(%r{src="[^"]+?\.svg"}, 'src="_.svg"'))))
.to be_equivalent_to (output)
Expand Down Expand Up @@ -729,7 +734,8 @@
</sections>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true)
.sub(%r{<localized-strings>.*</localized-strings>}m, "")
.gsub(%r{src="[^"]+?\.svg"}, 'src="_.svg"'))))
Expand Down Expand Up @@ -840,7 +846,8 @@
</sections>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -895,7 +902,8 @@
</sections>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1146,7 +1154,7 @@
<example id='C1'>
<name>EXAMPLE 1</name>
<figure id='B2'>
<name>Figure 2&#xA0;&#x2014; Second</name>
<name>Figure 2 — Second</name>
</figure>
</example>
<example id='C2'>
Expand All @@ -1162,7 +1170,8 @@
</sections>
</iso-standard>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1220,7 +1229,8 @@
</bibliography>
</standard-document>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1254,7 +1264,8 @@
</sections>
</standard-document>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1301,7 +1312,8 @@
</preface>
</standard-document>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1348,7 +1360,8 @@
</preface>
</standard-document>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1387,7 +1400,8 @@
</preface>
</standard-document>
OUTPUT
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert.new(presxml_options)
expect(Xml::C14n.format(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
Expand Down Expand Up @@ -1668,4 +1682,41 @@
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to Xml::C14n.format(presxml)
end

it "label figures embedded within other assets" do
input = <<~INPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="semantic">
<preface>
<foreword id="A">
<p id="B">
<table id="C">
<tbody><td>
<figure id="D">X</figure>
</td>
</tbody>
</table>
</p>
</foreword></preface></standard-document>
INPUT
presxml = <<~OUTPUT
<standard-document xmlns="https://www.metanorma.org/ns/standoc" type="presentation">
<preface><clause type="toc" id="_" displayorder="1"><title depth="1">Table of contents</title></clause>
<foreword id="A" displayorder="2">
<p id="B">
<table id="C"><name>Table 1</name>
<tbody><td>
<figure id="D"><name>Figure 1</name>X</figure>
</td>
</tbody>
</table>
</p>
</foreword></preface></standard-document>
OUTPUT
expect(strip_guid(IsoDoc::PresentationXMLConvert
.new(presxml_options)
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, ""))
.to be_equivalent_to (presxml)
end
end

0 comments on commit ec02d6f

Please sign in to comment.