Module:Escape
Appearance
escape = {
char = function(self, chr, args)
args = args or {}
local safe = args.safeChr or string.char(13)
chr = tostring(chr or '\\')
self[1] = ('%s0%%s%s'):format(
('%x%s%s'):format(chr:byte(), safe, safe),
('%s%x'):format(safe, chr:byte())
)
if not self[self[1]] then
self[self[1]] = {
char = chr,
text = ('%s(.)'):format(chr),
undo = self[1]:format'(%d+)'
}
end
return args.text and self:text(args.text)
or args.undo and self:undo(args.undo, chr)
or args.kill and self:kill(args.kill)
or self
end,
exec = function(self, text, mode, newEscape)
local target, v = self[self[1]]
repeat v = text:match(target[mode])
text = v and
text:gsub(
mode == 'text' and
('%s%s'):format(target.char, v)
or self[1]:format(v),
mode == 'text' and
self[1]:format(v:byte())
or (newEscape or '') .. v:char()
)
or text
until not v
return text
end,
text = function(self, text)
return self:exec(type(text) == 'table' and text[1] or text, 'text')
end,
undo = function(self, text, newEscape)
if type(text) == 'table' then
text, newEscape = text[1], text[2] or newEscape
end
return self:exec(text, 'undo', newEscape)
end,
kill = function(self, text, chars, newEscape)
if type(text) == 'table' then
text, chars, newEscape = text[1], text[2] or chars, text[3] or newEscape
end
return self:undo(self:text(text):gsub(chars, ''), newEscape)
end
}
function escape.main(frame)
local args = require('Module:Arguments').getArgs(frame)
if args.mode == 'char' then
return escape:char(args.char or args[2], args)
end
return escape[args.mode](escape:char(args.char), args)
end
return escape