diff options
author | Taco Hoekwater <taco@elvenkind.com> | 2011-06-01 08:54:21 +0000 |
---|---|---|
committer | Taco Hoekwater <taco@elvenkind.com> | 2011-06-01 08:54:21 +0000 |
commit | d7ccb42582f85acf30568913610ccf4d602023fb (patch) | |
tree | 7292e3545a420676878e7451b68892d360c62cb6 /Master/texmf-dist/scripts/context/lua/mtx-interface.lua | |
parent | 2d62a6fe9b80def59c392268022f1f9a2d6e358f (diff) |
commit context 2011.05.18
git-svn-id: svn://tug.org/texlive/trunk@22719 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/context/lua/mtx-interface.lua')
-rw-r--r-- | Master/texmf-dist/scripts/context/lua/mtx-interface.lua | 423 |
1 files changed, 340 insertions, 83 deletions
diff --git a/Master/texmf-dist/scripts/context/lua/mtx-interface.lua b/Master/texmf-dist/scripts/context/lua/mtx-interface.lua index 730a030d930..954cb449870 100644 --- a/Master/texmf-dist/scripts/context/lua/mtx-interface.lua +++ b/Master/texmf-dist/scripts/context/lua/mtx-interface.lua @@ -6,7 +6,39 @@ if not modules then modules = { } end modules ['mtx-cache'] = { license = "see context related readme files" } -local format = string.format +local concat, sort, insert = table.concat, table.sort, table.insert +local gsub, format, gmatch, find = string.gsub, string.format, string.gmatch, string.find +local utfchar, utfgsub = utf.char, utf.gsub + +local helpinfo = [[ +--interfaces generate context interface files +--messages generate context message files +--labels generate context label files + +--context equals --interfaces --messages --languages + +--scite generate scite interface +--bbedit generate bbedit interface files +--jedit generate jedit interface files +--textpad generate textpad interface files +--text create text files for commands and environments +--raw report commands to the console +--check generate check file + +--toutf replace named characters by utf +--preprocess preprocess mkvi files to tex files [force,suffix] + +--suffix use given suffix for output files +--force force action even when in doubt +]] + +local application = logs.application { + name = "mtx-interface", + banner = "ConTeXt Interface Related Goodies 0.13", + helpinfo = helpinfo, +} + +local report = application.report scripts = scripts or { } scripts.interface = scripts.interface or { } @@ -15,11 +47,11 @@ local flushers = { } local userinterfaces = { 'en','cs','de','it','nl','ro','fr','pe' } local messageinterfaces = { 'en','cs','de','it','nl','ro','fr','pe','no' } -function flushers.scite(interface,collection) +function flushers.scite(interface,commands) local result, i = {}, 0 result[#result+1] = format("keywordclass.macros.context.%s=",interface) - for i=1,#collection do - local command = collection[i] + for i=1,#commands do + local command = commands[i] if i==0 then result[#result+1] = "\\\n" i = 5 @@ -28,64 +60,139 @@ function flushers.scite(interface,collection) end result[#result+1] = format("%s ",command) end - io.savedata(format("cont-%s-scite.properties",interface), table.concat(result),"\n") - io.savedata(format("cont-%s-scite.lua",interface), table.serialize(collection,true)) + io.savedata(format("cont-%s-scite.properties",interface), concat(result),"\n") + io.savedata(format("cont-%s-scite.lua",interface), table.serialize(commands,true)) end -function flushers.jedit(interface,collection) +function flushers.jedit(interface,commands) local result = {} result[#result+1] = "<?xml version='1.0'?>" result[#result+1] = "<!DOCTYPE MODE SYSTEM 'xmode.dtd'>\n" result[#result+1] = "<MODE>" result[#result+1] = "\t<RULES>" result[#result+1] = "\t\t<KEYWORDS>" - for i=1,#collection do - local command = collection[i] - result[#result+1] = format("\t\t\t<KEYWORD2>%s</KEYWORD2>",command) + for i=1,#commands do + result[#result+1] = format("\t\t\t<KEYWORD2>%s</KEYWORD2>",commands[i]) end result[#result+1] = "\t\t</KEYWORDS>" result[#result+1] = "\t</RULES>" result[#result+1] = "</MODE>" - io.savedata(format("context-jedit-%s.xml",interface), table.concat(result),"\n") + io.savedata(format("context-jedit-%s.xml",interface), concat(result),"\n") end -function flushers.bbedit(interface,collection) +function flushers.bbedit(interface,commands) local result = {} result[#result+1] = "<?xml version='1.0'?>" result[#result+1] = "<key>BBLMKeywordList</key>" result[#result+1] = "<array>" - for i=1,#collection do - local command = collection[i] - result[#result+1] = format("\t<string>\\%s</string>",command) + for i=1,#commands do + result[#result+1] = format("\t<string>\\%s</string>",commands[i]) end result[#result+1] = "</array>" - io.savedata(format("context-bbedit-%s.xml",interface), table.concat(result),"\n") + io.savedata(format("context-bbedit-%s.xml",interface), concat(result),"\n") +end + +function flushers.raw(interface,commands) + for i=1,#commands do + report(commands[i]) + end +end + +local textpadcreator = "mtx-interface-textpad.lua" + +function flushers.text(interface,commands,environments) + local c, cname = { }, format("context-commands-%s.txt",interface) + local e, ename = { }, format("context-environments-%s.txt",interface) + report("saving '%s'",cname) + for i=1,#commands do + c[#c+1] = format("\\%s",commands[i]) + end + io.savedata(cname,concat(c,"\n")) + report("saving '%s'",ename) + for i=1,#environments do + e[#e+1] = format("\\start%s",environments[i]) + e[#e+1] = format("\\stop%s", environments[i]) + end + io.savedata(format("context-environments-%s.txt",interface),concat(e,"\n")) end -function flushers.raw(interface,collection) - for i=1,#collection do - local command = collection[i] - logs.simple(command) +function flushers.textpad(interface,commands,environments) + flushers.text(interface,commands,environments) + -- + -- plugin, this is a rewrite of a file provided by Lukas Prochazka + -- + local function merge(templatedata,destinationdata,categories) + report("loading '%s'",templatedata) + local data = io.loaddata(templatedata) + local done = 0 + for i=1,#categories do + local category = categories[i] + local cpattern = ";%s*category:%s*(" .. category .. ")%s*[\n\r]+" + local fpattern = ";%s*filename:%s*(" .. "%S+" .. ")%s*[\n\r]+" + data = gsub(data,cpattern..fpattern,function(category,filename) + local found = resolvers.findfile(filename) or "" + local blob = found ~= "" and io.loaddata(found) or "" + if blob == "" then + report("category: %s, filename: %s, not found",category,filename) + else + done = done + 1 + report("category: %s, filename: %s, merged",category,filename) + end + return format("; category: %s\n; filename: %s\n%s\n\n",category,filename,blob) + end) + end + if done > 0 then + report("saving '%s' (%s files merged)",destinationdata,done) + io.savedata(destinationdata,data) + else + report("skipping '%s' (no files merged)",destinationdata) + end + end + local templatename = "textpad-context-template.txt" + local templatedata = resolvers.findfile(templatename) or "" + if templatedata == "" then + report("unable to locate template '%s'",templatename) + else + merge(templatedata, "context.syn", { "tex commands","context commands" }) + if environment.argument("textpad") == "latex" then + merge(templatedata, "context-latex.syn", { "tex commands","context commands", "latex commands" }) + end end + local r = { } + local c = io.loaddata("context-commands-en.txt") or "" -- sits on the same path + local e = io.loaddata("context-environments-en.txt") or "" -- sits on the same path + for s in gmatch(c,"\\(.-)%s") do + r[#r+1] = format("\n!TEXT=%s\n\\%s\n!",s,s) + end + for s in gmatch(e,"\\start(.-)%s+\\stop(.-)") do + r[#r+1] = format("\n!TEXT=%s (start/stop)\n\\start%s \\^\\stop%s\n!",s,s,s) + end + sort(r) + insert(r,1,"!TCL=597,\n!TITLE=ConTeXt\n!SORT=N\n!CHARSET=DEFAULT") + io.savedata("context.tcl",concat(r,"\n")) + -- cleanup + os.remove("context-commands-en.txt") + os.remove("context-environments-en.txt") end -function scripts.interface.editor(editor) - local interfaces= environment.files +function scripts.interface.editor(editor,split,forcedinterfaces) + local interfaces= forcedinterfaces or environment.files if #interfaces == 0 then interfaces= userinterfaces end - local xmlfile = resolvers.find_file("cont-en.xml") or "" + local xmlfile = resolvers.findfile("cont-en.xml") or "" if xmlfile == "" then - logs.simple("unable to locate cont-en.xml") + report("unable to locate cont-en.xml") end for i=1,#interfaces do local interface = interfaces[i] - local keyfile = resolvers.find_file(format("keys-%s.xml",interface)) or "" + local keyfile = resolvers.findfile(format("keys-%s.xml",interface)) or "" if keyfile == "" then - logs.simple("unable to locate keys-*.xml") + report("unable to locate keys-*.xml") else - local collection = { } - local mappings = { } + local commands = { } + local mappings = { } + local environments = { } local x = xml.load(keyfile) for e, d, k in xml.elements(x,"cd:command") do local at = d[k].at @@ -101,23 +208,28 @@ function scripts.interface.editor(editor) if name and name ~= "" then local remapped = mappings[name] or name if type == "environment" then - collection[#collection+1] = "start" .. remapped - collection[#collection+1] = "stop" .. remapped + if split then + environments[#environments+1] = remapped + else + commands[#commands+1] = "start" .. remapped + commands[#commands+1] = "stop" .. remapped + end else - collection[#collection+1] = remapped + commands[#commands+1] = remapped end end end - if #collection > 0 then - table.sort(collection) - flushers[editor](interface,collection) + if #commands > 0 then + sort(commands) + sort(environments) + flushers[editor](interface,commands,environments) end end end end function scripts.interface.check() - local xmlfile = resolvers.find_file("cont-en.xml") or "" + local xmlfile = resolvers.findfile("cont-en.xml") or "" if xmlfile ~= "" then local f = io.open("cont-en-check.tex","w") if f then @@ -142,13 +254,13 @@ function scripts.interface.check() end end -function scripts.interface.context() - local filename = resolvers.find_file(environment.files[1] or "mult-def.lua") or "" +function scripts.interface.interfaces() + local filename = resolvers.findfile(environment.files[1] or "mult-def.lua") or "" if filename ~= "" then local interface = dofile(filename) if interface and next(interface) then local variables, constants, commands, elements = interface.variables, interface.constants, interface.commands, interface.elements - local filename = resolvers.find_file("cont-en.xml") or "" + local filename = resolvers.findfile("cont-en.xml") or "" local xmldata = filename ~= "" and (io.loaddata(filename) or "") local function flush(texresult,xmlresult,language,what,tag) local t = interface[what] @@ -161,7 +273,7 @@ function scripts.interface.context() local v = t[key] local value = v[language] or v["en"] if not value then - logs.simple(format("warning, no value for key '%s' for language '%s'",key,language)) + report("warning, no value for key '%s' for language '%s'",key,language) else local value = t[key][language] or t[key].en texresult[#texresult+1] = format("\\setinterface%s{%s}{%s}",tag,key,value) @@ -196,12 +308,12 @@ function scripts.interface.context() flush(texresult,xmlresult,language,"commands", "command") texresult[#texresult+1] = format("%%\n\\endinput") xmlresult[#xmlresult+1] = format("</cd:interface>") - local texfilename = format("mult-%s.tex",language) + local texfilename = format("mult-%s.mkii",language) local xmlfilename = format("keys-%s.xml",language) - io.savedata(texfilename,table.concat(texresult,"\n")) - logs.simple(format("saving interface definitions '%s'",texfilename)) - io.savedata(xmlfilename,table.concat(xmlresult,"\n")) - logs.simple(format("saving interface translations '%s'",xmlfilename)) + io.savedata(texfilename,concat(texresult,"\n")) + report("saving interface definitions '%s'",texfilename) + io.savedata(xmlfilename,concat(xmlresult,"\n")) + report("saving interface translations '%s'",xmlfilename) if language ~= "en" and xmldata ~= "" then local newdata = xmldata:gsub("(<cd:interface.*language=.)en(.)","%1"..language.."%2",1) newdata = replace(newdata, 'cd:string', 'value', interface.commands, interface.elements, language) @@ -212,63 +324,208 @@ function scripts.interface.context() newdata = replace(newdata, 'cd:inherit', 'name', interface.commands, interface.elements, language) local xmlfilename = format("cont-%s.xml",language) io.savedata(xmlfilename,newdata) - logs.simple(format("saving interface specification '%s'",xmlfilename)) + report("saving interface specification '%s'",xmlfilename) end end end end end -function scripts.interface.messages() - local filename = resolvers.find_file(environment.files[1] or "mult-mes.lua") or "" - if filename ~= "" then - local messages = dofile(filename) - for i=1,#messageinterfaces do - local interface = messageinterfaces[i] - local texresult = { } - for category, data in next, messages do - for tag, message in next, data do - if tag ~= "files" then - local msg = message[interface] or message["all"] or message["en"] - if msg then - texresult[#texresult+1] = format("\\setinterfacemessage{%s}{%s}{%s}",category,tag,msg) - end - end - end +function scripts.interface.preprocess() + require("luat-mac.lua") + local newsuffix = environment.argument("suffix") or "log" + local force = environment.argument("force") + for i=1,#environment.files do + local oldname = environment.files[i] + local newname = file.replacesuffix(oldname,newsuffix) + if oldname == newname then + report("skipping '%s' because old and new name are the same",oldname) + elseif io.exists(newname) and not force then + report("skipping '%s' because new file exists, use --force",oldname) + else + report("processing '%s' into '%s'",oldname,newname) + io.savedata(newname,resolvers.macros.preprocessed(io.loaddata(oldname))) + end + end +end + +-- function scripts.interface.messages() +-- local filename = resolvers.findfile(environment.files[1] or "mult-mes.lua") or "" +-- if filename ~= "" then +-- local messages = dofile(filename) +-- report("messages for * loaded from '%s'",filename) +-- report() +-- for i=1,#messageinterfaces do +-- local interface = messageinterfaces[i] +-- local texresult = { } +-- for category, data in next, messages do +-- for tag, message in next, data do +-- if tag ~= "files" then +-- local msg = message[interface] or message["all"] or message["en"] +-- if msg then +-- texresult[#texresult+1] = format("\\setinterfacemessage{%s}{%s}{%s}",category,tag,msg) +-- end +-- end +-- end +-- end +-- texresult[#texresult+1] = format("%%\n\\endinput") +-- local interfacefile = format("mult-m%s.mkii",interface) +-- io.savedata(interfacefile,concat(texresult,"\n")) +-- report("messages for '%s' saved in '%s'",interface,interfacefile) +-- end +-- end +-- end + +function scripts.interface.toutf() + local filename = environment.files[1] + if filename then + require("char-def.lua") + local contextnames = { } + for unicode, data in next, characters.data do + local contextname = data.contextname + if contextname then + contextnames[contextname] = utf.char(unicode) end - texresult[#texresult+1] = format("%%\n\\endinput") - io.savedata(format("mult-m%s.tex",interface),table.concat(texresult,"\n")) + contextnames.uumlaut = contextnames.udiaeresis + contextnames.Uumlaut = contextnames.Udiaeresis + contextnames.oumlaut = contextnames.odiaeresis + contextnames.Oumlaut = contextnames.Odiaeresis + contextnames.aumlaut = contextnames.adiaeresis + contextnames.Aumlaut = contextnames.Adiaeresis end + report("loading '%s'",filename) + local str = io.loaddata(filename) or "" + local done = { } + str = gsub(str,"(\\)([a-zA-Z][a-zA-Z][a-zA-Z]+)(%s*)", function(b,s,a) + local cn = contextnames[s] + if cn then + done[s] = (done[s] or 0) + 1 + return cn + else + done[s] = (done[s] or 0) - 1 + return b .. s .. a + end + end) + for k, v in table.sortedpairs(done) do + if v > 0 then + report("+ %5i : %s => %s",v,k,contextnames[k]) + else + report("- %5i : %s",-v,k,contextnames[k]) + end + end + filename = filename .. ".toutf" + report("saving '%s'",filename) + io.savedata(filename,str) end end -logs.extendbanner("ConTeXt Interface Related Goodies 0.11",true) +-- function scripts.interface.labels() +-- require("char-def.lua") +-- require("lang-txt.lua") +-- local interfaces = require("mult-def.lua") +-- local variables = interfaces.variables +-- local contextnames = { } +-- for unicode, data in next, characters.data do +-- local contextname = data.contextname +-- if contextname then +-- contextnames[utfchar(unicode)] = "\\" .. contextname .. " " +-- end +-- end +-- contextnames["i"] = nil +-- contextnames["'"] = nil +-- contextnames["\\"] = nil +-- local function flush(f,kind,what,expand,namespace,prefix) +-- local whatdata = languages.data.labels[what] +-- f:write("\n") +-- f:write(format("%% %s => %s\n",what,kind)) +-- for tag, data in table.sortedpairs(whatdata) do +-- if not data.hidden then +-- f:write("\n") +-- for language, text in table.sortedpairs(data.labels) do +-- if text ~= "" then +-- if expand then +-- text = utfgsub(text,".",contextnames) +-- text = gsub(text," ", "\ ") +-- end +-- if namespace and namespace[tag] then +-- tag = prefix .. tag +-- end +-- if find(text,",") then +-- text = "{" .. text .. "}" +-- end +-- if text == "" then +-- -- skip +-- else +-- if type(text) == "table" then +-- f:write(format("\\setup%stext[\\s!%s][%s={{%s},}]\n",kind,language,tag,text)) +-- else +-- f:write(format("\\setup%stext[\\s!%s][%s={{%s},{%s}}]\n",kind,language,tag,text[1],text[2])) +-- end +-- end +-- end +-- end +-- end +-- end +-- end +-- function flushall(txtname,expand) +-- local f = io.open(txtname,"w") +-- if f then +-- report("saving '%s'",txtname) +-- f:write("% this file is auto-generated, don't edit this file\n") +-- flush(f,"head","titles",expand,variables,"\\v!") +-- flush(f,"label","texts",expand,variables,"\\v!") +-- flush(f,"mathlabel","functions",expand) +-- flush(f,"taglabel","tags",expand) +-- f:write("\n") +-- f:write("\\endinput\n") +-- f:close() +-- end +-- end +-- flushall("lang-txt.mkii",true) +-- flushall("lang-txt.mkiv",false) +-- end -messages.help = [[ ---scite generate scite interface ---bbedit generate scite interface ---jedit generate scite interface ---check generate check file ---context generate context definition files ---messages generate context message files -]] +local ea = environment.argument -if environment.argument("context") then - scripts.interface.context() -elseif environment.argument("messages") then - scripts.interface.messages() -elseif environment.argument("scite") or environment.argument("bbedit") or environment.argument("jedit") then - if environment.argument("scite") then +if ea("context") then + scripts.interface.interfaces() + -- scripts.interface.messages() + -- scripts.interface.labels() +elseif ea("interfaces") or ea("messages") or ea("labels") then + if ea("interfaces") then + scripts.interface.interfaces() + end + -- if ea("messages") then + -- scripts.interface.messages() + -- end + -- if ea("labels") then + -- scripts.interface.labels() + -- end +elseif ea("preprocess") then + scripts.interface.preprocess() +elseif ea("toutf") then + scripts.interface.toutf() +elseif ea("check") then + scripts.interface.check() +elseif ea("scite") or ea("bbedit") or ea("jedit") or ea("textpad") or ea("text") or ea("raw") then + if ea("scite") then scripts.interface.editor("scite") end - if environment.argument("bbedit") then + if ea("bbedit") then scripts.interface.editor("bbedit") end - if environment.argument("jedit") then + if ea("jedit") then scripts.interface.editor("jedit") end -elseif environment.argument("check") then - scripts.interface.check() + if ea("textpad") then + scripts.interface.editor("textpad",true, { "en" }) + end + if ea("text") then + scripts.interface.editor("text") + end + if ea("raw") then + scripts.interface.editor("raw") + end else - logs.help(messages.help) + application.help() end |