Skip to content

Commit d02f153

Browse files
committed
book: handle footnotes gracefully
Currently there is only one translation with footnotes: Čeština. Nevertheless, we need to drag those footnote definitions from way after all sections into the sections, filtered by the ones that are actually referenced in that section. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c15bf73 commit d02f153

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

script/update-book2.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def genbook(language_code, &get_content)
122122

123123
section_title = sec.at("h3").content
124124

125-
html = sec.inner_html.to_s + nav
125+
html = sec.inner_html.to_s
126126

127127
html.gsub!("<h3", "<h2")
128128
html.gsub!(/\/h3>/, "/h2>")
@@ -142,9 +142,15 @@ def genbook(language_code, &get_content)
142142
end
143143
end
144144

145+
footnotes = Set.new([])
146+
145147
xlink = html.scan(/href="\#(.*?)"/)
146148
xlink&.each do |link|
147149
xref = link.first
150+
if xref.start_with?('_footnotedef_') || xref.start_with?('_footnoteref_')
151+
footnotes.add(xref)
152+
next
153+
end
148154
book.xrefs[xref] = 'redirect-to-en' if !book.xrefs[xref]
149155
begin
150156
html.gsub!(/href="\##{xref}"/, "href=\"{{< relurl \"#{book_prefix}ch00/#{xref}\" >}}\"")
@@ -153,6 +159,15 @@ def genbook(language_code, &get_content)
153159
end
154160
end
155161

162+
if footnotes.empty?
163+
footnotes = ''
164+
else
165+
@children = alldoc.xpath("//div[@id='footnotes']").children.select do |e|
166+
e.name != 'div' || footnotes.include?(e.attributes['id'].to_s)
167+
end
168+
footnotes = "<div id='footnotes'>#{@children.map{|e| e.to_html}.join('').gsub(/\n\n*/, "\n")}</div>"
169+
end
170+
156171
images = []
157172
subsec = html.scan(/<img src="(.*?)"/)
158173
subsec&.each do |sub|
@@ -173,7 +188,7 @@ def genbook(language_code, &get_content)
173188
schapter.sections[section] = csection = Section.new(schapter, section)
174189
end
175190
csection.title = section_title.to_s
176-
csection.html = pretext + html
191+
csection.html = pretext + html + footnotes + nav
177192

178193
# create xref
179194
if section == 1
@@ -203,7 +218,7 @@ def genbook(language_code, &get_content)
203218
# record all the xrefs
204219
sec.search(".//*[@id]").each do |id|
205220
id_xref = id.attribute("id").to_s
206-
book.xrefs[id_xref] = csection
221+
book.xrefs[id_xref] = csection if !id_xref.start_with?('_footnoteref_')
207222
end
208223

209224
section += 1

0 commit comments

Comments
 (0)