summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/tex4ebook/tex4ebook
blob: a95bf44c236fafef7084c7717c38be1d0817ee75 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env texlua
-- Package tex4ebook. Author Michal Hoftich <michal.h21@gmail.com>
-- This package is subject of LPPL license, version 1.3 
--
kpse.set_program_name("luatex")
require("lapp-mk4")
-- require("ebookutils")
local ebookutils = require "mkutils"
local mkparams = require "mkparams"
local mk_config = require "make4ht-config"

-- Setting
local latex_cmd="latex"
local copy_cmd="copy"
local move_cmd="move"
local env_param="%%" 
local htlatex_call=""
-- These correspond to htlatex parameters
local tex4ht_sty_par=""
local tex4ht_par=""
local t4ht_par=""
local latex_par=""
local output_formats={epub=true,mobi=true,epub3=true}
local executor=nil
local tidy = false
local include_fonts = false
local arg_message = [[
tex4ebook - ebook generation support for LaTeX
Usage:
tex4ebook [switches] filename ["tex4ht.sty op."] ["tex4ht op."] ["t4ht op"] ["latex op"]
  -c,--config (default xhtml) Custom config file
  -d,--output-dir (default nil)  Output directory
  -e,--build-file (default nil)  If build file is different than `filename`.mk4
  -f,--format (default epub) Output format. Supported values: epub, epub3, mobi
  -h,--help  Display help message
  -l,--lua  Use lualatex for document compilation
  -m,--mode (default default) Switch which can be used in the makefile
  -r,--resolution (default 167)
  -s,--shell-escape  Enable shell escape in htlatex run
  -t,--tidy Run html tidy on html output. May result in wrong spacing!
  -v,--version  Display version number
  -x,--xetex Use xelatex for document compilation
  
  <filename> (string) Input file name

Positional optional argumens:
  ["tex4ht.sty op."]  Additional parameters for tex4ht.sty
  ["tex4ht op."]      Options for tex4ht command
  ["t4ht op"]         Options for t4ht command
  ["latex op"]        Additional options for LaTeX

Documentation:                    https://tug.org/applications/tex4ht/mn.html
Issue tracker for tex4ht bugs:    https://puszcza.gnu.org.ua/bugs/?group=tex4ht
Issue tracker for tex4ebook bugs: https://github.com/michal-h21/tex4ebook/issues
]]

-- This option is no longer available, all files must be unicode
-- -u,--utf8 
local args=lapp(arg_message)

if args["filename"] == nil and args["version"] == false then
  print(arg_message) 
  return
else
  input_file=args["filename"]
end

if args.version then
  print "tex4ebook v0.2c"
  return 
end

if args.lua then
  latex_cmd="dvilualatex"
elseif args.xetex then
  latex_cmd="xelatex --no-pdf"
end


if args["shell-escape"] then 
  latex_par = latex_par .. " -shell-escape"
end

if args["include-fonts"] then 
  include_fonts = true
end

-- local mathml = ","
-- if args["mathml"] then
--   -- mathml = ",mathml,"
--   tex4ht_sty_par = tex4ht_sty_par .. ",mathml"
-- end
local   mode = args.mode or "default"

if os.type=="unix" then
  env_param="$"
  copy_cmd="cp"
  move_cmd="mv"
  t4ht_dir_format="%s/"
else 
  env_param="%%"
  copy_cmd="copy"
  move_cmd="move"
  t4ht_dir_format="%s"
end

if args.tidy then 
  tidy = true
else
  tidy = false
end
-- Env file copying 

--[[if not ebookutils.file_exists("tex4ht.env") then
local env_file = kpse.find_file("epub2.env")
ebookutils.copy_filter(env_file,"tex4ht.env",function(s) return s % {
move = move_cmd,
copy = copy_cmd,
resolution = args.resolution
} end)
end--]]

--print ("nazdar ${world}" % {world="svete"})
--print(args.config)


local input = ebookutils.remove_extension(input_file)
local config=ebookutils.remove_extension(args.config)
-- local tex4ht_sty_par = config ..tex4ht_sty_par..","+args.format
--local sty_args =  args[2] and ", " .. args[2]  or ""
local sty_args = ""
if args[2] then 
  sty_args = "," .. args[2]
end
local tex4ht_sty_par = tex4ht_sty_par + sty_args --args[2]
local tex4ht_par = tex4ht_par +args[3]
local t4ht_par = t4ht_par + args[4]
local latex_par = latex_par + args[5]

-- use default parameter processing from make4ht
-- add some needed args
args.filename=input_file
local params = mkparams.process_args(args)


local output_format = params.output_format --args.format
-- use epub as default output_format
output_format = output_format or "epub"
local extensions = ebookutils.load_extensions(params.extensions, output_format)
-- but also support tex4ebook!s own parameters
local oldparams = {
  -- htlatex=latex_cmd
  -- ,input=input 
  -- ,tex_file = tex_file
  format= output_format
  -- ,latex_par=latex_par
  -- ,tex4ht_sty_par=tex4ht_sty_par
  -- ,tex4ht_par=tex4ht_par
  -- ,t4ht_par=t4ht_par
  -- ,mode = mode
  ,t4ht_dir_format=t4ht_dir_format
  ,tidy = tidy
  ,include_fonts = include_fonts
  ,resolution=args.resolution
  ,mathml=mathml
  ,packages="\\RequirePackage{tex4ebook}"
}  

-- extend params
for k,v in pairs(oldparams) do
  params[k] = v
end

params.tex4ht_sty_par=params.tex4ht_sty_par .. ",charset=utf-8,"..output_format
params.tex4ht_par= " -cmozhtf -utf8" .. params.tex4ht_par


if output_formats[output_format] then
  executor=require("exec_"..output_format)
  params=executor.prepare(params)
  if #extensions > 0 then
    params = ebookutils.extensions_prepare_parameters(extensions,params)
  end
else
  print("Unknown output format: "..output_format)
  return
end

-- find tex4ebook configuration file
local configname = "tex4ebook"
local conffile = mk_config.find_config(configname) or mk_config.find_xdg_config(configname)
if conffile then
  print("Using configuration file: " .. conffile)
  ebookutils.load_config(params, conffile)
else
  print "No conffile"
end

local build_file = input.. ".mk4"

if args["build-file"] and args["build-file"] ~= "nil"  then 
	build_file = args["build-file"] 
end

local config_file = ebookutils.load_config(params, build_file)

params["config_file"] = config_file
--config_file.Make:run()
print("${htlatex} ${input} \"${tex4ht_sty_par}\" \"${tex4ht_par}\" \"${t4ht_par}\" \"${latex_par}\"" % params)
if #extensions > 0 then
  config_file.Make = ebookutils.extensions_modify_build(extensions, config_file.Make)
end

executor.run(input,params)
executor.writeContainer()
executor.clean()
--print(args[1])