From bc20cf026cb0d2a175c116c5840d167f341d885e Mon Sep 17 00:00:00 2001 From: Marius Date: Sat, 26 Mar 2011 14:00:13 +0200 Subject: beta 2011.03.26 12:41 --- scripts/context/lua/x-ldx.lua | 326 ------------------------------------------ 1 file changed, 326 deletions(-) delete mode 100644 scripts/context/lua/x-ldx.lua (limited to 'scripts') diff --git a/scripts/context/lua/x-ldx.lua b/scripts/context/lua/x-ldx.lua deleted file mode 100644 index 18e7b9b38..000000000 --- a/scripts/context/lua/x-ldx.lua +++ /dev/null @@ -1,326 +0,0 @@ ---[[ldx-- -Lua Documentation Module - -This file is part of the documentation suite and -itself serves as an example of using in combination -with . - -I will rewrite this using lpeg once I have the time to study that nice new -subsystem. On the other hand, we cannot expect proper -ad for educational purposed the syntax migh be wrong. ---ldx]]-- - --- there is anice parser on from http://lua-users.org/wiki/LpegRecipes (by --- Patrick Donnelly) but lua crashes when I apply functions to some of the --- matches - -banner = "version 1.0.1 - 2007+ - PRAGMA ADE / CONTEXT" - ---[[ -This script needs a few libraries. Instead of merging the code here -we can use - - -mtxrun --internal x-ldx.lua - - -That way, the libraries included in the runner will be used. -]]-- - --- libraries l-string.lua l-table.lua l-io.lua l-file.lua - --- begin library merge --- end library merge - ---[[ -Just a demo comment line. We will handle such multiline comments but -only when they start and end at the beginning of a line. More rich -comments are tagged differently. -]]-- - ---[[ldx-- -First we define a proper namespace for this module. The l stands for -, the d for documentation and the x for -. ---ldx]]-- - -if not ldx then ldx = { } end - ---[[ldx-- -We load the lua file into a table. The entries in this table themselves are -tables and have keys like code and comment. ---ldx]]-- - -function ldx.load(filename) - local data = file.readdata(filename) - local expr = "%s*%-%-%[%[ldx%-*%s*(.-)%s*%-%-ldx%]%]%-*%s*" - local i, j, t = 0, 0, { } - while true do - local comment, ni - ni, j, comment = data:find(expr, j) - if not ni then break end - t[#t+1] = { code = data:sub(i, ni-1) } - t[#t+1] = { comment = comment } - i = j + 1 - end - local str = data:sub(i, #data) - str = str:gsub("^%s*(.-)%s*$", "%1") - if #str > 0 then - t[#t+1] = { code = str } - end - return t -end - ---[[ldx-- -We will tag keywords so that we can higlight them using a special font -or color. Users can extend this list when needed. ---ldx]]-- - -ldx.keywords = { } - ---[[ldx-- -Here come the reserved words: ---ldx]]-- - -ldx.keywords.reserved = { - ["and"] = 1, - ["break"] = 1, - ["do"] = 1, - ["else"] = 1, - ["elseif"] = 1, - ["end"] = 1, - ["false"] = 1, - ["for"] = 1, - ["function"] = 1, - ["if"] = 1, - ["in"] = 1, - ["local"] = 1, - ["nil"] = 1, - ["not"] = 1, - ["or"] = 1, - ["repeat"] = 1, - ["return"] = 1, - ["then"] = 1, - ["true"] = 1, - ["until"] = 1, - ["while"] = 1 -} - ---[[ldx-- -We need to escape a few tokens. We keep the hash local to the -definition but set it up only once, hence the do -construction. ---ldx]]-- - -do - local e = { [">"] = ">", ["<"] = "<", ["&"] = "&" } - function ldx.escape(str) - return (str:gsub("([><&])",e)) - end -end - ---[[ldx-- -Enhancing the code is a bit tricky due to the fact that we have to -deal with strings and escaped quotes within these strings. Before we -mess around with the code, we hide the strings, and after that we -insert them again. Single and double quoted strings are tagged so -that we can use a different font to highlight them. ---ldx]]-- - -ldx.make_index = true - -function ldx.enhance(data) -- i need to use lpeg and then we can properly autoindent -) - local e = ldx.escape - for k=1,#data do - local v = data[k] - if v.code then - local dqs, sqs, com, cmt, cod = { }, { }, { }, { }, e(v.code) - cod = cod:gsub('\\"', "##d##") - cod = cod:gsub("\\'", "##s##") - cod = cod:gsub("%-%-%[%[.-%]%]%-%-", function(s) - cmt[#cmt+1] = s - return ">>l>" - end) - cod = cod:gsub("%-%-([^\n]*)", function(s) - com[#com+1] = s - return ">>c>" - end) - cod = cod:gsub("(%b\"\")", function(s) - dqs[#dqs+1] = s:sub(2,-2) or "" - return ">>d>" - end) - cod = cod:gsub("(%b\'\')", function(s) - sqs[#sqs+1] = s:sub(2,-2) or "" - return ">>s>" - end) - cod = cod:gsub("(%a+)",function(key) - local class = ldx.keywords.reserved[key] - if class then - return "" .. key .. "" - else - return key - end - end) - cod = cod:gsub(">>s>", function(s) - return "" .. sqs[tonumber(s)] .. "" - end) - cod = cod:gsub(">>d>", function(s) - return "" .. dqs[tonumber(s)] .. "" - end) - cod = cod:gsub(">>c>", function(s) - return "" .. com[tonumber(s)] .. "" - end) - cod = cod:gsub(">>l>", function(s) - return cmt[tonumber(s)] - end) - cod = cod:gsub("##d##", "\\\"") - cod = cod:gsub("##s##", "\\\'") - if ldx.make_index then - local lines = cod:split("\n") - local f = "(function)%s+([%w%.]+)%s*%(" - for k=1,#lines do - local v = lines[k] - -- functies - v = v:gsub(f,function(key, str) - return "" .. str .. "(" - end) - -- variables - v = v:gsub("^([%w][%w%,%s]-)(=[^=])",function(str, rest) - local t = string.split(str, ",%s*") - for k=1,#t do - t[k] = "" .. t[k] .. "" - end - return table.concat(t,", ") .. rest - end) - -- so far - lines[k] = v - end - v.code = table.concat(lines,"\n") - else - v.code = cod - end - end - end -end - ---[[ldx-- -We're now ready to save the file in format. This boils -down to wrapping the code and comment as well as the whole document. We tag -lines in the code as such so that we don't need messy CDATA constructs -and by calculating the indentation we also avoid space troubles. It also makes -it possible to change the indentation afterwards. ---ldx]]-- - -function ldx.as_xml(data) -- ldx: not needed - local t, cmode = { }, false - t[#t+1] = "\n" - t[#t+1] = "\n\n" - for k=1,#data do - local v = data[k] - if v.code and not v.code:is_empty() then - t[#t+1] = "\n\n" - local split = v.code:split("\n") - for k=1,#split do -- make this faster - local v = split[k] - local a, b = v:find("^(%s+)") - if v then v = v:gsub("[\n\r ]+$","") end - if a and b then - v = v:sub(b+1,#v) - if cmode then - t[#t+1] = "" .. v .. "\n" - else - t[#t+1] = "" .. v .. "\n" - end - elseif v:is_empty() then - if cmode then - t[#t+1] = "\n" - else - t[#t+1] = "\n" - end - elseif v:find("^%-%-%[%[") then - t[#t+1] = "" .. v .. "\n" - cmode= true - elseif v:find("^%]%]%-%-") then - t[#t+1] = "" .. v .. "\n" - cmode= false - elseif cmode then - t[#t+1] = "" .. v .. "\n" - else - t[#t+1] = "" .. v .. "\n" - end - end - t[#t+1] = "\n" - elseif v.comment then - t[#t+1] = "\n\n" .. v.comment .. "\n\n" - else - -- cannot happen - end - end - t[#t+1] = "\n\n" - return table.concat(t,"") -end - ---[[ldx-- -Saving the result is a trivial effort. ---ldx]]-- - -function ldx.save(filename,data) - file.savedata(filename,ldx.as_xml(data)) -end - ---[[ldx-- -The next function wraps it all in one call: ---ldx]]-- - -function ldx.convert(luaname,ldxname) - if not file.is_readable(luaname) then - luaname = luaname .. ".lua" - end - if file.is_readable(luaname) then - if not ldxname then - ldxname = file.replacesuffix(luaname,"ldx") - end - local data = ldx.load(luaname) - if data then - ldx.enhance(data) - if ldxname ~= luaname then - ldx.save(ldxname,data) - end - end - end -end - ---[[ldx-- -This module can be used directly: - - -mtxrun --internal x-ldx somefile.lua - - -will produce an ldx file that can be processed with -by running: - - -texexec --use=x-ldx --forcexml somefile.ldx - - -You can do this in one step by saying: - - -texmfstart texexec --ctx=x-ldx somefile.lua - - -This will trigger into loading the mentioned - file. That file describes the conversion as well -as the module to be used. - -The main conversion call is: ---ldx]]-- - --- todo: assume usage of "mtxrun --script x-ldx", maybe make it mtx-ldx - -if arg and arg[1] then - ldx.convert(arg[1],arg[2]) -end - ---~ exit(1) -- cgit v1.2.3