mtx-scite.lua /size: 11 Kb    last modification: 2020-07-01 14:35
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-- mtxrun --script scite --tree --source=t:/texmf/tex/context --target=e:/tmp/context --numbers
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-- todo: append to global properties else order of loading problem
49-- linux problem ... files are under root protection so we need --install
50--
51-- local scitesignals = { "scite-context.rme", "context.properties" }
52-- local screenfont   = "lmtypewriter10-regular.ttf"
53
54-- function scripts.scite.start(indeed)
55--     local usedsignal, datapath, fullname, workname, userpath, fontpath
56--     if os.type == "windows" then
57--         workname = "scite.exe"
58--         userpath = os.getenv("USERPROFILE") or ""
59--         fontpath = os.getenv("SYSTEMROOT")
60--         fontpath = (fontpath and file.join(fontpath,"fonts")) or ""
61--     else
62--         workname = "scite"
63--         userpath = os.getenv("HOME") or ""
64--         fontpath = ""
65--     end
66--     local binpaths = file.split_path(os.getenv("PATH")) or file.split_path(os.getenv("path"))
67--     for i=1,#scitesignals do
68--         local scitesignal = scitesignals[i]
69--         local scitepath = resolvers.findfile(scitesignal,"other text files") or ""
70--         if scitepath ~= "" then
71--             scitepath  = file.dirname(scitepath) -- data
72--             if scitepath == "" then
73--                 scitepath = resolvers.cleanpath(lfs.currentdir())
74--             else
75--                 usedsignal, datapath = scitesignal, scitepath
76--                 break
77--             end
78--         end
79--     end
80--     if not datapath or datapath == "" then
81--         report("invalid datapath, maybe you need to regenerate the file database")
82--         return false
83--     end
84--     if not binpaths or #binpaths == 0 then
85--         report("invalid binpath")
86--         return false
87--     end
88--     for i=1,#binpaths do
89--         local p = file.join(binpaths[i],workname)
90--         if lfs.isfile(p) and lfs.attributes(p,"size") > 10000 then -- avoind stub
91--             fullname = p
92--             break
93--         end
94--     end
95--     if not fullname then
96--         report("unable to locate %s",workname)
97--         return false
98--     end
99--     local properties  = dir.glob(file.join(datapath,"*.properties"))
100--     local luafiles    = dir.glob(file.join(datapath,"*.lua"))
101--     local extrafont   = resolvers.findfile(screenfont,"truetype font") or ""
102--     local pragmafound = dir.glob(file.join(datapath,"pragma.properties"))
103--     if userpath == "" then
104--         report("unable to figure out userpath")
105--         return false
106--     end
107--     local verbose = environment.argument("verbose")
108--     local tobecopied, logdata = { }, { }
109--     local function check_state(fullname,newpath)
110--         local basename = file.basename(fullname)
111--         local destination = file.join(newpath,basename)
112--         local pa, da = lfs.attributes(fullname), lfs.attributes(destination)
113--         if not da then
114--             logdata[#logdata+1] = { "new        : %s", basename }
115--             tobecopied[#tobecopied+1] = { fullname, destination }
116--         elseif pa.modification > da.modification then
117--             logdata[#logdata+1] = { "outdated   : %s", basename }
118--             tobecopied[#tobecopied+1] = { fullname, destination }
119--         else
120--             logdata[#logdata+1] = { "up to date : %s", basename }
121--         end
122--     end
123--     for i=1,#properties do
124--         check_state(properties[i],userpath)
125--     end
126--     for i=1,#luafiles do
127--         check_state(luafiles[i],userpath)
128--     end
129--     if fontpath ~= "" then
130--         check_state(extrafont,fontpath)
131--     end
132--     local userpropfile = "SciTEUser.properties"
133--     if os.name ~= "windows" then
134--         userpropfile = "."  .. userpropfile
135--     end
136--     local fullpropfile = file.join(userpath,userpropfile)
137--     local userpropdata = io.loaddata(fullpropfile) or ""
138--     local propfiledone = false
139--     if pragmafound then
140--         if userpropdata == "" then
141--             logdata[#logdata+1] = { "error      : no user properties found on '%s'", fullpropfile }
142--         elseif string.find(userpropdata,"import *pragma") then
143--             logdata[#logdata+1] = { "up to date : 'import pragma' in '%s'", userpropfile }
144--         else
145--             logdata[#logdata+1] = { "yet unset  : 'import pragma' in '%s'", userpropfile }
146--             userproperties = userpropdata .. "\n\nimport pragma\n\n"
147--             propfiledone = true
148--         end
149--     else
150--         if string.find(userpropdata,"import *context") then
151--             logdata[#logdata+1] = { "up to date : 'import context' in '%s'", userpropfile }
152--         else
153--             logdata[#logdata+1] = { "yet unset  : 'import context' in '%s'", userpropfile }
154--             userproperties = userpropdata .. "\n\nimport context\n\n"
155--             propfiledone = true
156--         end
157--     end
158--     if not indeed or verbose then
159--         report("used signal: %s", usedsignal)
160--         report("data path  : %s", datapath)
161--         report("full name  : %s", fullname)
162--         report("user path  : %s", userpath)
163--         report("extra font : %s", extrafont)
164--     end
165--     if #logdata > 0 then
166--         report("")
167--         for k=1,#logdata do
168--             local v = logdata[k]
169--             report(v[1],v[2])
170--         end
171--     end
172--     if indeed then
173--         if #tobecopied > 0 then
174--             report("warning    : copying updated files")
175--             for i=1,#tobecopied do
176--                 local what = tobecopied[i]
177--                 report("copying    : '%s' => '%s'",what[1],what[2])
178--                 file.copy(what[1],what[2])
179--             end
180--         end
181--         if propfiledone then
182--             report("saving     : '%s'",userpropfile)
183--             io.savedata(fullpropfile,userpropdata)
184--         end
185--         os.launch(fullname)
186--     end
187-- end
188
189-- local splitter = (Cf(Ct("") * (Cg(C(R("az","AZ","\127\255")^1) * Cc(true)) + P(1))^1,rawset) )^0
190--
191-- local function splitwords(words)
192--     return lpegmatch(splitter,words) -- or just split and tohash
193-- end
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 -- will become option
199            w[lower(s)] = s
200        end
201    end
202    return w
203end
204
205-- maybe: lowerkey = UpperWhatever
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-- if environment.argument("start") then
296--     scripts.scite.start(true)
297-- elseif environment.argument("test") then
298--     scripts.scite.start()
299-- else
300--     application.help()
301-- end
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