Skip to content

Commit

Permalink
cover image processing: metanorma/metanorma-jis#227
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Oct 25, 2024
1 parent 17f4d33 commit 3ca4c40
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 9 deletions.
13 changes: 13 additions & 0 deletions lib/metanorma/standoc/cleanup_bibdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ def bibdata_cleanup(xmldoc)
bibdata_embed_hdr_cleanup(xmldoc) # feeds bibdata_embed_id_cleanup
bibdata_embed_id_cleanup(xmldoc)
biblio_indirect_erefs(xmldoc, @internal_eref_namespaces&.uniq)
coverpage_images(xmldoc)
end

def coverpage_images(xmldoc)
%w(coverpage-image innercoverpage-image tocside-image
backpage-image).each do |n|
xmldoc.xpath("//bibdata/ext/#{n}").each do |x|
ins = add_misc_container(xmldoc)
ins << "<presentation-metadata><name>#{n}</name>" \
"<value>#{x.remove.children.to_xml}</value>" \
"</presentation-metadata>"
end
end
end

def bibdata_anchor_cleanup(xmldoc)
Expand Down
26 changes: 17 additions & 9 deletions lib/metanorma/standoc/front.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "date"
require "nokogiri"
require "htmlentities"
require "pathname"
require_relative "./front_contributor"
Expand Down Expand Up @@ -91,9 +90,7 @@ def metadata_date(node, xml)
a == "date" || /^date_\d+$/.match(a) or next
type, date = node.attr(a).split(/ /, 2)
type or next
xml.date(type:) do |d|
d.on date
end
xml.date(type:) { |d| d.on date }
end
end

Expand Down Expand Up @@ -126,8 +123,7 @@ def metadata_relations(node, xml)

def relation_normalise(type)
type.sub(/-by$/, "By").sub(/-of$/, "Of").sub(/-from$/, "From")
.sub(/-in$/, "In")
.sub(/^has-([a-z])/) { "has#{$1.upcase}" }
.sub(/-in$/, "In").sub(/^has-([a-z])/) { "has#{$1.upcase}" }
end

def metadata_getrelation(node, xml, type, desc = nil)
Expand Down Expand Up @@ -193,6 +189,7 @@ def metadata_ext(node, ext)
metadata_flavor(node, ext)
metadata_committee(node, ext)
metadata_ics(node, ext)
metadata_coverpage_images(node, ext)
end

def metadata_doctype(node, xml)
Expand Down Expand Up @@ -220,9 +217,9 @@ def title_english(node, xml)
["en"].each do |lang|
at = { language: lang, format: "text/plain" }
xml.title **attr_code(at) do |t|
t << (Metanorma::Utils::asciidoc_sub(node.attr("title") ||
node.attr("title-en")) ||
node.title)
title = Metanorma::Utils::asciidoc_sub(node.attr("title") ||
node.attr("title-en"))
t << (title || node.title)
end
end
end
Expand All @@ -234,6 +231,17 @@ def title_otherlangs(node, xml)
xml.title v, { language: titlelang, format: "text/plain" }
end
end

def metadata_coverpage_images(node, xml)
%w(coverpage-image innercoverpage-image tocside-image
backpage-image).each do |n|
if a = node.attr(n)
xml.send n do |c|
a.split(",").each { |x| c.image src: x }
end
end
end
end
end
end
end
68 changes: 68 additions & 0 deletions spec/metanorma/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,74 @@
.to be_equivalent_to Xml::C14n.format(output)
end

it "populates cover images" do
input = <<~INPUT
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
:no-isobib:
:docnumber: 1000
:coverpage-image: images/image1.gif,images/image2.gif
:innercoverpage-image: images/image1.gif,images/image2.gif
:tocside-image: images/image1.gif,images/image2.gif
:backpage-image: images/image1.gif,images/image2.gif
INPUT
output = <<~OUTPUT
<metanorma-extension>
<presentation-metadata>
<name>coverpage-image</name>
<value>
<image src="images/image1.gif"/>
<image src="images/image2.gif"/>
</value>
</presentation-metadata>
<presentation-metadata>
<name>innercoverpage-image</name>
<value>
<image src="images/image1.gif"/>
<image src="images/image2.gif"/>
</value>
</presentation-metadata>
<presentation-metadata>
<name>tocside-image</name>
<value>
<image src="images/image1.gif"/>
<image src="images/image2.gif"/>
</value>
</presentation-metadata>
<presentation-metadata>
<name>backpage-image</name>
<value>
<image src="images/image1.gif"/>
<image src="images/image2.gif"/>
</value>
</presentation-metadata>
<presentation-metadata>
<name>TOC Heading Levels</name>
<value>2</value>
</presentation-metadata>
<presentation-metadata>
<name>HTML TOC Heading Levels</name>
<value>2</value>
</presentation-metadata>
<presentation-metadata>
<name>DOC TOC Heading Levels</name>
<value>2</value>
</presentation-metadata>
<presentation-metadata>
<name>PDF TOC Heading Levels</name>
<value>2</value>
</presentation-metadata>
</metanorma-extension>
OUTPUT
expect(Xml::C14n.format(strip_guid(Nokogiri::XML(Asciidoctor
.convert(input, *OPTIONS))
.at("//xmlns:metanorma-extension").to_xml)))
.to be_equivalent_to Xml::C14n.format(output)
end

it "reads scripts into blank HTML document" do
FileUtils.rm_f "test.html"
Asciidoctor.convert(<<~INPUT, *OPTIONS)
Expand Down

0 comments on commit 3ca4c40

Please sign in to comment.