diff options
author | Norbert Preining <norbert@preining.info> | 2019-11-02 03:01:48 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-11-02 03:01:48 +0000 |
commit | 3479a92321ed7fe7e2133d3daec2d4f5fd53fbc6 (patch) | |
tree | f81c77e1742124279f68e41484d9e53ad40248ff /support/make4ht/domfilters/make4ht-tablerows.lua | |
parent | 61b8d58232b999b6cc966aee9152fb86bbb759cd (diff) |
CTAN sync 201911020301
Diffstat (limited to 'support/make4ht/domfilters/make4ht-tablerows.lua')
-rw-r--r-- | support/make4ht/domfilters/make4ht-tablerows.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/support/make4ht/domfilters/make4ht-tablerows.lua b/support/make4ht/domfilters/make4ht-tablerows.lua new file mode 100644 index 0000000000..155c24529a --- /dev/null +++ b/support/make4ht/domfilters/make4ht-tablerows.lua @@ -0,0 +1,58 @@ +local log = logging.new ("tablerows") +return function(dom) + local is_empty_row = function(row) + local not_empty = false + local element_count = 0 + -- ignore hline rows + local row_class = row:get_attribute("class") + if row_class == "hline" or row_class == "cline" then return false end + -- detect if the row contain only one empty child + for _,child in ipairs(row:get_children() or {}) do + if child:is_element() then + element_count = element_count + 1 + -- empty rows contain only one element, it is not empty otherwise + if element_count > 1 then return false end + -- detect if it contains only whitespace + not_empty = child:get_text():gsub("%s","") ~= "" or not_empty + end + end + -- print("element count", element_count, not_empty) + return element_count == 1 and not_empty == false + end + local is_not_styled = function(row, css) + -- get the id attribute and escape it, so it can be used in regexp + local id = row:get_attribute("id") + if not id then return true end -- no styling without id + local search_term = "%#" .. id:gsub("%-", "%%-") + -- if the CSS file contains the row id (<td> elements can also have id + -- that matches this pattern, so we should keep the row if we match them too) + return not css:match(search_term) + end + local load_css_files = function() + -- the empty rows can be styled using CSS, for example configuration for + -- Booktabs does that. We shouldn't remove such rows. + local cssfiles = {} + for _, link in ipairs(dom:query_selector("head link")) do + local src = link:get_attribute("href") + if src then + local f = io.open(src, "r") + if f then + local contents = f:read("*all") + f:close() + table.insert(cssfiles, contents) + end + end + end + return table.concat(cssfiles, "\n") + end + local css = load_css_files() + for _, tbl in ipairs(dom:query_selector("table")) do + -- find the empty rows + for _, row in ipairs(tbl:query_selector("tr")) do + if is_empty_row(row) and is_not_styled(row, css) then row:remove_node() end + end + + end + return dom +end + |