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
|
if not modules then modules = { } end modules ['mtx-convert'] = {
version = 1.001,
comment = "companion to mtxrun.lua",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- todo: eps and svg
local helpinfo = [[
--convertall convert all graphics on path
--inputpath=string original graphics path
--outputpath=string converted graphics path
--watch watch folders
--force force conversion (even if older)
--delay time between sweeps
]]
local application = logs.application {
name = "mtx-convert",
banner = "ConTeXT Graphic Conversion Helpers 0.10",
helpinfo = helpinfo,
}
local report = application.report
graphics = graphics or { }
graphics.converters = graphics.converters or { }
local gsprogram = (os.type == "windows" and "gswin32c") or "gs"
local gstemplate = "%s -q -sDEVICE=pdfwrite -dEPSCrop -dNOPAUSE -dNOCACHE -dBATCH -dAutoRotatePages=/None -dProcessColorModel=/DeviceCMYK -sOutputFile=%s %s -c quit"
function graphics.converters.eps(oldname,newname)
return gstemplate:format(gsprogram,newname,oldname)
end
local improgram = "convert"
local imtemplate = {
low = "%s -quality 0 -compress zip %s pdf:%s",
medium = "%s -quality 75 -compress zip %s pdf:%s",
high = "%s -quality 100 -compress zip %s pdf:%s",
}
function graphics.converters.jpg(oldname,newname)
local ea = environment.arguments
local quality = (ea.high and 'high') or (ea.medium and 'medium') or (ea.low and 'low') or 'high'
return imtemplate[quality]:format(improgram,oldname,newname)
end
graphics.converters.gif = graphics.converters.jpg
graphics.converters.tif = graphics.converters.jpg
graphics.converters.tiff = graphics.converters.jpg
graphics.converters.png = graphics.converters.jpg
local function convert(kind,oldname,newname)
if graphics.converters[kind] then -- extra test
local tmpname = file.replacesuffix(newname,"tmp")
local command = graphics.converters[kind](oldname,tmpname)
report("command: %s",command)
io.flush()
os.spawn(command)
os.remove(newname)
os.rename(tmpname,newname)
if lfs.attributes(newname,"size") == 0 then
os.remove(newname)
end
end
end
function graphics.converters.convertpath(inputpath,outputpath)
inputpath = inputpath or "."
outputpath = outputpath or "."
for name in lfs.dir(inputpath) do
local suffix = file.extname(name)
if name:find("%.$") then
-- skip . and ..
elseif graphics.converters[suffix] then
local oldname = file.join(inputpath,name)
local newname = file.join(outputpath,file.replacesuffix(name,"pdf"))
local et = lfs.attributes(oldname,"modification")
local pt = lfs.attributes(newname,"modification")
if not pt or et > pt then
dir.mkdirs(outputpath)
convert(suffix,oldname,newname)
end
elseif lfs.isdir(inputpath .. "/".. name) then
graphics.converters.convertpath(inputpath .. "/".. name,outputpath .. "/".. name)
end
end
end
function graphics.converters.convertfile(oldname)
local suffix = file.extname(oldname)
if graphics.converters[suffix] then
local newname = file.replacesuffix(name,"pdf")
if oldname == newname then
-- todo: downsample, crop etc
elseif environment.argument("force") then
convert(suffix,oldname,newname)
else
local et = lfs.attributes(oldname,"modification")
local pt = lfs.attributes(newname,"modification")
if not pt or et > pt then
convert(suffix,oldname,newname)
end
end
end
end
scripts = scripts or { }
scripts.convert = scripts.convert or { }
scripts.convert.delay = 5 * 60 -- 5 minutes
function scripts.convert.convertall()
local watch = environment.arguments.watch or false
local delay = environment.arguments.delay or scripts.convert.delay
local input = environment.arguments.inputpath or "."
local output = environment.arguments.outputpath or "."
while true do
graphics.converters.convertpath(input, output)
if watch then
os.sleep(delay)
else
break
end
end
end
function scripts.convert.convertgiven()
local files = environment.files
for i=1,#files do
graphics.converters.convertfile(files[i])
end
end
if environment.argument("convertall") then
scripts.convert.convertall()
elseif environment.files[1] then
scripts.convert.convertgiven()
else
application.help()
end
|