Skip to content

Commit

Permalink
index generation, primary, secondary, tertiary terms: #67
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Dec 11, 2020
1 parent ea7cb08 commit 56cdc52
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 109 deletions.
4 changes: 3 additions & 1 deletion lib/asciidoctor/bipm/bipm.rng
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@
<optional>
<ref name="boilerplate"/>
</optional>
<ref name="preface"/>
<optional>
<ref name="preface"/>
</optional>
<oneOrMore>
<ref name="sections"/>
</oneOrMore>
Expand Down
1 change: 1 addition & 0 deletions lib/isodoc/bipm/i18n-en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ subclause: Section
formula: Equation
inequality: Inequality
bibliography: References
index: Index
1 change: 1 addition & 0 deletions lib/isodoc/bipm/i18n-fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ requirement: exigence
recommendation: recommandation
permission: autorisation
example_xref: exemple
index: Index
doctype_dict:
brochure: brochure
mise-en-pratique: mise-en-pratique
Expand Down
88 changes: 87 additions & 1 deletion lib/isodoc/bipm/presentation_xml_convert.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,96 @@
require "isodoc"
require "metanorma-generic"
require "twitter_cldr"
require "sterile"
require_relative "init"

module IsoDoc
module BIPM
class PresentationXMLConvert < IsoDoc::Generic::PresentationXMLConvert
def section(docxml)
super
index(docxml)
end

def add_id
%(id="_#{UUIDTools::UUID.random_create}")
end

def index(docxml)
return unless docxml.at(ns("//index"))
i = docxml.root.add_child "<clause type='index' #{add_id}><title>#{@i18n.index}</title></clause>"
index = sort_indexterms(docxml.xpath(ns("//index")))
index.keys.sort.each do |k|
c = i.first.add_child "<clause #{add_id}><title>#{k}</title><ul></ul></clause>"
words = index[k].keys.each_with_object({}) { |w, v| v[w.downcase] = w }
words.keys.localize(@lang.to_sym).sort.to_a.each do |w|
c.first.at(ns("./ul")).add_child index_entries(words, index[k], w)
end
end
@xrefs.bookmark_anchor_names(docxml.xpath(ns(@xrefs.sections_xpath)))
end

def index_entries(words, index, primary)
ret = index_entries_head(words[primary], index.dig(words[primary], nil, nil))
words2 = index[words[primary]]&.keys&.reject { |k| k.nil?}&.each_with_object({}) { |w, v| v[w.downcase] = w }
unless words2.empty?
ret += "<ul>"
words2.keys.localize(@lang.to_sym).sort.to_a.each do |w|
ret += index_entries2(words2, index[words[primary]], w)
end
ret += "</ul>"
end
ret + "</li>"
end

def index_entries2(words, index, secondary)
ret = index_entries_head(words[secondary], index.dig(words[secondary], nil))
words3 = index[words[secondary]]&.keys&.reject { |k| k.nil?}&.each_with_object({}) { |w, v| v[w.downcase] = w }
unless words3.empty?
ret += "<ul>"
words3.keys.localize(@lang.to_sym).sort.to_a.each do |w|
ret += (index_entries_head(words3[w], index[words[secondary]][words3[w]]) + "</li>")
end
ret += "</ul>"
end
ret + "</li>"
end

def index_entries_head(head, entries)
ret = "<li>#{head}"
e1 = entries&.join(", ")
(e1.nil? || e1.empty?) ? ret : ret + ", #{e1}"
end

def sort_indexterms(terms)
index = extract_indexterms(terms)
index.keys.sort.each_with_object({}) do |k, v|
v[k[0].upcase.transliterate] ||= {}
v[k[0].upcase.transliterate][k] = index[k]
end
end

def extract_indexterms(terms)
terms.each_with_object({}) do |t, v|
term = t["primary"]
term2 = t["secondary"]
term3 = t["tertiary"]
index2bookmark(t)
v[term] ||= {}
v[term][term2] ||= {}
v[term][term2][term3] ||= []
v[term][term2][term3] << "<xref target='#{t['id']}' pagenumber='true'/>"
end
end

def index2bookmark(t)
t.name = "bookmark"
t.delete("primary")
t.delete("secondary")
t.delete("tertiary")
t["id"] = "_#{UUIDTools::UUID.random_create}"
end

def table1(f)
return if labelled_ancestor(f)
return if f["unnumbered"] && !f.at(ns("./name"))
Expand Down Expand Up @@ -113,7 +199,7 @@ def twitter_cldr_localiser_symbols
end

def mathml1(f, locale)
localize_maths(f, locale)
localize_maths(f, locale)
end

