Skip to content

Commit

Permalink
fix ordering of HTML ToC for H1 and H2 on same line: metanorma/metano…
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Nov 12, 2023
1 parent 6933d17 commit a2897d6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/isodoc/html_function/postprocess_cover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ def html_toc_entries(docxml, path)
docxml.xpath(p.join(" | ")).each do |h|
h["id"] ||= "_#{UUIDTools::UUID.random_create}"
m << { entry: html_toc_entry("h#{i + 1}", h),
line: h.line }
line: h.line, level: i + 1 }
end
end.sort_by { |k| k[:line] }
end.sort_by { |k| [k[:line], k[:level]] }
end

def toc_exclude_class
Expand Down
2 changes: 1 addition & 1 deletion spec/assets/htmlintro.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/* an empty html intro page */

<ul id="toc-list"></ul>

<div id="toc"/>
57 changes: 40 additions & 17 deletions spec/isodoc/postproc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
olstyle: "l2" },
).convert("test", <<~INPUT, false)
<iso-standard xmlns="http://riboseinc.com/isoxml">
<preface><foreword displayorder="1">
<preface><foreword displayorder="1" id="fwd">
<note>
<p id="_f06fd0d1-a203-4f3d-a515-0bdba0f8d83f">These results are based on a study carried out on three different types of kernel.</p>
</note>
Expand All @@ -375,7 +375,8 @@
INPUT
html = Nokogiri::XML(File.read("test.html")).at("//body")
html.xpath("//script").each(&:remove)
expect(html.to_xml).to be_equivalent_to <<~OUTPUT
expect(strip_guid(xmlpp(html.to_xml)))
.to be_equivalent_to <<~OUTPUT
<body lang="en" xml:lang="en">
<div class="title-section">
/* an empty html cover page */
Expand All @@ -384,25 +385,27 @@
<div class="prefatory-section">
/* an empty html intro page */
<ul id="toc-list"/>
</div>
<br/>
<main class="main-section"><button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<br/>
<div>
<h1 class="ForewordTitle">Anta&#x16D;parolo</h1>
<div class="Note">
<p>&#xA0; These results are based on a study carried out on three different types of kernel.</p>
</div>
</div>
</main>
</body>
<div id="toc"><ul><li class="h1"><a href="#_"> Antaŭparolo</a></li></ul></div>
</div>
<br/>
<main class="main-section">
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<br/>
<div id="fwd">
<h1 class="ForewordTitle" id="_">Antaŭparolo</h1>
<div class="Note">
<p>  These results are based on a study carried out on three different types of kernel.</p>
</div>
</div>
</main>
</body>
OUTPUT
end

it "populates HTML ToC" do
FileUtils.rm_f "test.doc"
FileUtils.rm_f "test.html"
IsoDoc::HtmlConvert.new({})
IsoDoc::HtmlConvert.new({htmlintropage: "spec/assets/htmlintro.html"})
.convert("test", <<~INPUT, false)
<iso-standard xmlns="http://riboseinc.com/isoxml">
<preface><foreword displayorder="1"><title>Foreword</title>
Expand All @@ -414,13 +417,33 @@
<variant-title type="toc">SUBCLOZ</variant-title>
</clause>
</clause>
<clause displayorder="3"><title>Second Clause</title><clause><title>Subclause</title></clause></clause>
</sections>
</iso-standard>
INPUT
html = Nokogiri::XML(File.read("test.html"))
.at("//div[@id = 'toc']")
expect(xmlpp(html)).to be_equivalent_to xmlpp(<<~OUTPUT)
HAJSHJAS
expect(strip_guid(xmlpp(html.to_xml)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
<div id="toc">
<ul>
<li class="h1">
<a href="#_"> FORVORT</a>
</li>
<li class="h1">
<a href="#_"> First Clause</a>
</li>
<li class="h2">
<a href="#_"> SUBCLOZ</a>
</li>
<li class="h1">
<a href="#_"> Second Clause</a>
</li>
<li class="h2">
<a href="#_"> Subclause</a>
</li>
</ul>
</div>
OUTPUT
end

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def metadata(hash)
def strip_guid(xml)
xml.gsub(%r{ id="_[^"]+"}, ' id="_"')
.gsub(%r{ target="_[^"]+"}, ' target="_"')
.gsub(%r( href="#[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{13}"), ' href="#_"')
.gsub(%r( id="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{13}"), ' id="_"')
.gsub(%r( href="#_?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12,13}"), ' href="#_"')
.gsub(%r( id="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12,13}"), ' id="_"')
.gsub(%r( id="ftn[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{13}"), ' id="ftn_"')
.gsub(%r( id="fn:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{13}"), ' id="fn:_"')
.gsub(%r( reference="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"), ' reference="_"')
Expand Down

0 comments on commit a2897d6

Please sign in to comment.