1if not modules then modules = { } end modules ['mtx-scite'] = {
2 version = 1.001,
3 comment = "companion to mtxrun.lua",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9
10
11local P, R, S, C, Ct, Cf, Cc, Cg = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cf, lpeg.Cc, lpeg.Cg
12local lpegmatch = lpeg.match
13local format, lower, gmatch = string.format, string.lower, string.gmatch
14
15local helpinfo = [[
16<?xml version="1.0"?>
17<application>
18 <metadata>
19 <entry name="name">mtx-scite</entry>
20 <entry name="detail">Scite Helper Script</entry>
21 <entry name="version">1.00</entry>
22 </metadata>
23 <flags>
24 <category name="basic">
25 <subcategory>
26 <flag name="words"><short>convert spell-*.txt into spell-*.lua</short></flag>
27 <flag name="tree"><short>converts a tree into an html tree (--source --target --numbers)</short></flag>
28 <flag name="file"><short>converts a file into an html file (--source --target --numbers --lexer)</short></flag>
29 </subcategory>
30 </category>
31 </flags>
32</application>
33]]
34
35local application = logs.application {
36 name = "mtx-scite",
37 banner = "Scite Helper Script 1.00",
38 helpinfo = helpinfo,
39}
40
41local report = application.report
42
43local scite = require("util-sci")
44
45scripts = scripts or { }
46scripts.scite = scripts.scite or { }
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
195local function splitwords(words)
196 local w = { }
197 for s in string.gmatch(words,"[a-zA-Z\127-255]+") do
198 if #s > 2 then
199 w[lower(s)] = s
200 end
201 end
202 return w
203end
204
205
206
207function scripts.scite.words()
208 for i=1,#environment.files do
209 local tag = environment.files[i]
210 local tag = string.match(tag,"spell%-(..)%.") or tag
211 local txtname = format("spell-%s.txt",tag)
212 local luaname = format("spell-%s.lua",tag)
213 local lucname = format("spell-%s.luc",tag)
214 if lfs.isfile(txtname) then
215 report("loading %s",txtname)
216 local olddata = io.loaddata(txtname) or ""
217 local words = splitwords(olddata)
218 local min, max, n = 100, 1, 0
219 for k, v in next, words do
220 local l = #k
221 if l < min then
222 min = l
223 end
224 if l > max then
225 max = l
226 end
227 n = n + 1
228 end
229 if min > max then
230 min = max
231 end
232 local newdata = {
233 words = words,
234 source = oldname,
235 min = min,
236 max = max,
237 n = n,
238 }
239 report("saving %q, %s words, %s shortest, %s longest",luaname,n,min,max)
240 io.savedata(luaname,table.serialize(newdata,true))
241 report("compiling %q",lucname)
242 os.execute(format("luac -s -o %s %s",lucname,luaname))
243 else
244 report("no data file %s",txtname)
245 end
246 end
247 report("you need to move the lua files to lexers/data")
248end
249
250function scripts.scite.tree()
251 local source = environment.argument("source")
252 local target = environment.argument("target")
253 local numbers = environment.argument("numbers")
254 if not source or not lfs.isdir(source) then
255 report("you need to pass a valid source path with --source")
256 return
257 end
258 if not target or not lfs.isdir(target) then
259 report("you need to pass a valid target path with --target")
260 return
261 end
262 if source == target then
263 report("source and target paths must be different")
264 return
265 end
266 scite.converttree(source,target,numbers)
267end
268
269function scripts.scite.file()
270 local source = environment.argument("source")
271 local target = environment.argument("target")
272 local lexer = environment.argument("lexer")
273 local numbers = environment.argument("numbers")
274 if source then
275 local target = target or file.replacesuffix(source,"html")
276 if source == target then
277 report("the source file cannot be the same as the target")
278 else
279 scite.filetohtml(source,lexer,target,numbers)
280 end
281
282 else
283 for i=1,#environment.files do
284 local source = environment.files[i]
285 local target = file.replacesuffix(source,"html")
286 if source == target then
287 report("the source file cannot be the same as the target")
288 else
289 scite.filetohtml(source,nil,target,numbers)
290 end
291 end
292 end
293end
294
295
296
297
298
299
300
301
302
303if environment.argument("words") then
304 scripts.scite.words()
305elseif environment.argument("tree") then
306 scripts.scite.tree()
307elseif environment.argument("file") then
308 scripts.scite.file()
309elseif environment.argument("exporthelp") then
310 application.export(environment.argument("exporthelp"),environment.files[1])
311else
312 application.help()
313end
314
315 |