From 3479a92321ed7fe7e2133d3daec2d4f5fd53fbc6 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Sat, 2 Nov 2019 03:01:48 +0000 Subject: CTAN sync 201911020301 --- support/make4ht/mkutils.lua | 131 ++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 66 deletions(-) (limited to 'support/make4ht/mkutils.lua') diff --git a/support/make4ht/mkutils.lua b/support/make4ht/mkutils.lua index fd32c91e08..ba1cd6cbb9 100644 --- a/support/make4ht/mkutils.lua +++ b/support/make4ht/mkutils.lua @@ -1,7 +1,10 @@ module(...,package.seeall) +local log = logging.new("mkutils") + local make4ht = require("make4ht-lib") local mkparams = require("mkparams") +local indexing = require("make4ht-indexing") --template engine function interp(s, tab) local tab = tab or {} @@ -61,11 +64,11 @@ end -- searching for converted images function parse_lg(filename) - print("Parse LG") + log:info("Parse LG") local outputimages,outputfiles,status={},{},nil local fonts, used_fonts = {},{} if not file_exists(filename) then - print("Cannot read log file: "..filename) + log:warning("Cannot read log file: "..filename) else local usedfiles={} for line in io.lines(filename) do @@ -114,7 +117,7 @@ local cp_func = os.type == "unix" and "cp" or "copy" function cp(src,dest) local command = string.format('%s "%s" "%s"', cp_func, src, dest) if cp_func == "copy" then command = command:gsub("/",'\\') end - print("Copy: "..command) + log:info("Copy: "..command) os.execute(command) end @@ -123,7 +126,7 @@ function mv(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 - print("Move: ".. command) + log:info("Move: ".. command) os.execute(command) end @@ -211,18 +214,32 @@ function copy(filename,outfilename) if not used_dir[path] then local to_create, msg = find_directories(parts) if not to_create then - print(msg) + log:warning(msg) return false end used_dir[path] = true local stat, msg = mkdirectories(to_create) - if not stat then print(msg) end + if not stat then log:warning(msg) end end lfs.chdir(currdir) cp(filename, path) return true end +function execute(command) + local f = io.popen(command, "r") + local output = f:read("*all") + -- rc will contain return codes of the executed command + local rc = {f:close()} + -- the status code is on the third position + -- https://stackoverflow.com/a/14031974/2467963 + local status = rc[3] + -- print the command line output only when requested through + -- log level + log:output(output) + return status, output +end + -- find the zip command function find_zip() if io.popen("zip -v","r"):close() then @@ -269,6 +286,7 @@ env.os = os env.io = io env.math = math env.unicode = unicode +env.logging = logging -- it is necessary to use the settings table @@ -317,57 +335,14 @@ env.Make = make4ht.Make env.Make.params = env.settings env.Make:add("test","test the variables: ${tex4ht_sty_par} ${htlatex} ${input} ${config}") --- this function reads the LaTeX log file and tries to detect fatal errors in the compilation -local function testlogfile(par) - local logfile = par.input .. ".log" - local f = io.open(logfile,"r") - if not f then - print("Make4ht: cannot open log file "..logfile) - return 1 - end - local len = f:seek("end") - -- test only the end of the log file, no need to run search functions on everything - local newlen = len - 1256 - -- but the value to seek must be greater than 0 - newlen = (newlen > 0) and newlen or 0 - f:seek("set", newlen) - local text = f:read("*a") - f:close() - if text:match("No pages of output") or text:match("TeX capacity exceeded, sorry") then return 1 end - return 0 -end - - --- Make this function available in the build files -Make.testlogfile = testlogfile ---env.Make:add("htlatex", "${htlatex} ${latex_par} '\\\makeatletter\\def\\HCode{\\futurelet\\HCode\\HChar}\\def\\HChar{\\ifx\"\\HCode\\def\\HCode\"##1\"{\\Link##1}\\expandafter\\HCode\\else\\expandafter\\Link\\fi}\\def\\Link#1.a.b.c.{\\g@addto@macro\\@documentclasshook{\\RequirePackage[#1,html]{tex4ht}\\let\\HCode\\documentstyle\\def\\documentstyle{\\let\\documentstyle\\HCode\\expandafter\\def\\csname tex4ht\\endcsname{#1,html}\\def\\HCode####1{\\documentstyle[tex4ht,}\\@ifnextchar[{\\HCode}{\\documentstyle[tex4ht]}}}\\makeatother\\HCode '${config}${tex4ht_sty_par}'.a.b.c.\\input ' ${input}") - --- template for calling LaTeX with tex4ht loaded -Make.latex_command = "${htlatex} ${latex_par} '\\makeatletter".. -"\\def\\HCode{\\futurelet\\HCode\\HChar}\\def\\HChar{\\ifx\"\\HCode".. -"\\def\\HCode\"##1\"{\\Link##1}\\expandafter\\HCode\\else".. -"\\expandafter\\Link\\fi}\\def\\Link#1.a.b.c.{\\g@addto@macro".. -"\\@documentclasshook{\\RequirePackage[#1,html]{tex4ht}${packages}}".. -"\\let\\HCode\\documentstyle\\def\\documentstyle{\\let\\documentstyle".. -"\\HCode\\expandafter\\def\\csname tex4ht\\endcsname{#1,html}\\def".. -"\\HCode####1{\\documentstyle[tex4ht,}\\@ifnextchar[{\\HCode}{".. -"\\documentstyle[tex4ht]}}}\\makeatother\\HCode ${tex4ht_sty_par}.a.b.c.".. -"\\input \"\\detokenize{${tex_file}}\"'" - -env.Make:add("htlatex",function(par) - local command = Make.latex_command - if os.type == "windows" then - command = command:gsub("'",'') - end - command = command % par - print("LaTeX call: "..command) - os.execute(command) - return testlogfile(par) -end +local htlatex = require "make4ht-htlatex" +env.Make:add("htlatex", htlatex.htlatex ,{correct_exit=0}) 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 par.expanded = command % par -- quotes in latex_command must be escaped, they cause Latexmk error @@ -407,13 +382,13 @@ function load_config(settings, config_name) end local f = io.open(config_name,"r") if not f then - print("Cannot open config file", config_name) + log:info("Cannot open config file", config_name) return env end - print("Using build file", config_name) + log:info("Using build file", config_name) local code = f:read("*all") local fn, msg = run(code,env) - if not fn then print(msg) end + if not fn then log:warning(msg) end assert(fn) -- reload extensions from command line arguments for the "format" parameter for _,v in ipairs(saved_extensions) do @@ -423,25 +398,49 @@ function load_config(settings, config_name) end env.Make:add("xindy", function(par) - -- par.encoding = par.encoding or "utf8" - -- par.language = par.language or "english" - par.idxfile = par.idxfile or par.input .. ".idx" - local modules = par.modules or {par.input} + local xindylog = logging.new "xindy" + local settings = get_filter_settings "xindy" or {} + par.encoding = settings.encoding or par.encoding or "utf8" + par.language = settings.language or par.language or "english" + local modules = settings.modules or par.modules or {} local t = {} for k,v in ipairs(modules) do + xindylog:debug("Loading module: " ..v) t[#t+1] = "-M ".. v end par.moduleopt = table.concat(t, " ") - local xindy_call = "xindy -L ${language} -C ${encoding} ${moduleopt} ${idxfile}" % par - print(xindy_call) - return os.execute(xindy_call) -end, { language = "english", encoding = "utf8"}) + return indexing.run_indexing_command("texindy -L ${language} -C ${encoding} ${moduleopt} -o ${indfile} ${newidxfile}", par) +end, {}) + +env.Make:add("makeindex", function(par) + local makeindxcall = "makeindex ${options} -t ${ilgfile} -o ${indfile} ${newidxfile}" + local settings = get_filter_settings "makeindex" or {} + par.options = settings.options or par.options or "" + par.ilgfile = par.input .. ".ilg" + local status = indexing.run_indexing_command(makeindxcall, par) + return status +end, {}) + +env.Make:add("xindex", function(par) + local xindex_call = "xindex -l ${language} ${options} -o ${indfile} ${newidxfile}" + local settings = get_filter_settings "xindex" or {} + par.options = settings.options or par.options or "" + par.language = settings.language or par.language or "en" + local status = indexing.run_indexing_command(xindex_call, par) + return status +end, {}) + + local function find_lua_file(name) local extension_path = name:gsub("%.", "/") .. ".lua" return kpse.find_file(extension_path, "lua") end +-- for the BibLaTeX support +env.Make:add("biber", "biber ${input}") +env.Make:add("bibtex", "bibtex ${input}") + --- load the output format plugins function load_output_format(format_name) local format_library = "make4ht.formats."..format_name @@ -532,10 +531,10 @@ function load_extensions(extensions, format) if module_names[name] == true then local extension = load_extension(name,format) if extension then - print("Load extension", name) + log:info("Load extension", name) table.insert(extension_table, extension) else - print("Cannot load extension: ".. name) + log:warning("Cannot load extension: ".. name) end end end -- cgit v1.2.3