Module:Char

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

heavily used module, because of {{Tle/parm}}, {{Igen/sub}} and {{Igen/top}}.

This module checks for some critical leading characters in the passed string,
and replaces them by their decimal numeric character references,
to avoid their interpretion as wiki syntax, and returns the (altered) string.

It can be invoked directly by either "Char|substitute" or shorter by "Char|subst"
or by using the Template:Charsubst: {{Charsubst|1= }}

Note:
This module can substitute the leading characters #, *, : and ;.
Other characters, like e.g. & or < are returned-as-they-are, but their bracketing, e.g. {{Charsubst|<}}, may avoid misinterpretion.
Special characters, like | (&#124;) and =(&#61;) may cause problems within templates, but to substitute them by their decimal numeric character references won't help.
In some cases they can be expressed by workaround templates: | by {{!}} and = by {{=}}.
It is possible to replace the pipe | by other characters, e.g the similar looking mathematical operator . This Unicode block contains also operators as , and . But neither in that block, nor among all the 1114112 Unicode characters is a substitute for the equal sign "=" known.

See also Hilfe_Diskussion:Sonderzeichenreferenz#HTML_Entitäten_Tabelle

Code

-- This function can substitute some critical leading characters
-- by their decimal numeric character references
-- to avoid their interpretion as wiki syntax

local ncr = {}

-- function:  removes characters at the start of the string: '#', '*', ':', ';'
function ncr.subst      ( frame )
	local gpar = frame.args
	local char = ''
	if gpar then char = mw.text.trim ( gpar[1] )-- global
		else	 char = mw.text.trim ( frame )	-- local
	end
   if  mw.ustring.sub ( char, 1, 1 ) == '#' then 
         return '&#35;' .. mw.ustring.sub( char, 2, #char );
    elseif mw.ustring.sub ( char, 1, 1 ) == '*' then 
         return '&#42;' .. mw.ustring.sub( char, 2, #char );
    elseif mw.ustring.sub ( char, 1, 1 ) == ':' then 
         return '&#58;' .. mw.ustring.sub( char, 2, #char );
    elseif mw.ustring.sub ( char, 1, 1 ) == ';' then 
         return '&#59;' .. mw.ustring.sub( char, 2, #char );
    else
         return char;
    end
end -- end function subst

-- alias long function name for abbreviation 
function ncr.substitute ( frame )
	local gpar = frame.args
	return ncr.subst ( gpar[1] );
end -- end function substitute


-- function: removes characters: '"', '&', "'", '<', '=', '>', '[', ']', '{', '|', '}'
function ncr.nowiki ( frame )
	local gpar = frame.args
	return mw.text.nowiki( gpar[1] );
end -- end function nowiki

return ncr;