diff options
Diffstat (limited to 'support/make4ht/domfilters/make4ht-idcolons.lua')
-rw-r--r-- | support/make4ht/domfilters/make4ht-idcolons.lua | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/support/make4ht/domfilters/make4ht-idcolons.lua b/support/make4ht/domfilters/make4ht-idcolons.lua index ccdafbd6d4..589c515ef9 100644 --- a/support/make4ht/domfilters/make4ht-idcolons.lua +++ b/support/make4ht/domfilters/make4ht-idcolons.lua @@ -1,18 +1,36 @@ +local allowed_chars = { + ["-"] = true, + ["."] = true +} +local function fix_colons(id) + -- match every non alphanum character + return id:gsub("[%W]", function(s) + -- some characters are allowed, we don't need to replace them + if allowed_chars[s] then return s end + -- in other cases, replace with underscore + return "_" + end) +end + local function id_colons(obj) - -- replace : characters in links and ids with unserscores + -- replace non-valid characters in links and ids with underscores obj:traverse_elements(function(el) local name = string.lower(obj:get_element_name(el)) if name == "a" then - local href = obj:get_attribute(el, "href") + local href = el:get_attribute("href") + -- don't replace colons in external links if href and not href:match("[a-z]%://") then - obj:set_attribute(el, "href", href:gsub(":", "_")) + local base, id = href:match("(.*)%#(.*)") + if base and id then + id = fix_colons(id) + el:set_attribute("href", base .. "#" .. id) + end end end - local id = obj:get_attribute( el , "id") + local id = el:get_attribute("id") if id then - obj:set_attribute(el, "id", id:gsub(":", "_")) + el:set_attribute("id", fix_colons(id)) end - -- local id = obj:get_attribute(el, "id") end) return obj end |