Closed togrul-topal closed 1 year ago
Try this version of the build file:
local domfilter = require "make4ht-domfilter"
local domobject = require "luaxml-domobject"
local function fix_ids(dom, prefix)
-- we need to prevent duplicate ID attributes in included SVG, so we add a prefix to them
dom:traverse_elements(function(element)
local id = element:get_attribute("id")
if id then
element:set_attribute("id", prefix .. id)
end
local xlink = element:get_attribute("xlink:href")
if xlink and xlink:sub(1,1) == "#" then
element:set_attribute("xlink:href", "#" .. prefix .. xlink:sub(2))
end
local clip = element:get_attribute("clip-path")
if clip then
element:set_attribute("clip-path", clip:gsub("%#", "#" .. prefix))
end
end)
end
local process = domfilter {
function(dom)
for count, img in ipairs(dom:query_selector "img") do
local file = img:get_attribute "src" or ""
-- because we sometimes use imgdir option, script should not search in imgdir directory
-- the next line therefore transformes imgdir/tikz.svg to tikz.svg
local filename = file:match("^.+/(.+)$")
if not filename then filename = file end
-- process all SVG images
if filename:match("svg$") then
-- open the SVG file and load it into a string
local f = io.open(filename, "r")
local content = f:read("*all")
f:close()
-- parse it to a new DOM object
local newdom = domobject.parse(content)
fix_ids(newdom, "x" .. count .. "-")
-- now find the <svg> element
local root = newdom:root_node()
-- <svg> should be a child of the root node
for _, child in ipairs(root:get_children()) do
-- replace <img> with <svg>
if child:is_element() and child:get_element_name() == "svg" then
img:replace_node(child)
end
end
end
end
return dom
end
}
-- call the LuaXML dom processing on all HTML files
Make:match("html$", process)
Spot on, IDs were the problem!
Not the hero we deserve but the hero we need.
This thread refers to this StackExchange thread that I have found earlier. If works well with a single tikz. However, when I have several TiKZ pictures in my file, some problems appear.
I have the following build.lua file:
I run everything with