Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/DeleteComments1.lua.html -- http://anggtwu.net/LUA/DeleteComments1.lua -- (find-angg "LUA/DeleteComments1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e () (interactive) (find-angg "LUA/DeleteComments1.lua")) -- Based on: (find-dn6 "output.lua" "Deletecomments-class") -- «.DeleteComments» (to "DeleteComments") -- ____ _ _ ____ _ -- | _ \ ___| | ___| |_ ___ / ___|___ _ __ ___ _ __ ___ ___ _ __ | |_ ___ -- | | | |/ _ \ |/ _ \ __/ _ \ | / _ \| '_ ` _ \| '_ ` _ \ / _ \ '_ \| __/ __| -- | |_| | __/ | __/ || __/ |__| (_) | | | | | | | | | | | __/ | | | |_\__ \ -- |____/ \___|_|\___|\__\___|\____\___/|_| |_| |_|_| |_| |_|\___|_| |_|\__|___/ -- -- «DeleteComments» (to ".DeleteComments") -- This class implements a way to delete comments that TRIES to -- simulate what TeX does. The TeXBook explains in its pages 46-47 -- that the "eyes" and "mouth" of TeX enter the "State S" (for -- "skipping blanks") after a "%"; we simulate that by splitting -- bigstr in a certain way, and then after each "%" we delete the -- "%..." part of that line plus the whitespaces and newlines in -- (hopefully) the right places. -- -- This version doesn't treat backslashes followed by "%"s in the -- right way. This is still to be done. -- -- See: (find-es "tex" "comments") -- (find-es "tex" "comments" "Skipping blanks") -- DeleteComments = Class { type = "DeleteComments", split = function (bigstr) local A = {} for _,li in ipairs(splitlines(bigstr)) do local a,b = li:match("^([^%%]*)(.*)") table.insert(A, {a, b, "\n"}) end return DeleteComments(A) end, from = function (bigstr) return DeleteComments.split(bigstr):delcomments():concat() end, __tostring = function (dc) return mytabletostring(dc) end, __index = { hascomment = function (dc, k) return dc[k][2] ~= "" end, endswithcmd = function (dc, k) return dc[k][1]:reverse():match("^[A-Za-z]+\\") end, addspaceaftercmd = function (dc, k) dc[k][1] = dc[k][1].." " end, valid = function (dc, k) return 1 <= k and k <= #dc end, ltrim = function (dc, k) dc[k][1] = dc[k][1]:match("^[ \t]*(.*)") end, delcomment = function (dc, k) if dc:endswithcmd(k) then dc:addspaceaftercmd(k) end dc[k][2] = "" -- delete the "%..." dc[k][3] = "" -- delete the newline if dc:valid(k+1) then dc:ltrim(k+1) end end, delcomments = function (dc) for k=1,#dc do if dc:hascomment(k) then dc:delcomment(k) end end return dc end, concat = function (dc) local bigstr = "" for k=1,#dc-1 do bigstr = bigstr..dc[k][1]..dc[k][2]..dc[k][3] end if #dc > 0 then bigstr = bigstr..dc[#dc][1]..dc[#dc][2] end return bigstr end, }, } -- «DeleteComments-tests» (to ".DeleteComments-tests") -- (find-es "dednat" "deletecomments-2021") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "DeleteComments1.lua" bigstr = [[ foo%12 bar\plic%34 qoo%56 blep bletch % woo ]] dc = DeleteComments.split(bigstr) = dc = dc:delcomments() = dc:concat() --]==] -- Local Variables: -- coding: utf-8-unix -- End: