summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/scripts/tex4ebook/exec_epub.lua')
-rwxr-xr-xMaster/texmf-dist/scripts/tex4ebook/exec_epub.lua52
1 files changed, 37 insertions, 15 deletions
diff --git a/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua b/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua
index ff5784bcdb3..1311516bfb6 100755
--- a/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua
+++ b/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua
@@ -133,6 +133,8 @@ function make_opf()
local dir_part = item:split("/")
table.remove(dir_part,#dir_part)
local id=table.concat(dir_part,"-")..fname.."_"..ext
+ -- remove invalid characters from id start
+ id = id:gsub("^[%.%-]*","")
return "<item id='"..id .. "' href='"..item.."' media-type='"..mimetype.."' />",id
end
local find_all_files= function(s,r)
@@ -161,13 +163,22 @@ function make_opf()
local opf_complete = {}
table.insert(opf_complete,h_first:read("*all"))
local used_html = find_all_files(opf_complete[1])
- local lg_file = ebookutils.parse_lg(outputfilename..".lg")
- local used_files = lg_file["files"]
+ -- local lg_file = ebookutils.parse_lg(outputfilename..".lg")
+ -- The lg_file has been already loaded by make4ht, it doesn't make sense to load it again
+ -- Furthermore, it is now possible to add new files from Lua build files
+ local lg_file = Make.lgfile or ebookutils.parse_lg(outputfilename..".lg")
+ local used_files = {}
+ for _,filename in ipairs(lg_file["files"]) do
+ -- we need to test the filenames in order to prevent duplicates
+ -- filenames are tested without paths, so there may be issues if
+ -- the same filename is used in different directories. Is that a problem?
+ used_files[filename] = true
+ end
--[[for f in lfs.dir("./OEBPS") do
--table.insert(used_files,f)
--used_files[f] = true
end--]]
- local all_html = find_all_files(table.concat(used_files,"\n"))
+ -- local all_html = find_all_files(table.concat(used_files,"\n"))
local outside_spine = {}
-- This was duplicated code
--[[for i, ext in pairs(all_html) do
@@ -180,6 +191,7 @@ function make_opf()
end--]]
local all_used_files = find_all_files(opf_complete[1],"([%a%d%-%_]+%.[%a%d]+)")
local used_paths = {}
+ local used_ids = {}
for _,k in ipairs(lg_file["files"]) do
local ext = k:match("%.([%a%d]*)$")
local parts = k:split "/"
@@ -188,11 +200,11 @@ function make_opf()
table.remove(parts,#parts)
--table.insert(parts,1,"OEBPS")
table.insert(parts,1,outputdir)
- --print("SSSSS "..fn.." ext .." .. ext)
+ -- print("SSSSS "..fn.." ext .." .. ext)
--if string.find("jpg gif png", ext) and not all_used_files[k] then
local item,id = lg_item(k)
if item then
- local path = table.concat(parts)
+ local path = table.concat(parts)
if not used_paths[path] then
ebookutils.mkdirectories(parts)
used_paths[path]=true
@@ -206,20 +218,28 @@ function make_opf()
print "Tidy: Cannot load tidyconf.conf"
end
end
- ebookutils.copy(k, outputdir .. "/"..k)
- if not all_used_files[fn] then
- table.insert(opf_complete,item)
- if allow_in_spine[ext] then
- table.insert(outside_spine,id)
- end
+ if not used_ids[id] then
+ ebookutils.copy(k, outputdir .. "/"..k)
+ if not all_used_files[fn] then
+ table.insert(opf_complete,item)
+ if allow_in_spine[ext] then
+ table.insert(outside_spine,id)
+ end
+ end
end
+ used_ids[id] = true
end
end
for _,f in ipairs(lg_file["images"]) do
local f = f.output
- local p = lg_item(f)
- ebookutils.copy(f, outputdir .. "/"..f)
- table.insert(opf_complete,p)
+ local p, id = lg_item(f)
+ -- process the images only if they weren't registered in lg_file["files"]
+ -- they would be processed twice otherwise
+ if not used_files[f] and not used_ids[id] then
+ ebookutils.copy(f, outputdir .. "/"..f)
+ table.insert(opf_complete,p)
+ end
+ used_ids[id] = true
end
local end_opf = h_second:read("*all")
local spine_items = {}
@@ -283,6 +303,8 @@ function make_opf()
end
print(mimetype)
local zip = find_zip()
+ -- we need to remove the epub file if it exists already, because it may contain files which aren't used anymore
+ if ebookutils.file_exists(outputfile) then os.remove(outputfile) end
print("Pack mimetype " .. os.execute("cd "..basedir.." && "..zip.." -q0X "..outputfile .." ".. mimetype_name))
print("Pack metadir " .. os.execute("cd "..basedir.." && "..zip.." -qXr9D " .. outputfile.." "..metadir_name))
print("Pack outputdir " .. os.execute("cd "..basedir.." && "..zip.." -qXr9D " .. outputfile.." "..outputdir_name))
@@ -309,4 +331,4 @@ end
--deldir(outputdir)
--deldir(metadir)
--os.remove(mimetype)
- end
+end