-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.lua
161 lines (149 loc) · 5.54 KB
/
build.lua
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
#!/usr/bin/env texlua
-- Build script for zhlipsum.
module = "zhlipsum"
sourcefiles = {"*.dtx"}
installfiles = {"*.sty", "*.def"}
unpackfiles = {"zhlipsum.dtx"}
gbkfiles = {"zhlipsum-gbk.def"}
big5files = {"zhlipsum-big5.def"}
unpackexe = "xetex"
unpackopts = "-file-line-error -halt-on-error -interaction=batchmode"
typesetfiles = {"zhlipsum.dtx", "zhlipsum-en.tex"}
typesetsuppfiles = {"ctxdoc.cls"}
typesetexe = "xelatex"
makeindexexe = "zhmakeindex"
stdengine = "xetex"
checkengines = {"pdftex", "xetex", "luatex"}
specialformats = {}
specialformats.latex = {
pdftex = {binary = "latex", options = "-output-format=dvi"},
uptex = {binary = "euptex"}
}
function docinit_hook(file)
get_en_doc(typesetdir .. "/" .. typesetfiles[1],
typesetdir .. "/" .. typesetfiles[2])
return 0
end
function get_en_doc(input, output)
local file_banner = "%%\n"
.. "%% This is file `zhlipsum-en.tex',\n"
.. "%% generated with Lua script `get-doc-en.lua'.\n"
.. "%%\n"
.. "%% The original source files were:\n"
.. "%%\n"
.. "%% zhlipsum.dtx\n"
local tag_preamble_str = "\\preamble"
local tag_endpreamble_str = "\\endpreamble"
local tag_driver_begin_str = "%<*driver>"
local tag_driver_end_str = "%</driver>"
local tag_inline_str = "%^^A!"
local tag_begin_str = "%^^A+"
local tag_end_str = "%^^A-"
local tag_str_len = string.len(tag_inline_str)
local function test_tag(str, tag)
return string.sub(str, 1, string.len(tag)) == tag
end
local function remove_normal_space(str)
-- Normal text lines begin with 2 spaces.
-- `3` is the first non-space char's position.
return string.sub(str, 3)
end
local function process_preamble_line(str)
return "%% " .. str
end
local function process_verbatim_line(str)
return remove_normal_space(str)
end
local function process_normal_line(str)
return string.sub(str, tag_str_len + 2)
end
-- Test whether it's in the preamble.
local preamble_flag = 0
-- Test whether it's in the driver part.
local driver_flag = 0
-- Test whether it's in the verbatim environment.
local inside_flag = 0
-- Begin processing
local input_file = io.open(input, "r")
local output_file = io.open(output, "w")
output_file:write(file_banner)
for line in input_file:lines() do
-- Check for the preamble, as DocStrip does.
if test_tag(line, tag_preamble_str) then
preamble_flag = preamble_flag + 1
elseif test_tag(line, tag_endpreamble_str) then
preamble_flag = preamble_flag - 1
else
-- Check if in the driver part.
if test_tag(line, tag_driver_begin_str) then
driver_flag = driver_flag + 1
elseif test_tag(line, tag_driver_end_str) then
driver_flag = driver_flag - 1
else
-- If beginning with `%^^A+` or `%^^A-`, then increase or
-- decrease the flag, in order to determine the start or end
-- position of verbatim.
if test_tag(line, tag_begin_str) then
inside_flag = inside_flag + 1
elseif test_tag(line, tag_end_str) then
inside_flag = inside_flag - 1
else
if preamble_flag == 1 then
output_file:write(process_preamble_line(line), "\n")
elseif driver_flag == 1 then
-- Inside <*driver> ... </driver>.
-- Similar to normal text but do not have `% ` at beginning.
if inside_flag == 1 then
output_file:write(line, "\n")
elseif test_tag(line, tag_inline_str) then
output_file:write(process_normal_line(line), "\n")
end
elseif inside_flag == 1 then
-- If inside_flag = 1, then it's a verbatim environment.
output_file:write(process_verbatim_line(line), "\n")
elseif test_tag(line, tag_inline_str) then
-- If beginning with `%^^A!`, then this line is normal text.
output_file:write(process_normal_line(line), "\n")
end
end
end
end
end
input_file:close()
output_file:close()
end
function zhconv(input, output, encoding)
local input_tmp = input .. ".tmp"
local cmd_cp = "cp" .. " " .. input .. " " .. input_tmp
local cmd_rm = "rm" .. " " .. input_tmp
local cmd_conv = "iconv" .. " " .. "--from-code=UTF-8"
.. " " .. "--to-code=" .. encoding
.. " <" .. input_tmp
.. " >" .. output
run(maindir, cmd_cp)
run(maindir, cmd_conv)
run(maindir, cmd_rm)
end
function hooked_bundleunpack(sourcedirs, sources)
unhooked_bundleunpack(sourcedirs, sources)
-- UTF-8 to GBK conversion
for _, glob in ipairs(gbkfiles) do
for _, f in ipairs(filelist(unpackdir, glob)) do
local f_utf = unpackdir .. "/" .. f
zhconv(f_utf, f_utf, "GBK")
end
end
-- UTF-8 to Big5 conversion
for _, glob in ipairs(big5files) do
for _, f in ipairs(filelist(unpackdir, glob)) do
local f_utf = unpackdir .. "/" .. f
zhconv(f_utf, f_utf, "BIG-5")
end
end
end
local l3build_main = main
function main(target, names)
unhooked_bundleunpack = bundleunpack
bundleunpack = hooked_bundleunpack
l3build_main(target, names)
end