22
33class 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"
0 commit comments