diff options
Diffstat (limited to 'support/make4ht/extensions')
4 files changed, 83 insertions, 8 deletions
diff --git a/support/make4ht/extensions/make4ht-ext-copy_images.lua b/support/make4ht/extensions/make4ht-ext-copy_images.lua new file mode 100644 index 0000000000..7b927c7d9f --- /dev/null +++ b/support/make4ht/extensions/make4ht-ext-copy_images.lua @@ -0,0 +1,68 @@ +local M = {} +local mkutils = require "mkutils" +local domfilter = require "make4ht-domfilter" + +local copied_images = {} + +local function image_copy(path, parameters, img_dir) + if mkutils.is_url(path) then return nil, "External image" end + -- get image basename + local basename = path:match("([^/]+)$") + -- if outdir is empty, keep it empty, otherwise add / separator + local outdir = parameters.outdir == "" and "" or parameters.outdir .. "/" + if img_dir ~= "" then + outdir = outdir .. img_dir .. "/" + end + -- handle trailing // + outdir = outdir:gsub("%/+","/") + local output_file = outdir .. basename + if outdir == "" then + mkutils.cp(path, output_file) + else + mkutils.copy(path, output_file) + end +end + +-- filters support only html formats +function M.test(format) + current_format = format + if format == "odt" then return false end + return true +end + +function M.modify_build(make) + local ext_settings = get_filter_settings "copy_images" or {} + local img_dir = ext_settings.img_dir or "" + local img_extensions = ext_settings.extensions or {"jpg", "png", "jpeg", "svg"} + local process = domfilter({ + function(dom, par) + for _, img in ipairs(dom:query_selector("img")) do + local src = img:get_attribute("src") + if src and not mkutils.is_url(src) then + -- remove path specification + src = src:match("([^/]+)$") + if img_dir ~= "" then + src = img_dir .. "/" .. src + src = src:gsub("%/+", "/") + end + img:set_attribute("src", src) + end + end + return dom + end + }, "copy_images") + + -- add matcher for all image extensions + for _, ext in ipairs(img_extensions) do + make:match(ext .. "$", function(path, parameters) + image_copy(path, parameters, img_dir) + -- prevent further processing of the image + return false + end) + end + + make:match("html$", process, {img_dir = img_dir}) + return make +end + +return M diff --git a/support/make4ht/extensions/make4ht-ext-dvisvgm_hashes.lua b/support/make4ht/extensions/make4ht-ext-dvisvgm_hashes.lua index 53da32c3be..94183c7dbf 100644 --- a/support/make4ht/extensions/make4ht-ext-dvisvgm_hashes.lua +++ b/support/make4ht/extensions/make4ht-ext-dvisvgm_hashes.lua @@ -177,7 +177,7 @@ local function get_dvi_pages(arg) f:close() local dvi_pages = dvireader.get_pages(content) -- we must find page numbers and output name sfor the generated images - local lg = mkutils.parse_lg(arg.input ..".lg") + local lg = mkutils.parse_lg(arg.input ..".lg", arg.builddir) for _, name in ipairs(lg.images) do local page = tonumber(name.page) local hash = dvi_pages[page] diff --git a/support/make4ht/extensions/make4ht-ext-latexmk_build.lua b/support/make4ht/extensions/make4ht-ext-latexmk_build.lua index b9ed94b0dc..b93f458d62 100644 --- a/support/make4ht/extensions/make4ht-ext-latexmk_build.lua +++ b/support/make4ht/extensions/make4ht-ext-latexmk_build.lua @@ -16,7 +16,7 @@ function M.modify_build(make) -- tex4ht command overwrites content that was set by LaTeX with it's own stuff local tmp_file make:add("save_tmp", function(par) - local f = io.open(par.input .. ".tmp", "r") + local f = io.open(mkutils.file_in_builddir(par.input .. ".tmp", par), "r") if f then tmp_file = f:read("*all") f:close() @@ -25,7 +25,7 @@ function M.modify_build(make) end) make:add("load_tmp", function(par) if tmp_file then - local f = io.open(par.input .. ".tmp", "w") + local f = io.open(mkutils.file_in_builddir(par.input .. ".tmp", par), "w") if f then f:write(tmp_file) end diff --git a/support/make4ht/extensions/make4ht-ext-staticsite.lua b/support/make4ht/extensions/make4ht-ext-staticsite.lua index b9272203a5..dd293b6a23 100644 --- a/support/make4ht/extensions/make4ht-ext-staticsite.lua +++ b/support/make4ht/extensions/make4ht-ext-staticsite.lua @@ -67,7 +67,7 @@ local function insert_filter(make, pattern, fn) local insert_executed = false table.insert(make.matches, 1, { pattern=pattern, - params = {}, + params = make.params or {}, command = function() if not insert_executed then fn() @@ -96,12 +96,17 @@ end local function copy_files(filename, par) local function prepare_path(dir, subdir) - local path = dir .. "/" .. subdir .. "/" .. filename + local f = filename + if par.builddir then + f = f:gsub("^" .. par.builddir .. "/", "") + end + local path = dir .. "/" .. subdir .. "/" .. f return path:gsub("//", "/") end -- get extension settings local site_settings = get_filter_settings "staticsite" - local site_root = site_settings.site_root or "./" + local site_root = site_settings.site_root or par.outdir + if site_root == "" then site_root = "./" end local map = site_settings.map or {} -- default path without subdir, will be used if the file is not matched -- by any pattern in the map @@ -150,8 +155,10 @@ function M.modify_build(make) -- match.params.outdir = outdir -- print(match.pattern, match.params.outdir) -- end - make:match("html?$", process) - make:match(".*", copy_files, {slug=slug}) + local params = make.params + params.slug = slug + make:match("html?$", process, params) + make:match(".*", copy_files, params) end) return make |