include Init
Expand Down
217 changes: 110 additions & 107 deletions spec/isodoc/html_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1599,113 +1599,116 @@
OUTPUT
end

it "splits bilingual collection PDF" do
FileUtils.rm_f("test.pdf")
FileUtils.rm_f("test_fr.pdf")
FileUtils.rm_f("test_en.pdf")
IsoDoc::BIPM::PdfConvert.new({}).convert('test', <<~"INPUT", false)
<metanorma-collection xmlns="http://metanorma.org">
<bibdata type="collection">
<title format="text/plain" language="en">The International Temperature Scale of 1990 (ITS-90)</title>
</bibdata>
<manifest>
<level>brochure</level>
<title>Brochure/Brochure</title>
<docref fileref="brochure-its90-fr.xml" id="doc000000000">
<identifier>brochure-its90-fr</identifier>
</docref>
<docref fileref="brochure-its90-en.xml" id="doc000000001">
<identifier>brochure-its90-en</identifier>
</docref>
</manifest>
<doc-container id="doc000000000">
<bipm-standard xmlns="https://www.metanorma.org/ns/bipm" type="presentation" version="0.0.5">
<bibdata type="standard">
<title language="en" format="text/plain" type="main">The International Temperature Scale of 1990 (ITS-90)</title>
<title language="en" format="text/plain" type="cover">The International System of Units (SI)</title>
<title language="fr" format="text/plain" type="main">&#xC9;chelle Internationale de Temp&#xE9;rature De 1990 (EIT-90)</title>
<title language="fr" format="text/plain" type="cover">Le Syst&#xE8;me international d&#x2019;unit&#xE9;s (SI)</title>
<docidentifier type="BIPM">BIPM PLTS-2000</docidentifier>
<language current="true">fr</language>
<script current="true">Latn</script>
</bibdata>
<preface><clause id="_abstract" obligation="informative"><title depth="1">Abstract</title></clause></preface>
</bipm-standard>
</doc-container>
<doc-container id="doc000000001">
<bipm-standard xmlns="https://www.metanorma.org/ns/bipm" type="presentation" version="0.0.5">
<bibdata type="standard">
<title language="en" format="text/plain" type="main">The International Temperature Scale of 1990 (ITS-90)</title>
<title language="en" format="text/plain" type="cover">The International System of Units (SI)</title>
<title language="fr" format="text/plain" type="main">&#xC9;chelle Internationale de Temp&#xE9;rature De 1990 (EIT-90)</title>
<title language="fr" format="text/plain" type="cover">Le Syst&#xE8;me international d&#x2019;unit&#xE9;s (SI)</title>
<docidentifier type="BIPM">BIPM PLTS-2000</docidentifier>
<language current="true">en</language>
<script current="true">Latn</script>
</bibdata>
<preface><clause id="_abstract1" obligation="informative"><title depth="1">Abstract</title></clause></preface>
</bipm-standard>
</doc-container>
</metanorma-collection>
INPUT
expect(File.exist?("test.pdf")).to be true
expect(File.exist?("test_en.pdf")).to be true
expect(File.exist?("test_fr.pdf")).to be true
end

