Module:Escape/doc
This is a documentation subpage for Module:Escape. It contains usage information, categories and other content that is not part of the original Module page. |
This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
This Lua module is used on many pages. To avoid large-scale disruption and unnecessary server load, any changes to this module should first be tested in its /sandbox or /testcases subpages. The tested changes can then be added to this page in one single edit. Please consider discussing any changes on the talk page before implementing them. |
Usage
This module is designed as an way to escape strings in a customized and efficient manner. It works by replacing characters that are preceded by your escape char (or phrase) There are two ways to call this module:
From another module:
local esc = require('Module:Escape') esc:char(escape char (or sequence)) local to_escape = esc:text(string) code that replaces or removes unescaped chars local result = esc:undo(to_escape)
From a template:
{{invoke:Escape|main|mode=function|char=escape char (or sequence)|text}}
In a template, the most useful function is
kill
.
This module is primarily intended to be used by other modules. However all functions can be called in template space using |mode=the function you want to call
followed by arguments.
All module functions (i.e. any func. other than main()) should be called using a colon (:), e.g.
esc:char('%')
or esc:kill{'{{example|\\}}}', '}'} == '{{example|}'
|
This function takes only one argument: A string. All characters in this string which are preceded by the sequence set by escape:char() will be replaced with placeholders that can be converted back into that char by escape:undo() |
---|---|
|
Takes two arguments:
|
|
This is basically equivalent to calling string.gsub() on the string returned by escape:text() and feeding that result into escape:undo() in a single step. Takes three arguments:
|
|
This function's primary use is to initialize the patterns to scan a string for an escape/escaped sequence. It takes two arguments, the first being the escape character and the second being a table of arguments (optional). By default, this module will escape the \
{
require('Module:Escape'):char('{')
esc:char('{')
esc
When called without the second argument, char() will return a table containing the functions. This allows, for example, For the most part, there is very little reason to set ShortcutIf provided a second argument that is a table containing a {key = value} pair, such that the key istext
undo
kill
escape:char('\\', {text = 'string'})
escape:char('\\'):text('string')
kill
text
undo
undo
text
|
Caveats
- When using a multi-character escape sequence, this module only marks it using the byte value of the first character. Thus, escape:undo() will unescape, for example, all characters escaped with 'e' and 'esc' if both were used. In practice however this shouldn't be a problem as multiple escape sequences are pretty rare unless you're transitioning between multiple code languages. (Multiple multi-char escape sequences beginning with the same character are simply bad practice anyhow.)
- Since byte values are stored as numbers, it is not recommended for you to use a number as an escape sequence (though it may work just fine).
- Placeholder byte values separated with return ('\r') characters--chosen because they are seldom used at all, and virtually never used unpaired with '\n'; moreover, it is distinct from the markers generated by
<nowiki>...</nowiki>
or(which use the delete char). To set a different separator char, include the key-value pairmw.text.nowiki()
{safeChr = alternate character}
in the table that you pass to escape:char().
Speed
The following are benchmarks...
when executing the following module function:
function p.test_kill500(frame)
local esc = require('Module:Escape')
for k = 1, 500 do
local v = esc:kill(p.test_string2(), 'test')
end
return os.clock(esc)
end
0.08486
when repeating the following line 500 times in a template:
{{#invoke:Escape|main|mode=kill|{{#invoke:Escape/testcases|test_string2}}|test}}
0.767
All times in seconds. The module time x500 was calculated when you loaded this doc page (normally between 0.02 and 0.07). The template time x500 was recorded on Jan 15, 2015.
Examples
Template
Original:
test { test {\{ test, \test, \{,test\ \ \ {\
Using internal method to remove {
:
{
{{#invoke:Escape|main|mode=kill|test { test {\{ test, \test, \{,test\ \ \ {\|{}}
test test { test, test, {,test \
Using Treplace to remove {
:
{
{{#invoke:Escape|main|mode=undo|{{replace|{{#invoke:Escape|main|mode=text|test { test {\{ test, \test, \{,test\ \ \ {\}}|{|}}}}
test test { test, test, {,test \
No removal of {
between escape/unescape (escape char not restored):
{
{{#invoke:Escape|main|mode=undo
|{{#invoke:Escape|main|mode=text|test { test {\{ test, \test, \{,test\ \ \ {\}}
}}
test { test {{ test, test, {,test {\
Restore to original after escape
{{#invoke:Escape|main|mode=undo
|{{#invoke:Escape|main|mode=text|test { test {\{ test, \test, \{,test\ \ \ {\}}
|\
}}
test { test {\{ test, \test, \{,test\ \ \ {\
Remove the word test
if not escaped and then place a different escape char in the place of the old escape char (for use by something else):
test
Note: The '%' char is a special in Lua, so use '%%' if that is the desired replacement. Otherwise, just a single char is fine (or a word).
{{#invoke:Escape|main|mode=kill
|test { test {\{ test, \test, \{,test\ \ \ {\
|test
|%%
}}
{ {%{ , %test, %{,% % % {\
Module
Here's some sample output from the debug console below the module editor:
local escape = require('Module:Escape') test3 = escape:char('\\'):text(test2) test4 = escape:char('{', {undo = test3}) test4 = escape:char('\\', {undo = test3}) test5 = escape:char('{', {undo = test4}) =escape:undo(test3)--doesn't work because char is still set to '{' in current session =escape:undo(test4) =escape:char('\\'):undo(test3) =escape:char('{', {undo = escape:char('\\'):undo(test3)}) =test == escape:char('{', {undo = escape:char('\\'):undo(test3)}) =test == escape:char('{', {undo = escape:char('\\'):undo(test3, '\\')}) local t = 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\' local e = require('Module:Escape') local tk0 = escape:kill(t, '{') |