local domfilter = require "make4ht-domfilter" local function handle_attributes(el) local attr = el._attr or {} local newattr = {} for k,v in pairs(attr) do newattr["data-" .. k] = v end el._attr = newattr end local function set_class(el, name) el:set_attribute("class", name) end local function change_element_name(el, newname) el._name = newname end local function calc_fence_size(el, current_size) local current_size = current_size or 1 for _, n in ipairs(el:get_children()) do -- process elements and try to find mfrac elements if n:is_element() then local class = n:get_attribute("class") or "" -- increase fence size for each fraction if class:match("^mfrac") then current_size = current_size + 1 end -- and process recursivelly children current_size = calc_fence_size(n, current_size) end end return current_size end local function handle_fences(dom) for _, mo in ipairs(dom:query_selector("span.mo")) do if mo:get_attribute("data-fence") then -- try to calculate size of the fence local size = calc_fence_size(mo:get_parent()) mo:set_attribute("data-fence-size", tostring(size)) end end end local function handle_children(el) if el:is_element() then local current_name = el._name change_element_name(el, "span") handle_attributes(el) set_class(el, current_name) for _, child in ipairs(el:get_children()) do handle_children(child) end elseif el:is_text() then -- trim unwanted spaces el._text = el._text:gsub("^%s*", ""):gsub("%s*$", "") end end -- this is a trick to insert this function as the last DOM filter. -- it is necessary that it is executed after "mathml-fixes" filter. Make:add("ahoj", function() local process = domfilter{ function(dom) for _, m in ipairs(dom:query_selector("math")) do local display = m:get_attribute("display") handle_attributes(m) if display == "block" then change_element_name(m, "div") set_class(m, "math block") else change_element_name(m, "span") set_class(m, "math inline") end for _, child in ipairs(m:get_children()) do handle_children(child) end end handle_fences(dom) local link = dom:create_element("link", {rel="stylesheet", type="text/css", href = "mathml.css"}) local head = dom:query_selector("head")[1] head:add_child_node(link) return dom end } Make:match("html$", process) end) Make:htlatex {} Make:ahoj {}