Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Escape input

Permanently protected module
From Wikipedia, the free encyclopedia

--- This module allows special Lua character sequences to be escaped and inserted
-- as parameters in arguments.
--  
-- In addition to those listed on [[mw:Extension:Scribunto/Lua_reference_manual#string]],
-- the following is how replacements are conducted:
--
-- {| class="wikitable"
-- ! Escape sequence !! Character
-- |-
-- | <code><nowiki>\{</nowiki></code> || <code><nowiki>{</nowiki></code>
-- |-
-- | <code><nowiki>\}</nowiki></code> || <code><nowiki>}</nowiki></code>
-- |-
-- | <code><nowiki>\[</nowiki></code> || <code><nowiki>[</nowiki></code>
-- |-
-- | <code><nowiki>\]</nowiki></code> || <code><nowiki>]</nowiki></code>
-- |-
-- | <code><nowiki>\p</nowiki></code> || <code><nowiki>|</nowiki></code>
-- |-
-- | <code><nowiki>\e</nowiki></code> || <code><nowiki>=</nowiki></code>
-- |-
-- | <code><nowiki>\_</nowiki></code> || <code><nowiki> </nowiki></code>
-- |}
--
-- @module escape_input
-- @alias p

local p = {}

--- Conducts the necessary replacement of the matched pattern string
-- @param {string} s Matched sequence to replace
-- @return Escaped character
local function replace( s )
	-- necessary replacement to make these characters work in the format string
	if s == '\\a' then return '\a'
	elseif s == '\\b' then return '\b'
	elseif s == '\\t' then return '\t'
	elseif s == '\\n' then return '\n'
	elseif s == '\\v' then return '\v'
	elseif s == '\\f' then return '\f'
	elseif s == '\\r' then return '\r'
	elseif s == '\\"' then return '\"'
	elseif s == '\\\'' then return '\''
	elseif s == '\\\\' then return '\\'
	-- additional custom escape sequences specific for wikitext markup
	elseif s == '\\{' then return '{'
	elseif s == '\\}' then return '}'
	elseif s == '\\[' then return '['
	elseif s == '\\]' then return ']'
	elseif s == '\\p' then return '|'
	elseif s == '\\e' then return '='
	elseif s == '\\_' then return ' '
	else return s
	end
end

--- Escapes a specified format string
--
-- @param {string} string String with escape characters
-- @return Escaped string
function p.escape( string )
	return mw.ustring.gsub( string, '\\.', replace )
end

--- Main entrypoint
--
-- @usage {{#invoke:escape input|main|1=String to escape}}
-- @param {table} frame Calling frame
-- @param {table} frame.args Frame arguments
-- @param {string} frame.args[1] Escaped string sequence
-- @return Escaped wikitext
function p.main( frame )
	return p.escape( frame.args[1] )
end

return p