Difference between revisions of "Module:Escape/doc"

From blackwiki
Jump to navigation Jump to search
blackwiki>Codehydro
(save)
blackwiki>Codehydro
Line 29: Line 29:
 
===Module Example===
 
===Module Example===
 
Here's some sample output from the debug consol below the module editor:
 
Here's some sample output from the debug consol below the module editor:
 
 
{|- class='wikitable'
 
{|- class='wikitable'
 
|
 
|
{{blue|'''local escape {{=}} require('Module:Escape')<br>test {{=}} 'test, \\test, \\{,test\\\\ \\\\ \\\\\\\\'<br><br>test2 {{=}} escape:char('{'):text(test)<br>{{=}}test2'''}}<br>
+
{{blue|'''local escape {{=}} require('Module:Escape')<br>test {{=}} '{{#invoke:Escape/testcases|test_string_module}}'<br><br>test2 {{=}} escape:char('{'):text(test)<br>{{=}}test2'''}}<br>
test, \test, \�7b�44�7btest\\ \\ \\\\
+
{{#invoke:Escape|main
 +
|mode=text
 +
|char={
 +
|{{#invoke:Escape/testcases|test_string}}
 +
}}
  
 
{{blue|'''test3 {{=}} escape:char('\\'):text(test2)<br><br>{{=}}test3'''}}<br>
 
{{blue|'''test3 {{=}} escape:char('\\'):text(test2)<br><br>{{=}}test3'''}}<br>
test, �5c�116�5cest, �5c�24�5c7b�44�7btest�5c�92�5c �5c�92�5c �5c�92�5c�5c�92�5c
+
{{#invoke:Escape|main
 +
|mode=text
 +
|{{#invoke:Escape|main
 +
|mode=text
 +
|char={
 +
|{{#invoke:Escape/testcases|test_string}}
 +
}}
 +
}}
  
 
{{blue|'''test4 {{=}} escape:char('{', {undo {{=}} test3})<br><br>{{=}}test4'''}}<br>
 
{{blue|'''test4 {{=}} escape:char('{', {undo {{=}} test3})<br><br>{{=}}test4'''}}<br>
test, �5c�116�5cest, �5c�24�5c7b�44�7btest�5c�92�5c �5c�92�5c �5c�92�5c�5c�92�5c
+
{{#invoke:Escape|main|mode=undo
 +
|char={
 +
|{{#invoke:Escape|main|mode=text
 +
|{{#invoke:Escape|main|mode=text
 +
  |char={
 +
  |{{#invoke:Escape/testcases|test_string}}
 +
  }}
 +
}}
 +
}}
  
 
{{blue|'''test4 {{=}} escape:char('\\', {undo {{=}} test3})<br><br>{{=}}test4'''}}<br>
 
{{blue|'''test4 {{=}} escape:char('\\', {undo {{=}} test3})<br><br>{{=}}test4'''}}<br>
test, \test, \�7b�44�7btest\\ \\ \\\\<br><br>
+
{{#invoke:Escape|main
 +
|mode=undo
 +
|{{#invoke:Escape|main
 +
|mode=text
 +
|{{#invoke:Escape|main
 +
  |mode=text
 +
  |char={
 +
  |{{#invoke:Escape/testcases|test_string}}}}
 +
}}
 +
}}
 +
 
 
{{blue|'''test5 {{=}} escape:char('{', {undo {{=}} test4})<br><br>{{=}}test5 {{=}}{{=}} test'''}}<br>
 
{{blue|'''test5 {{=}} escape:char('{', {undo {{=}} test4})<br><br>{{=}}test5 {{=}}{{=}} test'''}}<br>
true<br><br>
+
{{#ifeq:
'''{{blue|{{=}}escape:undo(test3)}}'''<br>
+
{{#invoke:Escape|main
test, �5c�116�5cest, �5c�24�5c7b�44�7btest�5c�92�5c �5c�92�5c �5c�92�5c�5c�92�5c
+
|mode=undo
 +
|char={
 +
|{{#invoke:Escape|main
 +
  |mode=undo
 +
  |{{#invoke:Escape|main
 +
  |mode=text
 +
  |{{#invoke:Escape|main
 +
    |mode=text
 +
    |char={
 +
    |{{#invoke:Escape/testcases|test_string}}
 +
    }}
 +
  }}|\
 +
  }}|{
 +
}}
 +
|{{#invoke:Escape/testcases|test_string}}
 +
|true
 +
|false
 +
}}
 +
 
 +
'''{{blue|{{=}}escape:undo(test3)}}'''{{green|--doesn't work because char is still set to '{' in current session}}<br>
 +
<!--Obviously template calls don't remember sessions so we must code it here-->
 +
{{#invoke:Escape|main|mode=undo
 +
|char={
 +
|{{#invoke:Escape|main|mode=text|{{#invoke:Escape|main|mode=text|char={|{{#invoke:Escape/testcases|test_string}}}}}}
 +
}}
  
 
'''{{blue|{{=}}escape:undo(test4)}}<br>
 
'''{{blue|{{=}}escape:undo(test4)}}<br>
test, \test, \,test\\ \\ \\\\
+
{{#invoke:Escape|main|mode=undo
 +
|char={
 +
|{{#invoke:Escape|main|mode=undo
 +
|{{#invoke:Escape|main|mode=text
 +
  |{{#invoke:Escape|main|mode=text
 +
  |char={
 +
  |{{#invoke:Escape/testcases|test_string}}
 +
  }}
 +
}}
 +
|\}}
 +
}}
  
 
'''{{blue|{{=}}escape:char('\\'):undo(test3)}}<br>
 
'''{{blue|{{=}}escape:char('\\'):undo(test3)}}<br>
test, test, �7b�44�7btest\ \ \\
+
{{#invoke:Escape|main
 +
|mode=undo
 +
|{{#invoke:Escape|main|mode=text|{{#invoke:Escape|main|mode=text|char={|{{#invoke:Escape/testcases|test_string}}}}}}
 +
}}
  
 
'''{{blue|{{=}}escape:char('{', {undo {{=}} escape:char('\\'):undo(test3)})}}<br>
 
'''{{blue|{{=}}escape:char('{', {undo {{=}} escape:char('\\'):undo(test3)})}}<br>
test, test, {,test\ \ \\
+
{{#invoke:Escape|main|mode=undo|char={
 +
|{{#invoke:Escape|main|mode=undo|{{#invoke:Escape|main|mode=text|{{#invoke:Escape|main|mode=text|char={|{{#invoke:Escape/testcases|test_string}}}}}}}}
 +
|{
 +
}}
  
 
'''{{blue|{{=}}test {{=}}{{=}} escape:char('{', {undo {{=}} escape:char('\\'):undo(test3)})}}<br>
 
'''{{blue|{{=}}test {{=}}{{=}} escape:char('{', {undo {{=}} escape:char('\\'):undo(test3)})}}<br>
Line 65: Line 133:
 
'''{{blue|local t {{=}} 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\'<br>{{=}}t}}<br>'''
 
'''{{blue|local t {{=}} 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\'<br>{{=}}t}}<br>'''
 
test { test {\{ test, \test, \{,test\ \ \ {\
 
test { test {\{ test, \test, \{,test\ \ \ {\
 +
 +
{{#invoke:Escape/testcases|test_string_module|2}}
  
 
'''{{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:29, 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')
test = 'test, \\test, \\{,test\\\\ \\\\ \\\\\\\\'

test2 = escape:char('{'):text(test)
=test2

test, \test, \7b 044 7btest\\ \\ \\\\

test3 = escape:char('\\'):text(test2)

=test3

test, 5c 0116 5cest, 5c 055 5cb 044 7btest5c 092 5c 5c 092 5c 5c 092 5c5c 092 5c

test4 = escape:char('{', {undo = test3})

=test4

test, 5c 0116 5cest, 5c 055 5cb 044 7btest5c 092 5c 5c 092 5c 5c 092 5c5c 092 5c

test4 = escape:char('\\', {undo = test3})

=test4

test, test, 7b 044 7btest\ \ \\

test5 = escape:char('{', {undo = test4})

=test5 == test

true

=escape:undo(test3)--doesn't work because char is still set to '{' in current session
test, 5c 0116 5cest, 5c 055 5cb 044 7btest5c 092 5c 5c 092 5c 5c 092 5c5c 092 5c

=escape:undo(test4)
test, \test, \,test\\ \\ \\\\

=escape:char('\\'):undo(test3)
test, test, 7b 044 7btest\ \ \\

=escape:char('{', {undo = escape:char('\\'):undo(test3)})
test, test, {,test\ \ \\

=test == escape:char('{', {undo = escape:char('\\'):undo(test3)})
false

=test == escape:char('{', {undo = escape:char('\\'):undo(test3, '\\')})
true

local t = 'test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\'
=t

test { test {\{ test, \test, \{,test\ \ \ {\

test { test {\\{ test, \\test, \\{,test\\ \\ \\ {\\

local e = require('Module:Escape')
local t2 = escape:text(t)
local t3 = string.gsub(t2, '{', )
local t4 = escape:undo(t3)
=t4

test test { test, test, {,test \

local tk0 = escape:kill(t, '{')
=tk0 == t4

true