Jump to content
Wikipedia The Free Encyclopedia

Module:DecodeEncode/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
(2 intermediate revisions by 2 users not shown)
Line 14: Line 14:
:<code>&amp;gt;</code> &rarr; <code>></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]).
All (追記) well-defined (追記ここまで) 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:
:A regular, rendered sentence:
Line 85: Line 85:
[[Category:Wikitext processing templates]]
[[Category:Wikitext processing templates]]
[[Category:Modules that manipulate strings]]
[[Category:Modules that manipulate strings]]
[[Category:Template metamodules]]

}}</includeonly>
}}</includeonly(追記) ><noinclude (追記ここまで)>
[[Category:Module documentation pages]]
</noinclude>

Latest revision as of 08:11, 22 April 2025

This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Warning This Lua module is used on approximately 140,000 pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.

Implements Lua functions mw.text.decode, mw.text.encode in a module.

{{#invoke:decodeEncode|decode|s=Source&nbsp;text&copy;}}Source text©

See List of XML and HTML character entity references.

Decode (&copy; → ©)

[edit ]
See § Known issues for possible THIN SPACE, epsilon issues
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:
{{#invoke:decodeEncode|decode|s=At 100 °F, & with a "burning" sun above, we ⁄walked⁄.}}
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⁄."

Decode a reduced set only

[edit ]

By setting |subset_only=true, only these five entity names are decoded: '&lt;', '&gt;', '&amp;', '&quot;', '&nbsp;' (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 |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 |subset_only=: |decodeNamedEntities=false = |subset_only=true.
Also, this module ignores the "omitted" logic: |subset_only= should be set explicitly to 'true' to be effective.

Encode (© → &copy;)

[edit ]
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:

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

Encode:

{{#invoke:decodeEncode|encode|s=At >100 °F, & with a "burning" sun above, we walked. ©|charset=&<>{{!}}°"'&©}}
At &gt;100 &#176;F, &amp; with a &quot;burning&quot; sun above, we walked. &#169;
Renders as:
"At >100 °F, & with a "burning" sun above, we walked. ©"

character set to encode

[edit ]

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

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

Known issues

[edit ]
  • 13 Sep 2021: NOTE: The encode function with user-supplied charset is now used productively in {{R/superscript }} and {{R/ref }}. Before implementing breaking changes here, these templates need to be adjusted accordingly!
  • 26 Sep 2021: U+2009 THIN SPACE (&thinsp;, &ThinSpace;)
Note: Possible bug: Decoding &ThinSpace; works, but &thinsp; doesn't.
Resolved in code.
  • 4 Feb 2023: U+03B5 ε GREEK SMALL LETTER EPSILON (&epsi;, &epsilon;)
See Module talk:DecodeEncode § Bug report: bad decoding of U+03B5 ε (epsilon)
Resolved in code.

See also

[edit ]

AltStyle によって変換されたページ (->オリジナル) /