summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/make4ht/formats/odt.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/scripts/make4ht/formats/odt.lua')
-rw-r--r--Master/texmf-dist/scripts/make4ht/formats/odt.lua93
1 files changed, 25 insertions, 68 deletions
diff --git a/Master/texmf-dist/scripts/make4ht/formats/odt.lua b/Master/texmf-dist/scripts/make4ht/formats/odt.lua
index d5a6b0c9ba5..453c46aff5e 100644
--- a/Master/texmf-dist/scripts/make4ht/formats/odt.lua
+++ b/Master/texmf-dist/scripts/make4ht/formats/odt.lua
@@ -4,6 +4,8 @@ local lfs = require "lfs"
local os = require "os"
local kpse = require "kpse"
local filter = require "make4ht-filter"
+local domfilter = require "make4ht-domfilter"
+local xtpipeslib = require "make4ht-xtpipes"
function M.prepare_parameters(settings, extensions)
@@ -75,82 +77,27 @@ function Odtfile:pack()
mkutils.delete_dir(self.archivelocation)
end
--- find if tex4ht.jar exists in a path
-local function find_tex4ht_jar(path)
- local jar_file = path .. "/tex4ht/bin/tex4ht.jar"
- return mkutils.file_exists(jar_file)
+-- escape string to be used in the gsub search
+local function escape_file(filename)
+ local quotepattern = '(['..("%^$().[]*+-?"):gsub("(.)", "%%%1")..'])'
+ return filename:gsub(quotepattern, "%%%1")
end
--- return value of TEXMFROOT variable if it exists and if tex4ht.jar can be located inside
-local function get_texmfroot()
- -- user can set TEXMFROOT environmental variable as the last resort
- local root_directories = {kpse.var_value("TEXMFROOT"), kpse.var_value("TEXMFDIST"), os.getenv("TEXMFROOT")}
- for _, root in ipairs(root_directories) do
- if root then
- if find_tex4ht_jar(root) then return root end
- -- TeX live locates files in texmf-dist subdirectory, but Miktex doesn't
- local path = root .. "/texmf-dist"
- if find_tex4ht_jar(path) then return path end
- end
- end
-end
-
--- Miktex doesn't seem to set TeX variables such as TEXMFROOT
--- we will try to find the TeX root using trick with locating package in TeX root
--- there is a danger that this file is located in TEXMFHOME, the location will fail then
-local function find_texmfroot()
- local tex4ht_path = kpse.find_file("tex4ht.sty")
- if tex4ht_path then
- local path = tex4ht_path:gsub("/tex/generic/tex4ht/tex4ht.sty$","")
- if find_tex4ht_jar(path) then return path end
- end
- return nil
-end
+local move_matches = xtpipeslib.move_matches
-- call xtpipes from Lua
local function call_xtpipes(make)
-- we must find root of the TeX distribution
- local selfautoparent = get_texmfroot() or find_texmfroot()
+ local selfautoparent = xtpipeslib.get_selfautoparent()
+
if selfautoparent then
- -- make pattern using TeX distro path
- local pattern = string.format("java -classpath %s/tex4ht/bin/tex4ht.jar xtpipes -i %s/tex4ht/xtpipes/ -o ${outputfile} ${filename}", selfautoparent, selfautoparent)
- -- call xtpipes on a temporary file
- local matchfunction = function(filename)
- -- move the matched file to a temporary file, xtpipes will write it back to the original file
- local basename = mkutils.remove_extension(filename)
- local tmpfile = basename ..".tmp"
- mkutils.mv(filename, tmpfile)
- local command = pattern % {filename = tmpfile, outputfile = filename}
- print(command)
- local status = os.execute(command)
- if status > 0 then
- -- if xtpipes failed to process the file, it may mean that it was bad-formed xml
- -- we can try to make it well-formed using Tidy
- local tidy_command = "tidy -utf8 -xml -asxml -q -o ${filename} ${tmpfile}" % {tmpfile = tmpfile, filename = filename}
- print("xtpipes failed trying tidy")
- print(tidy_command)
- local status = os.execute(tidy_command)
- if status > 0 then
- -- if tidy failed as well, just use the original file
- -- it will probably produce corrupted ODT file though
- print("Tidy failed as well")
- mkutils.mv(tmpfile, filename)
- end
- end
- end
+ local matchfunction = xtpipeslib.get_xtpipes(selfautoparent)
make:match("4oo", matchfunction)
make:match("4om", matchfunction)
- -- is is necessary to execute the above matches as first in the build file
- local matches = make.matches
-- move last match to a first place
- local function move_matches()
- local last = matches[#matches]
- table.insert(matches, 1, last)
- matches[#matches] = nil
- end
-- we need to move last two matches, for 4oo and 4om files
- move_matches()
- move_matches()
+ move_matches(make)
+ move_matches(make)
else
print "Cannot locate xtpipes. Try to set TEXMFROOT variable to a root directory of your TeX distribution"
end
@@ -182,10 +129,19 @@ function M.modify_build(make)
-- execute xtpipes from the build file, instead of t4ht. this fixes issues with wrong paths
-- expanded in tex4ht.env in Miktex or Debian
call_xtpipes(make)
- -- convert XML entities for Unicoe characters produced by Xtpipes to characters
+ -- fix the image dimensions wrongly set by xtpipes
+ local domfilters = domfilter {"t4htlinks","odtimagesize"}
+ make:match("4oo$", domfilters)
+ -- execute it before xtpipes, because we don't want xtpipes to mess with t4htlink elements
+ move_matches(make)
+ -- convert XML entities for Unicode characters produced by Xtpipes to characters
local fixentities = filter {"entities-to-unicode"}
make:match("4oo", fixentities)
make:match("4om", fixentities)
+ -- execute fixentities as first
+ move_matches(make)
+ move_matches(make)
+
-- build the ODT file. This match must be executed as a last one
-- this will be executed as a first match, just to find the last filename
-- in the lgfile
@@ -194,8 +150,9 @@ function M.modify_build(make)
if not executed then
-- this is list of processed files
local lgfiles = make.lgfile.files
- -- find the last one
- local lastfile = lgfiles[#lgfiles] .."$"
+ -- find the last file and escape it so it can be used
+ -- in filename match
+ local lastfile = escape_file(lgfiles[#lgfiles]) .."$"
-- make match for the last file
-- odt packing will be done here
make:match(lastfile, function()