diff options
Diffstat (limited to 'support/tex4ebook/tex4ebook-exec_epub3.lua')
-rw-r--r-- | support/tex4ebook/tex4ebook-exec_epub3.lua | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/support/tex4ebook/tex4ebook-exec_epub3.lua b/support/tex4ebook/tex4ebook-exec_epub3.lua index fc200bd866..328acaae37 100644 --- a/support/tex4ebook/tex4ebook-exec_epub3.lua +++ b/support/tex4ebook/tex4ebook-exec_epub3.lua @@ -113,6 +113,12 @@ local function add_media_overlays(content) end +-- elements that shouldn't be put inside <a> in TOC +local stop_toc_processing_elements = { + ol = true, + ul = true +} + local function remove_spurious_TOC_elements(tocdom) local function count_child_elements(el) -- count children elements of the current element @@ -127,36 +133,44 @@ local function remove_spurious_TOC_elements(tocdom) for _, el in ipairs(tocdom:query_selector("ol")) do if count_child_elements(el) == 0 then el:remove_node() - -- local newli = el:create_element("li") - -- local newspan = newli:create_element("span") - -- newli:add_child_node(newspan) - -- el:add_child_node(newli) end end -- place child elements of the <li> elements to an <a> element, epubcheck reports -- error for text nodes that are direct child of <li> for _, el in ipairs(tocdom:query_selector("li")) do - local text = {} - local link -- save the <a> element + + local newa = el:create_element("a") + local newchildren = {newa} + -- we want to stop putting content as child of <a> when it + -- finds child TOC entries + local keep_processing = true for i, child in ipairs(el._children) do - if child:is_text() then - -- trim spurious spaces - local childtext = child._text:gsub("^%s*",""):gsub("%s*$","") - -- save the text for the later use - table.insert(text, childtext) - child:remove_node() - elseif child:is_element() and child:get_element_name() == "a" then - link = child - table.insert(text, child:get_text()) + child_name = child:get_element_name() + -- put contents of <li> to a new <a> element + if child:is_element() and child_name == "a" then + -- set id and href of the new <a> element, if it isn't set already + if not newa:get_attribute("href") then + local id = child:get_attribute("id") + local href = child:get_attribute("href") + newa:set_attribute("id", id) + newa:set_attribute("href", href) + end + -- copy <a> contents to the new <a> element + for _, x in ipairs(child._children or {}) do newa:add_child_node(x:copy_node()) end + + elseif stop_toc_processing_elements[child_name] then + -- don't put child toc entries to the new <a> + keep_processing = false + newchildren[#newchildren+1] = child + elseif keep_processing == true then + -- put every node before <ol> or <ul> into the new <a> + newa:add_child_node(child:copy_node()) + else + newchildren[#newchildren+1] = child end end - if link then - local content = table.concat(text, " ") - -- remove the original text - link._children = {} - local text = link:create_text_node(content) - link:add_child_node(text) - end + -- set contents of <li> to be the new <a> + el._children = newchildren end return tocdom |