1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
module(...,package.seeall)
local eb = require("exec_epub")
local ext = "xhtml"
local outputdir = nil
local input = nil
function prepare(params)
local basedir = params.input.."-".. params.format
local outputdir_name="OEBPS"
outputdir= basedir.."/"..outputdir_name
input = params.input
params.ext = ext
params.packages = params.packages .. string.format("\\Configure{ext}{%s}",ext)
return eb.prepare(params)
end
function run(out,params)
return eb.run(out, params)
end
local function makeTOC(document)
local template = [[
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:epub="http://www.idpf.org/2007/ops"
>
<head><title>TOC</title></head>
<body>
<nav id="pub-toc" epub:type="toc">
<h1>Table of contents</h1>
<ol class="toc" hidden="hidden">
<li>
<a href="${document}">Document</a>
</li>
</ol>
</nav>
</body>
</html>
]] % {document=document}
return template
end
local function cleanOPF()
-- in epub3, there must be table of contents
-- if there is no toc in the document, we must add generic one
local opf = "content.opf"
local f = io.open(opf,"r")
if not f then
print("Cannot open "..opf .. " for toc searching")
return nil
end
local content = f:read("*all")
f:close()
if content:find "properties[%s]*=[%s]*\"[^\"]*nav" then
print "TOC nav found"
else
print "no TOC, using generic one"
local inputfile = input .. "." .. ext
print("Main file name", inputfile)
-- write toc file
local toc_name = "generic_toc" .."."..ext
local f = io.open(outputdir .. "/" .. toc_name, "w")
f:write(makeTOC(inputfile))
f:close()
-- add toc file to the conten.opf
content = content:gsub("<manifest>","<manifest>\n<item id='htmltoc'" ..
" properties=\"nav\" media-type=\"application/xhtml+xml\" href=\""..
toc_name .."\" />\n")
content = content:gsub("<spine([^>]*)>", "<spine%1>\n<itemref idref=\"htmltoc\" linear=\"no\"/>\n")
-- remove empty guide element
end
content = content:gsub("<guide>%s*</guide>","")
f = io.open(outputdir .. "/" ..opf,"w")
f:write(content)
f:close()
--makeTOC(inputfile)
end
function writeContainer()
--local ret = eb.writeContainer()
eb.make_opf()
cleanOPF()
local ret = eb.pack_container()
return ret
end
function clean()
return eb.clean()
end
|