Module:Escape/doc: Difference between revisions
Line 38: | Line 38: | ||
}} |
}} |
||
{{blue|'''test3 {{=}} escape:char('\\'):text(test2) |
{{blue|'''test3 {{=}} escape:char('\\'):text(test2)<br>{{=}}test3'''}}<br> |
||
{{#invoke:Escape|main |
{{#invoke:Escape|main |
||
|mode=text |
|mode=text |
||
Line 48: | Line 48: | ||
}} |
}} |
||
{{blue|'''test4 {{=}} escape:char('{', {undo {{=}} test3}) |
{{blue|'''test4 {{=}} escape:char('{', {undo {{=}} test3})<br>{{=}}test4'''}}<br> |
||
{{#invoke:Escape|main|mode=undo |
{{#invoke:Escape|main|mode=undo |
||
|char={ |
|char={ |
||
Line 59: | Line 59: | ||
}} |
}} |
||
{{blue|'''test4 {{=}} escape:char('\\', {undo {{=}} test3}) |
{{blue|'''test4 {{=}} escape:char('\\', {undo {{=}} test3})<br>{{=}}test4'''}}<br> |
||
{{#invoke:Escape|main |
{{#invoke:Escape|main |
||
|mode=undo |
|mode=undo |
||
Line 71: | Line 71: | ||
}} |
}} |
||
{{blue|'''test5 {{=}} escape:char('{', {undo {{=}} test4}) |
{{blue|'''test5 {{=}} escape:char('{', {undo {{=}} test4})<br>{{=}}test5 {{=}}{{=}} test'''}}<br> |
||
{{#ifeq: |
{{#ifeq: |
||
{{#invoke:Escape|main |
{{#invoke:Escape|main |
||
Line 129: | Line 129: | ||
'''{{blue|{{=}}test {{=}}{{=}} escape:char('{', {undo {{=}} escape:char('\\'):undo(test3, '\\')})}}<br> |
'''{{blue|{{=}}test {{=}}{{=}} escape:char('{', {undo {{=}} escape:char('\\'):undo(test3, '\\')})}}<br> |
||
{{#ifeq: |
|||
⚫ | |||
{{#invoke:Escape|main |
{{#invoke:Escape|main |
||
|mode=char|char= |
|mode=char |
||
|char={ |
|||
|undo={{#invoke:Escape|main|mode=text|{{#invoke:Escape|main|mode=text|char={|{{#invoke:Escape/testcases|test_string}}}}}} |
|undo={{#invoke:Escape|main |
||
|mode=char |
|||
|char=\ |
|||
|undo={{#invoke:Escape|main|mode=text|{{#invoke:Escape|main|mode=text|char={|{{#invoke:Escape/testcases|test_string}}}}}} |
|||
}} |
|||
}} |
|||
⚫ | |||
⚫ | |||
|false |
|||
}} |
}} |
||
'''{{blue|local t {{=}} ' |
'''{{blue|local t {{=}} '{{#invoke:Escape/testcases|test_string_module|2}}'<br>{{=}}t}}<br>''' |
||
{{#invoke:Escape/testcases|test_string2}} |
|||
test { test {\{ test, \test, \{,test\ \ \ {\ |
|||
⚫ | |||
'''{{blue|local e {{=}} require('Module:Escape')<br>local t2 {{=}} escape:text(t)<br>local t3 {{=}} string.gsub(t2, '{', '')<br>local t4 {{=}} escape:undo(t3)<br>{{=}}t4}}<br>''' |
'''{{blue|local e {{=}} require('Module:Escape')<br>local t2 {{=}} escape:text(t)<br>local t3 {{=}} string.gsub(t2, '{', '')<br>local t4 {{=}} escape:undo(t3)<br>{{=}}t4}}<br>''' |
Revision as of 19:56, 14 January 2015
Usage
This module is designed as an way to escape strings in a customized manner. There are two ways to call this module:
From another module:
local escape = require('Module:Escape')
From a template:
{{invoke:Escape|main|mode=|char=}}
By default, this module will escape the \
char. To escape the {
char instead, you can do require('Module:Escape'):char('{')
(or esc:char('{')
if you've stored table returned by this module in the local variable esc
). When used in a template, set |char=
equal to the char code.
Template Example
While other modes are available, only |mode=kill
is probably the one most useful outside of Module space. The first parameter is your text with escaped chars. The second parameter is a char or string or pattern you wish to remove.
{{#invoke:Escape|main |mode=kill |test { test {\{ test, \test, \{,test\ \\ \ {\ , |{ }}
test test { test, test, {,test \ ,
Module Example
Here's some sample output from the debug consol 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, '{') |
Template Testcases
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 {{replace}} 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):
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, %{,% % % {\