diff options
author | Norbert Preining <norbert@preining.info> | 2023-02-25 03:01:09 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2023-02-25 03:01:09 +0000 |
commit | 81a9d839224eef4c2bd9bf68410b4049c61cdb14 (patch) | |
tree | 813b5a34a2548b16dbbcd271697c7bcf29342bde /support/make4ht/mkutils.lua | |
parent | c283f3a6970d2bc9de3501415f5701277da68693 (diff) |
CTAN sync 202302250301
Diffstat (limited to 'support/make4ht/mkutils.lua')
-rw-r--r-- | support/make4ht/mkutils.lua | 205 |
1 files changed, 104 insertions, 101 deletions
diff --git a/support/make4ht/mkutils.lua b/support/make4ht/mkutils.lua index 6bcc4787c3..f7636b9d2a 100644 --- a/support/make4ht/mkutils.lua +++ b/support/make4ht/mkutils.lua @@ -7,17 +7,17 @@ local mkparams = require("mkparams") local indexing = require("make4ht-indexing") --template engine function interp(s, tab) - local tab = tab or {} - return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end)) + local tab = tab or {} + return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end)) end --print( interp("${name} is ${value}", {name = "foo", value = "bar"}) ) function addProperty(s,prop) - if prop ~=nil then - return s .." "..prop - else - return s - end + if prop ~=nil then + return s .." "..prop + else + return s + end end getmetatable("").__mod = interp getmetatable("").__add = addProperty @@ -39,27 +39,27 @@ function merge(t1, t2) end function string:split(sep) - local sep, fields = sep or ":", {} - local pattern = string.format("([^%s]+)", sep) - self:gsub(pattern, function(c) fields[#fields+1] = c end) - return fields + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]+)", sep) + self:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields end function remove_extension(path) - local found, len, remainder = string.find(path, "^(.*)%.[^%.]*$") - if found then - return remainder - else - return path - end + local found, len, remainder = string.find(path, "^(.*)%.[^%.]*$") + if found then + return remainder + else + return path + end end -- -- check if file exists function file_exists(file) - local f = io.open(file, "rb") - if f then f:close() end - return f ~= nil + local f = io.open(file, "rb") + if f then f:close() end + return f ~= nil end -- check if Lua module exists @@ -90,18 +90,18 @@ function parse_lg(filename) else local usedfiles={} for line in io.lines(filename) do - --- needs --- pokus.idv[1] ==> pokus0x.png --- + --- needs --- pokus.idv[1] ==> pokus0x.png --- -- line:gsub("needs --- (.+?)[([0-9]+) ==> ([%a%d%p%.%-%_]*)",function(name,page,k) table.insert(outputimages,k)end) - line:gsub("needs %-%-%- (.+)%[([0-9]+)%] ==> ([%a%d%p%.%-%_]*)", - function(file,page,output) - local rec = { - source = file, - page=page, - output = output - } - table.insert(outputimages,rec) - end - ) + line:gsub("needs %-%-%- (.+)%[([0-9]+)%] ==> (.*) %-%-%-", + function(file,page,output) + local rec = { + source = file, + page=page, + output = output + } + table.insert(outputimages,rec) + end + ) line:gsub("File: (.*)", function(k) if not usedfiles[k] then table.insert(outputfiles,k) @@ -137,20 +137,20 @@ function cp(src,dest) -- try to find file using kpse library if it cannot be found src = kpse.find_file(src) or src end - local command = string.format('%s "%s" "%s"', cp_func, src, dest) - if cp_func == "copy" then command = command:gsub("/",'\\') end - log:info("Copy: "..command) + local command = string.format('%s "%s" "%s"', cp_func, src, dest) + if cp_func == "copy" then command = command:gsub("/",'\\') end + log:info("Copy: "..command) if not file_exists(src) then log:error("File " .. src .. " doesn't exist") end - os.execute(command) + os.execute(command) end function mv(src, dest) local mv_func = os.type == "unix" and "mv " or "move " - local command = string.format('%s "%s" "%s"', mv_func, src, dest) + local command = string.format('%s "%s" "%s"', mv_func, src, dest) -- fix windows paths - if mv_func == "move" then command = command:gsub("/",'\\') end + if mv_func == "move" then command = command:gsub("/",'\\') end log:info("Move: ".. command) os.execute(command) end @@ -163,61 +163,61 @@ end local used_dir = {} function prepare_path(path) - --local dirs = path:split("/") - local dirs = {} - if path:match("^/") then dirs = {""} - elseif path:match("^~") then - local home = os.getenv "HOME" - dirs = home:split "/" - path = path:gsub("^~/","") - table.insert(dirs,1,"") - end - if path:match("/$")then path = path .. " " end - for _,d in pairs(path:split "/") do - table.insert(dirs,d) - end - table.remove(dirs,#dirs) - return dirs,table.concat(dirs,"/") + --local dirs = path:split("/") + local dirs = {} + if path:match("^/") then dirs = {""} + elseif path:match("^~") then + local home = os.getenv "HOME" + dirs = home:split "/" + path = path:gsub("^~/","") + table.insert(dirs,1,"") + end + if path:match("/$")then path = path .. " " end + for _,d in pairs(path:split "/") do + table.insert(dirs,d) + end + table.remove(dirs,#dirs) + return dirs,table.concat(dirs,"/") end -- Find which part of path already exists -- and which directories have to be created function find_directories(dirs, pos) - local pos = pos or #dirs - -- we tried whole path and no dir exist - if pos < 1 then return dirs end - local path = "" - -- in the case of unix absolute path, empty string is inserted in dirs - if pos == 1 and dirs[pos] == "" then - path = "/" - else - path = table.concat(dirs,"/", 1,pos) .. "/" - end - if not lfs.chdir(path) then -- recursion until we succesfully changed dir - -- or there are no elements in the dir table - return find_directories(dirs,pos - 1) + local pos = pos or #dirs + -- we tried whole path and no dir exist + if pos < 1 then return dirs end + local path = "" + -- in the case of unix absolute path, empty string is inserted in dirs + if pos == 1 and dirs[pos] == "" then + path = "/" + else + path = table.concat(dirs,"/", 1,pos) .. "/" + end + if not lfs.chdir(path) then -- recursion until we succesfully changed dir + -- or there are no elements in the dir table + return find_directories(dirs,pos - 1) elseif pos ~= #dirs then -- if we succesfully changed dir - -- and we have dirs to create - local p = {} - for i = pos+1, #dirs do - table.insert(p, dirs[i]) - end - return p + -- and we have dirs to create + local p = {} + for i = pos+1, #dirs do + table.insert(p, dirs[i]) + end + return p else -- whole path exists - return {} + return {} end end function mkdirectories(dirs) - if type(dirs) ~="table" then - return false, "mkdirectories: dirs is not table" - end - for _,d in ipairs(dirs) do - local stat,msg = lfs.mkdir(d) - if not stat then return false, "makedirectories error: "..msg end - lfs.chdir(d) - end - return true + if type(dirs) ~="table" then + return false, "mkdirectories: dirs is not table" + end + for _,d in ipairs(dirs) do + local stat,msg = lfs.mkdir(d) + if not stat then return false, "makedirectories error: "..msg end + lfs.chdir(d) + end + return true end function copy_filter(src,dest, filter) @@ -233,22 +233,22 @@ end function copy(filename,outfilename) - local currdir = lfs.currentdir() - if filename == outfilename then return true end - local parts, path = prepare_path(outfilename) - if not used_dir[path] then - local to_create, msg = find_directories(parts) - if not to_create then - log:warning(msg) - return false - end - used_dir[path] = true - local stat, msg = mkdirectories(to_create) - if not stat then log:warning(msg) end - end - lfs.chdir(currdir) - cp(filename, path) - return true + local currdir = lfs.currentdir() + if filename == outfilename then return true end + local parts, path = prepare_path(outfilename) + if not used_dir[path] then + local to_create, msg = find_directories(parts) + if not to_create then + log:warning(msg) + return false + end + used_dir[path] = true + local stat, msg = mkdirectories(to_create) + if not stat then log:warning(msg) end + end + lfs.chdir(currdir) + cp(filename, path) + return true end function execute(command) @@ -373,11 +373,14 @@ env.Make:add("httex", htlatex.httex, { env.Make:add("latexmk", function(par) local settings = get_filter_settings "htlatex" or {} par.interaction = par.interaction or settings.interaction or "batchmode" - local command = Make.latex_command + local command = Make.latex_command + -- add " %O " after the engine name. it should be filled by latexmk + command = command:gsub("%s", " %%O ", 1) par.expanded = command % par -- quotes in latex_command must be escaped, they cause Latexmk error par.expanded = par.expanded:gsub('"', '\\"') - local newcommand = 'latexmk -latex="${expanded}" -dvi ${tex_file}' % par + local newcommand = 'latexmk -pdf- -ps- -auxdir=. -outdir=. -latex="${expanded}" -dvi -jobname=${input} ${tex_file}' % par + log:info("LaTeX call: " .. newcommand) os.execute(newcommand) return Make.testlogfile(par) end, {correct_exit= 0}) @@ -406,7 +409,7 @@ env.Make:add("tex4ht",function(par) log:info("executing: " .. command) return execute(command) end - , nil, 1) +, nil, 1) env.Make:add("t4ht","t4ht ${t4ht_par} \"${input}.${ext}\"",{ext="dvi"},1) env.Make:add("clean", function(par) @@ -439,7 +442,7 @@ env.Make:add("clean", function(par) end Make:match("tmp$", function() -- remove temporary and auxilary files - for _,ext in ipairs {"aux", "xref", "tmp", "4tc", "4ct", "idv", "lg","dvi", "log"} do + for _,ext in ipairs {"aux", "xref", "tmp", "4tc", "4ct", "idv", "lg","dvi", "log", "ncx"} do remove_file(main_name .. "." .. ext) end end) |