From 08e9c27a3203f86fd3564a8b8e8091cf8313353c Mon Sep 17 00:00:00 2001 From: poire-z Date: Tue, 1 Aug 2023 10:09:23 +0200 Subject: [PATCH] Wikipedia EPUBs: re-add ToC in content The ToC is no longer in the HTML we get from the Wikipedia API. So, add it ourselves. Also, as we can't get anywhere the Wikipedia localized string for "Contents" (ie. "Sommaire" in French), use thick
to mark the start and end of this ToC. --- frontend/ui/wikipedia.lua | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/frontend/ui/wikipedia.lua b/frontend/ui/wikipedia.lua index ec3a19abb..b33e27d4f 100644 --- a/frontend/ui/wikipedia.lua +++ b/frontend/ui/wikipedia.lua @@ -945,6 +945,24 @@ hr.koreaderwikifrontpage { margin-right: 20%; margin-bottom: 1.2em; } +/* Have these HR get the same margins and position as our H2 */ +hr.koreaderwikitocstart { + page-break-before: always; + font-size: 140%; + margin: 0.7em 30% 1.5em; + height: 0.22em; + border: none; + background-color: black; +} +hr.koreaderwikitocend { + page-break-before: avoid; + page-break-after: always; + font-size: 140%; + margin: 1.2em 30% 0; + height: 0.22em; + border: none; + background-color: black; +} /* So many links, make them look like normal text except for underline */ a { @@ -1246,6 +1264,58 @@ table { ]]) epub:add("OEBPS/toc.ncx", table.concat(toc_ncx_parts)) + -- ---------------------------------------------------------------- + -- HTML table of content + -- We used to have it in the HTML we got from Wikipedia, but we no longer do. + -- So, build it from the 'sections' we got from the API. + local toc_html_parts = {} + -- Unfortunately, we don't and can't get any localized "Contents" or "Sommaire" to use + -- as a heading. So, use some
at start and at end to make this HTML ToC stand out. + table.insert(toc_html_parts, '
\n') + cur_level = 0 + for isec, s in ipairs(sections) do + -- Some chars in headings are converted to html entities in the + -- wikipedia-generated HTML. We need to do the same in TOC links + -- for the links to be valid. + local s_anchor = s.anchor:gsub("&", "&"):gsub('"', """):gsub(">", ">"):gsub("<", "<") + local s_title = string.format("%s %s", s.number, s.line) + local s_level = s.toclevel + if s_level == cur_level then + table.insert(toc_html_parts, "") + elseif s_level < cur_level then + table.insert(toc_html_parts, "") + while cur_level > s_level do + cur_level = cur_level - 1 + table.insert(toc_html_parts, "\n"..(" "):rep(cur_level)) + table.insert(toc_html_parts, "") + table.insert(toc_html_parts, "\n"..(" "):rep(cur_level)) + table.insert(toc_html_parts, "") + end + else -- s_level > cur_level + while cur_level < s_level do + table.insert(toc_html_parts, "\n"..(" "):rep(cur_level)) + table.insert(toc_html_parts, "