mtx-scite.lua /size: 11 Kb    last modification: 2025-02-21 11:03
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-- apt-get install aspell
10-- apt-get install aspell-de aspell-nl aspell-en aspell-gb  aspell-de  aspell-fr
11--
12-- cat /usr/share/hunspell/xxx.dic | aspell --lang=xx --encoding=utf-8 expand | tr " " "\n" >spell-xx.txt
13
14-- mtxrun --script scite --tree --source=t:/texmf/tex/context --target=e:/tmp/context --numbers
15
16local P, R, S, C, Ct, Cf, Cc, Cg = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cf, lpeg.Cc, lpeg.Cg
17local lpegmatch = lpeg.match
18local format, lower, gmatch = string.format, string.lower, string.gmatch
19
20local helpinfo = [[
21<?xml version="1.0"?>
22<application>
23 <metadata>
24  <entry name="name">mtx-scite</entry>
25  <entry name="detail">Scite Helper Script</entry>
26  <entry name="version">1.00</entry>
27 </metadata>
28 <flags>
29  <category name="basic">
30   <subcategory>
31    <flag name="words"><short>convert spell-*.txt into spell-*.lua</short></flag>
32    <flag name="tree"><short>converts a tree into an html tree (--source --target --numbers)</short></flag>
33    <flag name="file"><short>converts a file into an html file (--source --target --numbers --lexer)</short></flag>
34   </subcategory>
35  </category>
36 </flags>
37</application>
38]]
39
40local application = logs.application {
41    name     = "mtx-scite",
42    banner   = "Scite Helper Script 1.00",
43    helpinfo = helpinfo,
44}
45
46local report = application.report
47
48local scite = require("util-sci")
49
50dofile(resolvers.findfile("char-ini.lua"))
51
52local lower = characters.lower
53
54scripts       = scripts       or { }
55scripts.scite = scripts.scite or { }
56
57-- todo: append to global properties else order of loading problem
58-- linux problem ... files are under root protection so we need --install
59--
60-- local scitesignals = { "scite-context.rme", "context.properties" }
61-- local screenfont   = "lmtypewriter10-regular.ttf"
62
63-- function scripts.scite.start(indeed)
64--     local usedsignal, datapath, fullname, workname, userpath, fontpath
65--     if os.type == "windows" then
66--         workname = "scite.exe"
67--         userpath = os.getenv("USERPROFILE") or ""
68--         fontpath = os.getenv("SYSTEMROOT")
69--         fontpath = (fontpath and file.join(fontpath,"fonts")) or ""
70--     else
71--         workname = "scite"
72--         userpath = os.getenv("HOME") or ""
73--         fontpath = ""
74--     end
75--     local binpaths = file.split_path(os.getenv("PATH")) or file.split_path(os.getenv("path"))
76--     for i=1,#scitesignals do
77--         local scitesignal = scitesignals[i]
78--         local scitepath = resolvers.findfile(scitesignal,"other text files") or ""
79--         if scitepath ~= "" then
80--             scitepath  = file.dirname(scitepath) -- data
81--             if scitepath == "" then
82--                 scitepath = resolvers.cleanpath(lfs.currentdir())
83--             else
84--                 usedsignal, datapath = scitesignal, scitepath
85--                 break
86--             end
87--         end
88--     end
89--     if not datapath or datapath == "" then
90--         report("invalid datapath, maybe you need to regenerate the file database")
91--         return false
92--     end
93--     if not binpaths or #binpaths == 0 then
94--         report("invalid binpath")
95--         return false
96--     end
97--     for i=1,#binpaths do
98--         local p = file.join(binpaths[i],workname)
99--         if lfs.isfile(p) and lfs.attributes(p,"size") > 10000 then -- avoind stub
100--             fullname = p
101--             break
102--         end
103--     end
104--     if not fullname then
105--         report("unable to locate %s",workname)
106--         return false
107--     end
108--     local properties  = dir.glob(file.join(datapath,"*.properties"))
109--     local luafiles    = dir.glob(file.join(datapath,"*.lua"))
110--     local extrafont   = resolvers.findfile(screenfont,"truetype font") or ""
111--     local pragmafound = dir.glob(file.join(datapath,"pragma.properties"))
112--     if userpath == "" then
113--         report("unable to figure out userpath")
114--         return false
115--     end
116--     local verbose = environment.argument("verbose")
117--     local tobecopied, logdata = { }, { }
118--     local function check_state(fullname,newpath)
119--         local basename = file.basename(fullname)
120--         local destination = file.join(newpath,basename)
121--         local pa, da = lfs.attributes(fullname), lfs.attributes(destination)
122--         if not da then
123--             logdata[#logdata+1] = { "new        : %s", basename }
124--             tobecopied[#tobecopied+1] = { fullname, destination }
125--         elseif pa.modification > da.modification then
126--             logdata[#logdata+1] = { "outdated   : %s", basename }
127--             tobecopied[#tobecopied+1] = { fullname, destination }
128--         else
129--             logdata[#logdata+1] = { "up to date : %s", basename }
130--         end
131--     end
132--     for i=1,#properties do
133--         check_state(properties[i],userpath)
134--     end
135--     for i=1,#luafiles do
136--         check_state(luafiles[i],userpath)
137--     end
138--     if fontpath ~= "" then
139--         check_state(extrafont,fontpath)
140--     end
141--     local userpropfile = "SciTEUser.properties"
142--     if os.name ~= "windows" then
143--         userpropfile = "."  .. userpropfile
144--     end
145--     local fullpropfile = file.join(userpath,userpropfile)
146--     local userpropdata = io.loaddata(fullpropfile) or ""
147--     local propfiledone = false
148--     if pragmafound then
149--         if userpropdata == "" then
150--             logdata[#logdata+1] = { "error      : no user properties found on '%s'", fullpropfile }
151--         elseif string.find(userpropdata,"import *pragma") then
152--             logdata[#logdata+1] = { "up to date : 'import pragma' in '%s'", userpropfile }
153--         else
154--             logdata[#logdata+1] = { "yet unset  : 'import pragma' in '%s'", userpropfile }
155--             userproperties = userpropdata .. "\n\nimport pragma\n\n"
156--             propfiledone = true
157--         end
158--     else
159--         if string.find(userpropdata,"import *context") then
160--             logdata[#logdata+1] = { "up to date : 'import context' in '%s'", userpropfile }
161--         else
162--             logdata[#logdata+1] = { "yet unset  : 'import context' in '%s'", userpropfile }
163--             userproperties = userpropdata .. "\n\nimport context\n\n"
164--             propfiledone = true
165--         end
166--     end
167--     if not indeed or verbose then
168--         report("used signal: %s", usedsignal)
169--         report("data path  : %s", datapath)
170--         report("full name  : %s", fullname)
171--         report("user path  : %s", userpath)
172--         report("extra font : %s", extrafont)
173--     end
174--     if #logdata > 0 then
175--         report("")
176--         for k=1,#logdata do
177--             local v = logdata[k]
178--             report(v[1],v[2])
179--         end
180--     end
181--     if indeed then
182--         if #tobecopied > 0 then
183--             report("warning    : copying updated files")
184--             for i=1,#tobecopied do
185--                 local what = tobecopied[i]
186--                 report("copying    : '%s' => '%s'",what[1],what[2])
187--                 file.copy(what[1],what[2])
188--             end
189--         end
190--         if propfiledone then
191--             report("saving     : '%s'",userpropfile)
192--             io.savedata(fullpropfile,userpropdata)
193--         end
194--         os.launch(fullname)
195--     end
196-- end
197
198-- local splitter = (Cf(Ct("") * (Cg(C(R("az","AZ","\127\255")^1) * Cc(true)) + P(1))^1,rawset) )^0
199--
200-- local function splitwords(words)
201--     return lpegmatch(splitter,words) -- or just split and tohash
202-- end
203
204local function splitwords(words)
205    local w = { }
206    for s in string.gmatch(words,"%S+") do
207        if #s > 2 then -- will become option
208            w[lower(s)] = s
209        end
210    end
211    local u = { }
212    for s in string.utfcharacters(words) do
213        u[s] = true
214    end
215    for k, v in next, u do
216        u[k] = lower(k)
217    end
218    u["\n"] = nil
219    u["\r"] = nil
220    return w, u
221end
222
223function scripts.scite.words()
224    for i=1,#environment.files do
225        local tag = environment.files[i]
226        local tag = string.match(tag,"spell%-(..)%.") or tag
227        local txtname = format("spell-%s.txt",tag)
228        local luaname = format("spell-%s.lua",tag)
229        local lucname = format("spell-%s.luc",tag)
230        if lfs.isfile(txtname) then
231            report("loading %s",txtname)
232            local olddata = io.loaddata(txtname) or ""
233            local words, lower = splitwords(olddata)
234            local min, max, n = 100, 1, 0
235            for k, v in next, words do
236                local l = #k
237                if l < min then
238                    min = l
239                end
240                if l > max then
241                    max = l
242                end
243                n = n + 1
244            end
245            if min > max then
246                min = max
247            end
248            local newdata = {
249                words  = words,
250                source = oldname,
251                min    = min,
252                max    = max,
253                n      = n,
254                lower  = lower,
255            }
256            report("saving %q, %s words, %s shortest, %s longest",luaname,n,min,max)
257            io.savedata(luaname,table.serialize(newdata,true))
258         -- report("compiling %q",lucname)
259         -- os.execute(format("luac -s -o %s %s",lucname,luaname))
260        else
261            report("no data file %s",txtname)
262        end
263    end
264    report("you need to move the lua files to lexers/data")
265end
266
267function scripts.scite.tree()
268    local source  = environment.argument("source")
269    local target  = environment.argument("target")
270    local numbers = environment.argument("numbers")
271    if not source or not lfs.isdir(source) then
272        report("you need to pass a valid source path with --source")
273        return
274    end
275    if not target or not lfs.isdir(target) then
276        report("you need to pass a valid target path with --target")
277        return
278    end
279    if source == target then
280        report("source and target paths must be different")
281        return
282    end
283    scite.converttree(source,target,numbers)
284end
285
286function scripts.scite.file()
287    local source  = environment.argument("source")
288    local target  = environment.argument("target")
289    local lexer   = environment.argument("lexer")
290    local numbers = environment.argument("numbers")
291    if source then
292        local target = target or file.replacesuffix(source,"html")
293        if source == target then
294            report("the source file cannot be the same as the target")
295        else
296            scite.filetohtml(source,lexer,target,numbers)
297        end
298
299    else
300        for i=1,#environment.files do
301            local source  = environment.files[i]
302            local target  = file.replacesuffix(source,"html")
303            if source == target then
304                report("the source file cannot be the same as the target")
305            else
306                scite.filetohtml(source,nil,target,numbers)
307            end
308        end
309    end
310end
311
312-- if environment.argument("start") then
313--     scripts.scite.start(true)
314-- elseif environment.argument("test") then
315--     scripts.scite.start()
316-- else
317--     application.help()
318-- end
319
320if environment.argument("words") then
321    scripts.scite.words()
322elseif environment.argument("tree") then
323    scripts.scite.tree()
324elseif environment.argument("file") then
325    scripts.scite.file()
326elseif environment.argument("exporthelp") then
327    application.export(environment.argument("exporthelp"),environment.files[1])
328else
329    application.help()
330end
331
332