Skip to content

Commit 09934a4

Browse files
committed
allow footnotes in received DOC HTML not to be consecutively numbered in their ids: metanorma/isodoc#623
1 parent c6d64f8 commit 09934a4

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

lib/html2doc/notes.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22

33
class Html2Doc
44
def footnotes(docxml)
5-
i = 1
5+
#i = 1
6+
indexes = {}
7+
@footnote_idx = 1
68
fn = []
79
docxml.xpath("//a").each do |a|
8-
next unless process_footnote_link(docxml, a, i, fn)
9-
10-
i += 1
10+
process_footnote_link(docxml, a, indexes, fn) or next
11+
#i += 1
1112
end
12-
process_footnote_texts(docxml, fn)
13+
process_footnote_texts(docxml, fn, indexes)
1314
end
1415

15-
def process_footnote_texts(docxml, footnotes)
16+
# Currently cannot deal with separate footnote containers in each chapter
17+
# We may eventually need to support that
18+
def process_footnote_texts(docxml, footnotes, indexes)
1619
body = docxml.at("//body")
1720
list = body.add_child("<div style='mso-element:footnote-list'/>")
18-
footnotes.each_with_index do |f, i|
19-
fn = list.first.add_child(footnote_container(docxml, i + 1))
21+
footnotes.each do |f|
22+
#require 'debug'; binding.b
23+
fn = list.first.add_child(footnote_container(docxml, indexes[f["id"]]))
2024
f.parent = fn.first
25+
f["id"] = ""
2126
footnote_div_to_p(f)
2227
end
2328
footnote_cleanup(docxml)
@@ -47,14 +52,17 @@ def footnote_container(docxml, idx)
4752
DIV
4853
end
4954

50-
def process_footnote_link(docxml, elem, idx, footnote)
51-
return false unless footnote?(elem)
52-
55+
def process_footnote_link(docxml, elem, indexes, footnote)
56+
footnote?(elem) or return false
5357
href = elem["href"].gsub(/^#/, "")
58+
#require "debug"; binding.b
5459
note = docxml.at("//*[@name = '#{href}' or @id = '#{href}']")
55-
return false if note.nil?
56-
57-
set_footnote_link_attrs(elem, idx)
60+
note.nil? and return false
61+
unless indexes[href]
62+
indexes[href] = @footnote_idx
63+
@footnote_idx += 1
64+
end
65+
set_footnote_link_attrs(elem, indexes[href])
5866
if elem.at("./span[@class = 'MsoFootnoteReference']")
5967
process_footnote_link1(elem)
6068
else elem.children = FN
@@ -73,7 +81,7 @@ def process_footnote_link1(elem)
7381
end
7482

7583
def transform_footnote_text(note)
76-
note["id"] = ""
84+
#note["id"] = ""
7785
note.xpath(".//div").each { |div| div.replace(div.children) }
7886
note.xpath(".//aside | .//p").each do |p|
7987
p.name = "p"

spec/html2doc_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,9 @@ def image_clean(xml)
850850

851851
it "processes epub:type footnotes" do
852852
simple_body = '<div>This is a very simple
853-
document<a epub:type="footnote" href="#a1">1</a> allegedly<a epub:type="footnote" href="#a2">2</a></div>
854-
<aside id="a1">Footnote</aside>
855-
<aside id="a2">Other Footnote</aside>'
853+
document<a epub:type="footnote" href="#a_632">1</a> allegedly<a epub:type="footnote" href="#a_782">2</a></div>
854+
<aside id="a_632">Footnote</aside>
855+
<aside id="a_782">Other Footnote</aside>'
856856
Html2Doc.new(filename: "test").process(html_input(simple_body))
857857
expect(guid_clean(File.read("test.doc", encoding: "utf-8")))
858858
.to match_fuzzy(<<~OUTPUT)

0 commit comments

Comments
 (0)