Changes

Pywikibot 6.4.0
{{Module rating |pre-alpha<!-- Values: pre-alpha • alpha • beta • release • protected -- If a rating not needed/relevant, delete this template call -->}}
<!-- Please place categories where indicated at the bottom of this page and interwikis at Wikidata (see [[Wikipedia:Wikidata]]) -->
Implements Lua functions [[:mw:Extension:Scribunto/Lua_reference_manual#mw.text.decode|mw.text.decode]], [[:mw:Extension:Scribunto/Lua_reference_manual#mw.text.encode|mw.text.encode]] in a module.
:<code><nowiki>{{#invoke:decode|s=Source&amp;nbsp;text}}</nowiki></code> &rarr; <code><nowiki>Source&nbsp;text</nowiki></code>

See [[List of XML and HTML character entity references]].

== Decode (&amp;copy; &rarr; ©) ==
:Decodes [[List of XML and HTML character entity references|Named Entities]] ''from'' entity name ''into'' a regular (unicode) character:
:<code>&amp;copy;</code> &rarr; <code>&copy;</code>
:<code>&amp;gt;</code> &rarr; <code>></code>

All welldefined named entities are decoded ([https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references HTML Named character references], formally: as defined in the [https://www.php.net/get_html_translation_table PHP table]).

:A regular, rendered sentence:
::"At 100 °F, & with a "burning" sun above, we , we ⁄walked⁄."

:In code:
::"<code>At 100&amp;nbsp;&amp;deg;F, &amp; with a &amp;quot;burning&amp;quot; sun above, we &amp;frasl;walked&amp;frasl;.</code>" -- wikitext

:Processing:
:<code><nowiki>{{#invoke:decodeEncode|decode|s=At 100&nbsp;&deg;F, &amp; with a &quot;burning&quot; sun above, we &frasl;walked&frasl;.}}</nowiki></code> &rarr;
::<code>{{#invoke:decodeEncode|decode|s=At 100&nbsp;&deg;F, &amp; with a &quot;burning&quot; sun above, we &frasl;walked&frasl;.}}</code> -- In code: straight characters, no named entities.

:Renders, again:
::"At 100 °F, & with a "burning" sun above, we ⁄walked⁄."

===Decode a reduced set only===
By setting {{para|subset_only|true}}, only these five entity names are decoded: '&amp;lt;', '&amp;gt;', '&amp;amp;', '&amp;quot;', '&amp;nbsp;' (that is, into '&lt;', '&gt;', '&amp;', '&quot;', '&nbsp;').

:'''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 {{para|decodeNamedEntities}}, 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 {{para|subset_only}}: {{para|decodeNamedEntities|false}} = {{para|subset_only|true}}.

:Also, this module ignores the "omitted" logic: {{para|subset_only}} should be set explicitly to 'true' to be effective.

== Encode (© &rarr; &amp;copy;) ==
:Function <code>encode</code> encodes some entity-named characters into that name (for example: <code>&</code> &rarr; <code>&amp;amp;</code>).

Regular sentence:
:"At >100 °F, & with a "burning" sun above, we walked. ©"

In code:
:"<code><nowiki>At >100 °F, & with a "burning" sun above, we walked. ©</nowiki></code>"

Encode:
:<code><nowiki>{{#invoke:decodeEncode|encode|s=At >100 °F, & with a "burning" sun above, we walked. ©|charset=&<>{{!}}°"'&©}}</nowiki></code>

:&rarr;

:<code><nowiki>At &amp;gt;100 &amp;#176;F, &amp;amp; with a &amp;quot;burning&amp;quot; sun above, we walked. &amp;#169;</nowiki></code><!-- used Special:ExpandTemplate -->

:Renders as:

:"At &gt;100 &#176;F, &amp; with a &quot;burning&quot; sun above, we walked. &#169;"
===character set to encode===

Per Lua documentation, only a small set of characters is processed. The characterset can be set (expanded) by using {{para|charset}}.

:Example: {{para|charset|<nowiki><>" \'&</nowiki>}} (the default), {{para|charset|<nowiki><>°"'&©{{!}}</nowiki>}}; characters not in the default will be replaced by their decimal entity: <code>©</code> &rarr; <code>&amp;#169;</code> <small>(hexadecimal number, not decimal nor named &amp;copy;)</small>

==Template==
{{As of|Dec 2020}}, there are no tempates implementing this module.

==See also==

* [[:mw:Extension:Scribunto/Lua_reference_manual#mw.text.decode|mw.text.decode]]

* [[:mw:Extension:Scribunto/Lua_reference_manual#mw.text.encode|mw.text.encode]]

* [[:Module:Urldecode]]
<includeonly>{{sandbox other||
<!-- Categories below this line, please; interwikis at Wikidata -->
[[Category:Wikitext processing templates]]
[[Category:Modules that manipulate strings]]

}}</includeonly>