summaryrefslogtreecommitdiff
path: root/support/make4ht/make4ht
blob: 1e7b0d73df089cc5e1af56382027a7ded7e82c65 (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
#!/usr/bin/env texlua
-- Package make4ht. Author Michal Hoftich <michal.h21@gmail.com>
-- This package is subject of LPPL license, version 1.3 
kpse.set_program_name("luatex")

-- logging should be globally available
logging = require "make4ht-logging"
if os.type == "windows" then logging.use_colors = false end
local log = logging.new("make4ht")
local make4ht = require("make4ht-lib")
local lapp    = require("lapp-mk4")
local mkutils = require("mkutils")
local mkparams = require("mkparams")
local mk_config = require("make4ht-config")
-- args string is here just as sample, we dont pass it it to 
-- mkparams.get_args() so default args string is used
local args    =  [[
make4ht - build system for TeX4ht
Usage:
make4ht [options] filename ["tex4ht.sty op." "tex4ht op." "t4ht op" "latex op"]
-c,--config (default xhtml) Custom config file
-d,--output-dir (default nil)  Output directory
-l,--lua  Use lualatex for document compilation
-s,--shell-escape Enables running external programs from LaTeX
-u,--utf8  For output documents in utf8 encoding
-x,--xetex Use xelatex for document compilation
<filename> (string) Input file name
]]

-- set version number. the template should be replaced by the
-- actual version number by the build script
local version = "v0.3i"
mkparams.version_number = version

local args = mkparams.get_args()

local parameters = mkparams.process_args(args) 

log:status("Conversion started")
log:status("Input file: " .. parameters.tex_file)

local mode = parameters.mode
local build_file = parameters.build_file 

-- handle output formats
local allowed_output_formats = {xhtml = true, html5=true, odt = true, docbook=true, tei=true, jats=true}
-- formatter is Lua library which must provide at least prepare_parameters
-- and process_build_sequence functions
local formatter
local output_format = parameters.output_format
if allowed_output_formats[ output_format ] then
  formatter = mkutils.load_output_format(output_format)
else
  -- load html5 as default output format
  if output_format then 
    log:warning("Cannot load output format: ".. output_format)
  end
  formatter = mkutils.load_output_format("html5")
end
-- find make4ht configuration file
local configname = "make4ht"
local conffile = mk_config.find_config(configname) or mk_config.find_xdg_config(configname)
if conffile then
  log:info("Using configuration file: " .. conffile)
  mkutils.load_config(parameters, conffile)
end
local extensions = formatter.prepare_extensions(parameters.extensions)
extensions = mkutils.load_extensions(extensions, output_format)



-- run extensions with prepare_parameters function
parameters = formatter.prepare_parameters(parameters,extensions)
local make = mkutils.load_config(parameters, build_file)["Make"]
make.params = parameters
if make:length() < 1 then
	if mode == "draft" then
		make:htlatex()
  elseif mode == "clean" then
    make:clean()
    make.no_dvi_process = true
	else
		make:htlatex()
		make:htlatex()
		make:htlatex()
	end
end


if not args["no-tex4ht"] and not make.no_dvi_process then
  make:tex4ht()
end

local ext = args.xetex and "xdv" or "dvi"
if #make.image_patterns > 0 then
  make.params.t4ht_par = make.params.t4ht_par .. " -p"
end

if not make.no_dvi_process then
  make:t4ht {ext = ext}
end

-- run extensions which modify the build sequence
if #extensions > 0 then
  make = mkutils.extensions_modify_build(extensions, make)
end

-- allow output formats to modify the build process at the end
make = formatter.modify_build(make) or make

make:match("tmp$", function(filename,params) 
  -- remove the temporary tex file created when the input comes from the standard input
  if params.is_tmp_file then
    log:info("removing temp file", params.tex_file)
    os.remove(params.tex_file)
  end
  -- prevent copying of the temporary file to the outdir
  return false,"tmp file" end
)

make:match(".*",function(filename,par)
	local outdir =  '' --par["outdir"] and par["outdir"] .."/" or ''
	if par['outdir'] ~= "" then outdir = par['outdir'] .. '/' end
	log:info("outdir: "..outdir)
	local outfilename = outdir .. filename
	mkutils.copy(filename,outfilename)
	return true
end)

make:run()

log:status("Conversion finished")
logging.exit_status()