it "does not split monolingual collection PDF" do
FileUtils.rm_f("test.pdf")
FileUtils.rm_f("test_fr.pdf")
FileUtils.rm_f("test_en.pdf")
IsoDoc::BIPM::PdfConvert.new({}).convert('test', <<~"INPUT", false)
<metanorma-collection xmlns="http://metanorma.org">
<bibdata type="collection">
<title format="text/plain" language="en">The International Temperature Scale of 1990 (ITS-90)</title>
</bibdata>
<manifest>
<level>brochure</level>
<title>Brochure/Brochure</title>
<docref fileref="brochure-its90-fr.xml" id="doc000000000">
<identifier>brochure-its90-fr</identifier>
</docref>
<docref fileref="brochure-its90-en.xml" id="doc000000001">
<identifier>brochure-its90-en</identifier>
</docref>
</manifest>
<doc-container id="doc000000000">
<bipm-standard xmlns="https://www.metanorma.org/ns/bipm" type="presentation" version="0.0.5">
<bibdata type="standard">
<title language="en" format="text/plain" type="main">The International Temperature Scale of 1990 (ITS-90)</title>
<title language="en" format="text/plain" type="cover">The International System of Units (SI)</title>
<title language="fr" format="text/plain" type="main">&#xC9;chelle Internationale de Temp&#xE9;rature De 1990 (EIT-90)</title>
<title language="fr" format="text/plain" type="cover">Le Syst&#xE8;me international d&#x2019;unit&#xE9;s (SI)</title>
<docidentifier type="BIPM">BIPM PLTS-2000</docidentifier>
<language current="true">fr</language>
<script current="true">Latn</script>
</bibdata>
<preface><clause id="_abstract" obligation="informative"><title depth="1">Abstract</title></clause></preface>
</bipm-standard>
</doc-container>
<doc-container id="doc000000001">
<bipm-standard xmlns="https://www.metanorma.org/ns/bipm" type="presentation" version="0.0.5">
<bibdata type="standard">
<title language="en" format="text/plain" type="main">The International Temperature Scale of 1990 (ITS-90)</title>
<title language="en" format="text/plain" type="cover">The International System of Units (SI)</title>
<title language="fr" format="text/plain" type="main">&#xC9;chelle Internationale de Temp&#xE9;rature De 1990 (EIT-90)</title>
<title language="fr" format="text/plain" type="cover">Le Syst&#xE8;me international d&#x2019;unit&#xE9;s (SI)</title>
<docidentifier type="BIPM">BIPM PLTS-2000</docidentifier>
<language current="true">fr</language>
<script current="true">Latn</script>
</bibdata>
<preface><clause id="_abstract1" obligation="informative"><title depth="1">Abstract</title></clause></preface>
</bipm-standard>
</doc-container>
</metanorma-collection>
INPUT
expect(File.exist?("test.pdf")).to be true
expect(File.exist?("test_en.pdf")).to be false
expect(File.exist?("test_fr.pdf")).to be false
end
it "generates an index" do
expect(xmlpp(strip_guid(IsoDoc::BIPM::PresentationXMLConvert.new({}).convert('test', <<~INPUT, true).gsub(%r{<localized-strings>.*</localized-strings>}m, "")))).to be_equivalent_to <<~"OUTPUT"
<bipm-standard xmlns="https://open.ribose.com/standards/bipm">
<bibdata>
<language>fr</language>
<script>Latn</script>
</bibdata>
<sections>
<clause id="A">
<index primary="élongé"/>
<index primary="être" secondary="Husserl" tertiary="en allemand"/>
<index primary="Emancipation"/>
<index primary="Emancipation" secondary="dans la France"/>
<index primary="Emancipation" secondary="dans la France" tertiary="en Bretagne"/>
<clause id="B">
<index primary="Emancipation"/>
<index primary="zebra"/>
<index primary="Emancipation" secondary="dans les États-Unis"/>
<index primary="Emancipation" secondary="dans la France" tertiary="à Paris"/>
</clause>
</clause>
</sections>
</bipm-standard>
INPUT
<bipm-standard xmlns='https://open.ribose.com/standards/bipm' type='presentation'>
<bibdata>
<language current='true'>fr</language>
<script current='true'>Latn</script>
</bibdata>
<sections>
<clause id='A'>
<title>1.</title>
<bookmark id='_'/>
<bookmark id='_'/>
<bookmark id='_'/>
<bookmark id='_'/>
<bookmark id='_'/>
<clause id='B'>
<title>1.1.</title>
<bookmark id='_'/>
<bookmark id='_'/>
<bookmark id='_'/>
<bookmark id='_'/>
</clause>
</clause>
</sections>
<clause type='index' id='_'>
<title>Index</title>
<clause id='_'>
<title>E</title>
<ul>
<li>
&#xE9;long&#xE9;,
<xref target='_' pagenumber='true'>chap&#xEE;tre 1</xref>
</li>
<li>
Emancipation,
<xref target='_' pagenumber='true'>chap&#xEE;tre 1</xref>
,
<xref target='_' pagenumber='true'>section 1.1</xref>
<ul>
<li>
dans la France,
<xref target='_' pagenumber='true'>chap&#xEE;tre 1</xref>
<ul>
<li>
&#xE0; Paris,
<xref target='_' pagenumber='true'>section 1.1</xref>
</li>
<li>
en Bretagne,
<xref target='_' pagenumber='true'>chap&#xEE;tre 1</xref>
</li>
</ul>
</li>
<li>
dans les &#xC9;tats-Unis,
<xref target='_' pagenumber='true'>section 1.1</xref>
</li>
</ul>
</li>
<li>
&#xEA;tre
<ul>
<li>
Husserl
<ul>
<li>
en allemand,
<xref target='_' pagenumber='true'>chap&#xEE;tre 1</xref>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</clause>
<clause id='_'>
<title>Z</title>
<ul>
<li>
zebra,
<xref target='_' pagenumber='true'>section 1.1</xref>
</li>
</ul>
</clause>
</clause>
</bipm-standard>
OUTPUT

end

end
Loading

0 comments on commit 56cdc52

Please sign in to comment.