From fa5d18c2f98ae3bca364f3c19804bd42f8305512 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sun, 8 May 2016 22:44:34 +0000 Subject: context git-svn-id: svn://tug.org/texlive/trunk@40962 c570f23f-e606-0410-a88d-b1316a301751 --- .../texmf-dist/tex/context/modules/mkiv/x-ldx.lua | 89 +++++++++++++--------- 1 file changed, 51 insertions(+), 38 deletions(-) (limited to 'Master/texmf-dist/tex/context/modules/mkiv/x-ldx.lua') diff --git a/Master/texmf-dist/tex/context/modules/mkiv/x-ldx.lua b/Master/texmf-dist/tex/context/modules/mkiv/x-ldx.lua index 31cbebf1392..e88c4c79772 100644 --- a/Master/texmf-dist/tex/context/modules/mkiv/x-ldx.lua +++ b/Master/texmf-dist/tex/context/modules/mkiv/x-ldx.lua @@ -19,13 +19,13 @@ with . I will rewrite this using lpeg. On the other hand, we cannot expect proper and for educational purposed the syntax might be wrong. + +Todo: use the scite parser. --ldx]]-- --- there is a nice parser on from http://lua-users.org/wiki/LpegRecipes (by --- Patrick Donnelly) but lua crashes when I apply functions to some of the --- matches +local banner = "version 1.0.1 - 2007+ - PRAGMA ADE / CONTEXT" -banner = "version 1.0.1 - 2007+ - PRAGMA ADE / CONTEXT" +local report = logs.reporter("x-ldx") --[[ This script needs a few libraries. Instead of merging the code here @@ -226,6 +226,8 @@ and by calculating the indentation we also avoid space troubles. It also makes it possible to change the indentation afterwards. --ldx]]-- +local newmethod = true + function ldx.as_xml(data) -- ldx: not needed local t, cmode = { }, false t[#t+1] = "\n" @@ -233,38 +235,44 @@ function ldx.as_xml(data) -- ldx: not needed for k=1,#data do local v = data[k] if v.code and not emptystring(v.code) then - t[#t+1] = "\n\n" - local split = splitstring(v.code,"\n") - for k=1,#split do -- make this faster - local v = split[k] - local a, b = find(v,"^(%s+)") - if v then v = gsub(v,"[\n\r ]+$","") end - if a and b then - v = sub(v,b+1,#v) - if cmode then - t[#t+1] = "" .. v .. "\n" - else - t[#t+1] = "" .. v .. "\n" - end - elseif emptystring(v) then - if cmode then - t[#t+1] = "\n" + if newmethod then + t[#t+1] = "\n\n" + else + t[#t+1] = "\n\n" + local split = splitstring(v.code,"\n") + for k=1,#split do -- make this faster + local v = split[k] + local a, b = find(v,"^(%s+)") + if v then v = gsub(v,"[\n\r ]+$","") end + if a and b then + v = sub(v,b+1,#v) + if cmode then + t[#t+1] = "" .. v .. "\n" + else + t[#t+1] = "" .. v .. "\n" + end + elseif emptystring(v) then + if cmode then + t[#t+1] = "\n" + else + t[#t+1] = "\n" + end + elseif find(v,"^%-%-%[%[") then + t[#t+1] = "" .. v .. "\n" + cmode= true + elseif find(v,"^%]%]%-%-") then + t[#t+1] = "" .. v .. "\n" + cmode= false + elseif cmode then + t[#t+1] = "" .. v .. "\n" else - t[#t+1] = "\n" + t[#t+1] = "" .. v .. "\n" end - elseif find(v,"^%-%-%[%[") then - t[#t+1] = "" .. v .. "\n" - cmode= true - elseif find(v,"^%]%]%-%-") then - t[#t+1] = "" .. v .. "\n" - cmode= false - elseif cmode then - t[#t+1] = "" .. v .. "\n" - else - t[#t+1] = "" .. v .. "\n" end + t[#t+1] = "\n" end - t[#t+1] = "\n" elseif v.comment then t[#t+1] = "\n\n" .. v.comment .. "\n\n" else @@ -288,20 +296,25 @@ 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" + if not lfs.isfile(luaname) then + file.addsuffix(luaname,"lua") end - if file.is_readable(luaname) then + if lfs.isfile(luaname) then if not ldxname then ldxname = file.replacesuffix(luaname,"ldx") end + report("converting file %a to %a",luaname,ldxname) local data = ldx.load(luaname) if data then - ldx.enhance(data) +-- ldx.enhance(data) if ldxname ~= luaname then ldx.save(ldxname,data) end + else + report("invalid file %a",luaname) end + else + report("unknown file %a",luaname) end end @@ -336,6 +349,6 @@ The main conversion call is: if environment.files and environment.files[1] then ldx.convert(environment.files[1],environment.files[2]) +else + report("no file given") end - ---~ exit(1) -- cgit v1.2.3