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/extensions/common_domfilters.lua | 29 +++--- support/make4ht/extensions/dvisvgm_hashes.lua | 7 +- support/make4ht/extensions/preprocess_input.lua | 108 +++++++++++++++++++++++ support/make4ht/extensions/staticsite.lua | 3 +- support/make4ht/extensions/tidy.lua | 3 +- 5 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 support/make4ht/extensions/preprocess_input.lua (limited to 'support/make4ht/extensions') diff --git a/support/make4ht/extensions/common_domfilters.lua b/support/make4ht/extensions/common_domfilters.lua index 7c9a2dfaea..9399492d90 100644 --- a/support/make4ht/extensions/common_domfilters.lua +++ b/support/make4ht/extensions/common_domfilters.lua @@ -1,25 +1,34 @@ local M = {} +-- this variable will hold the output format name +local current_format + local filter = require "make4ht-domfilter" -- local process = filter {"fixinlines", "idcolons", "joincharacters" } -- filters support only html formats function M.test(format) - if format == "odt" then return false end + current_format = format + -- if format == "odt" then return false end return true end function M.modify_build(make) - local process = filter {"fixinlines", "idcolons", "joincharacters"} - make:match("html$", process) - local matches = make.matches - -- the filters should be first match to be executed, especially if tidy - -- should be executed as well - if #matches > 1 then - local last = matches[#matches] - table.insert(matches, 1, last) - matches[#matches] = nil + -- number of filters that should be moved to the beginning + local count = 0 + if current_format == "odt" then + -- some formats doesn't make sense in the ODT format + local process = filter {"joincharacters"} + local charclasses = {mn = true, ["text:span"] = true} + make:match("4oo$", process, {charclasses= charclasses}) + -- match math documents + make:match("4om$", process, {charclasses= charclasses}) + count = 2 + else + local process = filter {"fixinlines", "idcolons", "joincharacters", "tablerows"} + make:match("html$", process) + count = 1 end return make end diff --git a/support/make4ht/extensions/dvisvgm_hashes.lua b/support/make4ht/extensions/dvisvgm_hashes.lua index 83010ac388..87f58abd57 100644 --- a/support/make4ht/extensions/dvisvgm_hashes.lua +++ b/support/make4ht/extensions/dvisvgm_hashes.lua @@ -1,6 +1,7 @@ local dvireader = require "make4ht-dvireader" local mkutils = require "mkutils" local filter = require "make4ht-filter" +local log = logging.new "dvisvgm_hashes" local dvisvgm_par = {} @@ -149,7 +150,7 @@ end local function execute_dvisvgm(idvfile, pages) if #pages < 1 then return nil, "No pages to convert" end local command, logs = prepare_command(idvfile, pages) - print(command) + log:info(command) os.execute(command) local generated_pages = {} for _, dvisvgmlog in ipairs(logs) do @@ -184,7 +185,7 @@ local function get_dvi_pages(arg) local output_name = make_hashed_name(arg.input, hash) output_map[tex4ht_name] = output_name if not mkutils.file_exists(output_name) then - print(output_name) + log:debug("output file: ".. output_name) to_convert[#to_convert+1] = page end end @@ -246,7 +247,7 @@ function M.modify_build(make) function(str) return str:gsub('src="([^"]+)', function(filename) local newname = output_map[filename] or filename - print("newname", newname) + log:debug("newname", newname) return 'src="'.. newname end) end diff --git a/support/make4ht/extensions/preprocess_input.lua b/support/make4ht/extensions/preprocess_input.lua new file mode 100644 index 0000000000..3155a8a781 --- /dev/null +++ b/support/make4ht/extensions/preprocess_input.lua @@ -0,0 +1,108 @@ +-- preprocess R literate sources or Markdown files to LaTeX +local M = {} +local log = logging.new "preprocess_input" +local mkutils = require "mkutils" + +local commands = { + knitr = { command = 'Rscript -e "library(knitr); knit(\'${tex_file}\', output=\'${tmp_file}\')"'}, + pandoc = { command = 'pandoc -f ${input_format} -s -o \'${tmp_file}\' -t latex \'${tex_file}\''} +} +local filetypes = { + rnw = {sequence = {"knitr"} }, + rtex = {sequence = {"knitr"}}, + rmd = {sequence = {"knitr", "pandoc"}, options = {input_format = "markdown"}}, + rrst = {sequence = {"knitr", "pandoc"}, options = {input_format = "rst"}}, + md = {sequence = {"pandoc"}, options = {input_format = "markdown"}}, + rst = {sequence = {"pandoc"}, options = {input_format = "rst"}}, +} + + +local function execute_sequence(sequence, arg, make) + -- keep track of all generated tmp files + local temp_files = {} + -- the temporary file for the current compilation step + -- should become the tex_file for the next one. It doesn't + -- matter that it isn't TeX file in some cases + local previous_temp + for _, cmd_name in ipairs(sequence) do + local tmp_name = os.tmpname() + temp_files[#temp_files+1] = tmp_name + -- make the temp file name accessible to the executed commands + arg.tmp_file = tmp_name + -- the current temporary file should become tex_file in the next step + -- in the first execution of the compilation sequence we will use the + -- actual input file name + arg.tex_file = previous_temp or arg.tex_file + previous_temp = tmp_name + -- get the command to execute + local cmd = commands[cmd_name] + -- fill the command template with make4ht arguments and execute + local command = cmd.command % arg + log:info(command) + mkutils.execute(command) + end + return temp_files +end + +local function get_preprocessing_pipeline(input_file) + -- detect the file extension + local extension = input_file:match("%.(.-)$") + if not extension then return nil, "Cannot get extension: " .. input_file end + -- the table with file actions is case insensitive + -- the extension is converted to lowercase in order + -- to support both .rnw and .Rnw + extension = string.lower(extension) + local matched = filetypes[extension] + if not matched then return nil, "Unsupported extension: " .. extension end + return matched +end + +-- join the make4ht params and command options tables +local function make_options(arg, command_options) + local options = {} + local command_options = command_options or {} + for k,v in pairs(arg) do options[k] = v end + for k,v in pairs(command_options) do options[k] = v end + return options +end + +M.modify_build = function(make) + + -- get access to the main argumens + local arg = make.params + -- get the execution sequence for the input format + local matched, msg = get_preprocessing_pipeline(arg.tex_file) + if not matched then + log:error("preprocess_input error: ".. msg) + return + end + -- prepare options + local options = make_options(arg, matched.options) + -- run the execution sequence + local temp_files = execute_sequence(matched.sequence or {}, options, make) + -- the last temporary file contains the actual TeX file + local last_temp_file = temp_files[#temp_files] + -- remove the intermediate temp files + if #temp_files > 2 then + for i = 1, #temp_files - 1 do + log:debug("Removing temporary file", temp_files[i]) + os.remove(temp_files[i]) + end + end + if last_temp_file then + -- update all commands in the .mk4 file with the temp file as tex_file + local update_params = function(cmd) + local params = cmd.params + params.tex_file = last_temp_file + params.is_tmp_file = true + end + for _, cmd in ipairs(make.build_seq) do + update_params(cmd) + end + -- also update the main params + update_params(make) + end + return make +end + +return M diff --git a/support/make4ht/extensions/staticsite.lua b/support/make4ht/extensions/staticsite.lua index fcb6c09e17..a4204bc173 100644 --- a/support/make4ht/extensions/staticsite.lua +++ b/support/make4ht/extensions/staticsite.lua @@ -1,6 +1,7 @@ local M = {} local filter = require "make4ht-filter" local mkutils = require "mkutils" +local log = logging.new "staticsite" -- get the published file name local function get_slug(settings) @@ -15,7 +16,7 @@ local function get_slug(settings) local f = io.open(published_name, "r") local readtime = f:read("*line") time = tonumber(readtime) - print("Already pubslished", slug) + log:info("Already pubslished", slug) f:close() else -- escape diff --git a/support/make4ht/extensions/tidy.lua b/support/make4ht/extensions/tidy.lua index cf8ea9785a..b381a05a8f 100644 --- a/support/make4ht/extensions/tidy.lua +++ b/support/make4ht/extensions/tidy.lua @@ -1,5 +1,6 @@ local M = {} +local log = logging.new "tidy" function M.test(format) if format == "odt" then return false end return true @@ -41,7 +42,7 @@ function M.modify_build(make) local settings = get_filter_settings "tidy" or {} par.options = par.options or settings.options or "-utf8 -w 512 -ashtml -q" local command = "tidy ${options} ${filename}" % par - print("execute: ".. command) + log:info("running tidy: ".. command) -- os.execute(command) local run = io.popen(command, "r") local result = run:read("*all") -- cgit v1.2.3