Implements Lua functions mw.text.decode, mw.text.encode in a module.
<nowiki>{{#invoke:decodeEncode|decode|s=Source&nbsp;text&copy;}}</nowiki>
→ <nowiki>Source text©</nowiki>
See List of XML and HTML character entity references.
Decodes Named Entities from entity name into a regular (unicode) character:
&copy;
→ ©
&gt;
→ >
All well-defined named entities are decoded (HTML Named character references, formally: as defined in the PHP table).
A regular, rendered sentence:
"At 100 °F, & with a "burning" sun above, we, we ⁄walked⁄."
In code:
"At 100&nbsp;&deg;F, & with a &quot;burning&quot; sun above, we &frasl;walked&frasl;.
" -- wikitext
Processing:
<nowiki>{{#invoke:decodeEncode|decode|s=At 100 °F, & with a "burning" sun above, we ⁄walked⁄.}}</nowiki>
→
{{#invoke:decodeEncode|decode|s=At 100 °F, & with a "burning" sun above, we ⁄walked⁄.}}
-- In code: straight characters, no named entities.
Renders, again:
"At 100 °F, & with a "burning" sun above, we ⁄walked⁄."
By setting, only these five entity names are decoded: '<', '>', '&', '"', ' ' (that is, into '<', '>', '&', '"', ' ').
Note: There is a difference with the relevant Lua parameter. (This only concerns your task if you also work directly with the Lua mw.text.decode function). Lua documentation defines parameter, having this effect: when omitted or false, only the reduced set of entities is recognized and decoded. This use of 'false' is inverted in using : = .
Also, this module ignores the "omitted" logic: should be set explicitly to 'true' to be effective.
Function encode
encodes some entity-named characters into that name (for example: &
→ &amp;
).
Regular sentence:
"At >100 °F, & with a "burning" sun above, we walked. ©"
In code:
"<nowiki>At >100 °F, & with a "burning" sun above, we walked. ©</nowiki>
"
Encode:
<nowiki>{{#invoke:decodeEncode|encode|s=At >100 °F, & with a "burning" sun above, we walked. ©|charset=&<>{{!}}°"'&©}}</nowiki>
→
<nowiki>At &gt;100 &#176;F, &amp; with a &quot;burning&quot; sun above, we walked. &#169;</nowiki>
Renders as:
"At >100 °F, & with a "burning" sun above, we walked. ©"
Per Lua documentation, only a small set of characters is processed. The characterset can be set (expanded) by using .
Example: