Difference between revisions of "Module:Hatnote/doc"

From blackwiki
Jump to navigation Jump to search
test>DePiep
m (high-risk tc on 501681 pages)
Line 1: Line 1:
{{Documentation subpage}}
+
{{high-risk|802,000+}}
<includeonly>{{High-risk|500,000+}}</includeonly>
 
{{shortcut|T:HAT}}
 
{{lua|Module:Hatnote}}
 
  
This template produces formatted text, following the guideline for a '''[[WP:HATNOTE|Wikipedia hatnote]]'''.
+
This is a meta-module that provides various functions for making hatnotes. It implements the {{tl|hatnote}} template, for use in hatnotes at the top of pages, and the {{tl|format link}} template, which is used to format a wikilink for use in hatnotes. It also contains a number of helper functions for use in other Lua hatnote modules.
:<code><nowiki>{{hatnote|Example hatnote text.}}</nowiki></code> &rarr;<br><!-- a construct to show hatnote as expected (using newline) -->
 
{{hatnote|Example hatnote text.}}
 
  
Broadly speaking, a hatnote should answer a readers' question (maybe preemptively): ''Am I on the right page?''
+
== Use from wikitext ==
  
{{TOC limit}}
+
The functions in this module cannot be used directly from #invoke, and must be used through templates instead. Please see [[Template:Hatnote]] and [[Template:Format link]] for documentation.
  
== Function ==
+
== Use from other Lua modules ==
This template is primarily used to add a correctly formatted ''hatnote'' to a page. Often, but not always, this is a ''disambiguation link'' at the top of article pages. It places an HTML <code>div-</code> / <code>div</code> block around the text entered as its only argument, which provides standardized formatting (contents are indented and italicized in most displays); it also isolates the contained code to make sure that it is interpreted correctly.
 
  
This template is also used as the "meta-template" for additional specialized disambiguation link templates; see [[:Category:Hatnote templates]] for a list.
+
To load this module from another Lua module, use the following code.
  
The template does not automatically create links of any kind. Links and other desired formatting must be explicitly added, using normal [[Wikipedia:Cheatsheet|Wikipedia markup]].
+
<source lang="lua">
 +
local mHatnote = require('Module:Hatnote')
 +
</source>
  
== Usage ==
+
You can then use the functions as documented below.
  
; Basic usage:
+
=== Hatnote ===
&#123;{hatnote|''text''}}
 
  
; All parameters:
+
<source lang="lua">
&#123;{hatnote|''text''|extraclasses=''extra classes''|selfref=''yes''|category=''no''}}
+
mHatnote._hatnote(s, options)
 +
</source>
  
== Parameters ==
+
Formats the string <var>s</var> as a hatnote. This encloses <var>s</var> in the tags {{tag|div|params=class="hatnote"}}. Options are provided in the <var>options</var> table. Options include:
 +
* <var>options.extraclasses</var> - a string of extra classes to provide
 +
* <var>options.selfref</var> - if this is not nil or false, adds the class "selfref", used to denote self-references to Wikipedia (see [[Template:Selfref]]))
 +
The CSS of the hatnote class is defined in [[MediaWiki:Common.css]].
  
This template accepts the following parameters:
+
; Example 1
* <code>1</code> - the hatnote text. (required)
 
* <code>extraclasses</code> - any extra CSS classes to be added. For example, the {{tl|see also}} template adds the classes {{para|extraclasses|boilerplate seealso}}.
 
* <code>selfref</code> - if set to "yes", "y", "true" or "1", adds the CSS class "selfref". This is used to denote self-references to Wikipedia. See [[Template:Selfref]] for more information.
 
* <code>category</code> - if set to "no", "n", "false", or "0", suppresses the error tracking category ([[:Category:Hatnote templates with errors]]). This only has an effect if the first positional parameter (the hatnote text) is omitted.
 
  
== Example ==
+
<source lang="lua">
 +
mHatnote._hatnote('This is a hatnote.')
 +
</source>
  
* <code><nowiki>{{hatnote|Example hatnote text}}</nowiki></code> &rarr; {{hatnote|Example hatnote text}}
+
Produces:
 +
{{tag|div|content=This is a hatnote.|params=class="hatnote"}}
  
== Errors ==
+
Displays as:
 +
{{hatnote|This is a hatnote.}}
  
If no hatnote text is supplied, the template will output the following message:
+
; Example 2
* {{hatnote|category=no}}
 
  
If you see this error message, it is for one of four reasons:
+
<source lang="lua">
# No parameters were specified (the template code was <code><nowiki>{{hatnote}}</nowiki></code>). Please use <code><nowiki>{{hatnote|</nowiki>''text''<nowiki>}}</nowiki></code> instead.
+
mHatnote._hatnote('This is a hatnote.', {extraclasses = 'boilerplate seealso', selfref = true})
# Some parameters were specified, but the hatnote text wasn't included. For example, the template text <code><nowiki>{{hatnote|extraclasses=seealso}}</nowiki></code> will produce this error. Please use (for example) <code><nowiki>{{hatnote|</nowiki>''text''<nowiki>|extraclasses=seealso}}</nowiki></code> instead.
+
</source>
# The hatnote text was specified, but that text contains an equals sign ("="). The equals sign has a special meaning in template code, and because of this it cannot be used in template parameters that do not specify a parameter name. For example, the template code <code><nowiki>{{hatnote|2+2=4}}</nowiki></code> will produce this error. To work around this, you can specify the parameter name explictly by using <code>1=</code> before the hatnote text, like this: <code><nowiki>{{hatnote|1=2+2=4}}</nowiki></code>.
 
# You tried to access [[Module:Hatnote]] directly by using <code><nowiki>{{#invoke:hatnote|hatnote|</nowiki>''text''<nowiki>}}</nowiki></code>. Use of #invoke in this way has been disabled for performance reasons. Please use <code><nowiki>{{hatnote|</nowiki>''text''<nowiki>}}</nowiki></code> instead.
 
  
If you see this error message and are unsure of what to do, please post a message on [[Template talk:Hatnote]], and someone should be able to help you.
+
Produces:
 +
{{tag|div|content=This is a hatnote.|params=class="hatnote boilerplate seealso selfref"}}
  
Pages that contain this error message are tracked in [[:Category:Hatnote templates with errors]].
+
Displayed as:
 +
{{hatnote|This is a hatnote.|extraclasses=boilerplate seealso|selfref=true}}
  
== Technical details ==
+
=== Format link ===
  
The HTML code produced by this template looks like this:
+
<source lang="lua">
 +
mHatnote._formatLink(link, display)
 +
</source>
  
* <code><nowiki><div class="hatnote"></nowiki>''hatnote text''<nowiki></div></nowiki></code>
+
Formats <var>link</var> as a wikilink for display in hatnote templates, with optional display value <var>display</var>. Categories and files are automatically escaped with the [[Help:Colon trick|colon trick]], and links to sections are automatically formatted as ''page § section'', rather than the MediaWiki default of ''page#section''.
  
The code is produced by [[Module:Hatnote]].
+
;Examples:
 +
: {{code|mHatnote._formatLink('Lion')|lua}} &rarr; <nowiki>[[Lion]]</nowiki> &rarr; {{format hatnote link|Lion}}
 +
: {{code|mHatnote._formatLink('Lion#Etymology')|lua}} &rarr; <nowiki>[[Lion#Etymology|Lion § Etymology]]</nowiki> &rarr; {{format hatnote link|Lion#Etymology}}
 +
: {{code|mHatnote._formatLink('Category:Lions')|lua}} &rarr; <nowiki>[[:Category:Lions]]</nowiki> &rarr; {{format hatnote link|Category:Lions}}
 +
: {{code|mHatnote._formatLink('Lion#Etymology', 'Etymology of lion')|lua}} &rarr; <nowiki>[[Lion#Etymology|Etymology of lion]]</nowiki> &rarr; {{format hatnote link|Lion#Etymology|Etymology of lion}}
 +
 
 +
=== Format pages ===
 +
 
 +
<source lang="lua">
 +
mHatnote.formatPages(...)
 +
</source>
 +
 
 +
Formats a list of pages using the [[#Format link|_formatLink]] function, and returns the result as an array. For example, the code {{code|mHatnote.formatPages('Lion', 'Category:Lions', 'Lion#Etymology')|lua}} would produce an array like {{code|{'[[Lion]]', '[[:Category:Lions]]', '[[Lion#Etymology|Lion § Etymology]]'}|lua}}.
 +
 
 +
=== Format page tables ===
 +
 
 +
<source lang="lua">
 +
mHatnote.formatPageTables(...)
 +
</source>
 +
 
 +
Takes a list of page/display tables, formats them with the [[#Format link|_formatLink]] function, and returns the result as an array. Each item in the list must be a table. The first value in the table is the link, and is required. The second value in the table is the display value, and is optional. For example, the code {{code|mHatnote.formatPages({'Lion', 'the Lion article'}, {'Category:Lions'}, {'Lion#Etymology', 'the etymology of lion'})|lua}} would produce an array like {{code|{'[[Lion|the Lion article]]', '[[:Category:Lions]]', '[[Lion#Etymology|the etymology of lion]]'}|lua}}.
 +
 
 +
=== Find namespace id ===
 +
 
 +
<source lang="lua">
 +
mHatnote.findNamespaceId(link, removeColon)
 +
</source>
 +
 
 +
Finds the [[WP:NS|namespace id]] of the string <var>link</var>, which should be a valid page name, with or without the section name. This function will not work if the page name is enclosed with square brackets. When trying to parse the namespace name, colons are removed from the start of the link by default. This is helpful if users have specified colons when they are not strictly necessary. If you do not need to check for initial colons, set <var>removeColon</var> to false.
 +
 
 +
;Examples:
 +
: {{code|mHatnote.findNamespaceId('Lion')|lua}} &rarr; 0
 +
: {{code|mHatnote.findNamespaceId('Category:Lions')|lua}} &rarr; 14
 +
: {{code|mHatnote.findNamespaceId(':Category:Lions')|lua}} &rarr; 14
 +
: {{code|mHatnote.findNamespaceId(':Category:Lions', false)|lua}} &rarr; 0 (the namespace is detected as ":Category", rather than "Category")
 +
 
 +
=== Make wikitext error ===
 +
 
 +
<source lang="lua">
 +
mHatnote.makeWikitextError(msg, helpLink, addTrackingCategory)
 +
</source>
 +
 
 +
Formats the string <var>msg</var> as a red wikitext error message, with optional link to a help page <var>helpLink</var>. Normally this function also adds [[:Category:Hatnote templates with errors]]; however, if <var>addTrackingCategory</var> is not false after being passed through [[Module:Yesno]], then the category is suppressed. This means that the category can be suppressed with <var>addTrackingCategory</var> values including "no", "n", 0, "false", and {{code|false|lua}}.
 +
 
 +
Examples:
 +
:{{code|mHatnote.makeWikitextError('an error has occurred')|lua}} &rarr; <strong class="error">Error: an error has occurred.</strong>
 +
:{{code|mHatnote.makeWikitextError('an error has occurred', 'Template:Example#Errors')|lua}} &rarr; <strong class="error">Error: an error has occurred ([[Template:Example#Errors|help]]).</strong>
 +
 
 +
== Examples ==
 +
 
 +
For examples of how this module is used in other Lua modules, see the following (listed in order of complexity):
 +
 
 +
* [[Module:Details]]
 +
* [[Module:Further]]
 +
* [[Module:See also]]
 +
* [[Module:Main]]
 +
 
 +
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||
 +
<!-- Categories go here and interwikis go in Wikidata. -->
  
{{Hatnote templates documentation}}
 
{{Collapse top|TemplateData}}
 
{{TemplateDataHeader}}
 
<templatedata>
 
{
 
  "description": "Template for creating a standard Wikipedia hatnote. A hatnote is a short note placed at the top of an article to provide disambiguation of closely related terms or summarise a topic, explaining its boundaries.",
 
  "params": {
 
    "1": {
 
      "label": "Text",
 
      "description": "This field should contain the text that will be displayed in the hatnote.",
 
      "type": "string",
 
      "required": true
 
    },
 
    "extraclasses": {
 
      "type": "string/line",
 
      "label": "Extra classes",
 
      "description": "Extra CSS classes to be added to the <div> tags surrounding the hatnote text."
 
    },
 
    "selfref": {
 
      "type": "string/line",
 
      "label": "Self reference",
 
      "description": "Set to \"yes\" if the hatnote text is a self-reference to Wikipedia that would not make sense on mirrors or forks of the Wikipedia site. (E.g. \"For the Wikipedia Sandbox, see [[WP:SAND]]\".)"
 
    },
 
    "category": {
 
      "label": "Category",
 
      "description": "Set to \"no\", \"n\", \"false\", or \"0\" to suppresses the error tracking category (Category:Hatnote templates with errors). This only has an effect if the hatnote text is omitted."
 
    }
 
  }
 
}
 
</templatedata>
 
{{Collapse bottom}}<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||
 
[[Category:Hatnote templates| ]]
 
 
}}</includeonly>
 
}}</includeonly>

Revision as of 12:59, 26 April 2015

This is a meta-module that provides various functions for making hatnotes. It implements the {{hatnote}} template, for use in hatnotes at the top of pages, and the {{format link}} template, which is used to format a wikilink for use in hatnotes. It also contains a number of helper functions for use in other Lua hatnote modules.

Use from wikitext

The functions in this module cannot be used directly from #invoke, and must be used through templates instead. Please see Template:Hatnote and Template:Format link for documentation.

Use from other Lua modules

To load this module from another Lua module, use the following code.

local mHatnote = require('Module:Hatnote')

You can then use the functions as documented below.

Hatnote

mHatnote._hatnote(s, options)

Formats the string s as a hatnote. This encloses s in the tags <div class="hatnote">...</div>. Options are provided in the options table. Options include:

  • options.extraclasses - a string of extra classes to provide
  • options.selfref - if this is not nil or false, adds the class "selfref", used to denote self-references to Wikipedia (see Template:Selfref))

The CSS of the hatnote class is defined in MediaWiki:Common.css.

Example 1
mHatnote._hatnote('This is a hatnote.')

Produces: <div class="hatnote">This is a hatnote.</div>

Displays as:

Example 2
mHatnote._hatnote('This is a hatnote.', {extraclasses = 'boilerplate seealso', selfref = true})

Produces: <div class="hatnote boilerplate seealso selfref">This is a hatnote.</div>

Displayed as:

Format link

mHatnote._formatLink(link, display)

Formats link as a wikilink for display in hatnote templates, with optional display value display. Categories and files are automatically escaped with the colon trick, and links to sections are automatically formatted as page § section, rather than the MediaWiki default of page#section.

Examples
mHatnote._formatLink('Lion') → [[Lion]] → Lion
mHatnote._formatLink('Lion#Etymology') → [[Lion#Etymology|Lion § Etymology]] → Lion § Etymology
mHatnote._formatLink('Category:Lions') → [[:Category:Lions]] → Category:Lions
mHatnote._formatLink('Lion#Etymology', 'Etymology of lion') → [[Lion#Etymology|Etymology of lion]] → Etymology of lion

Format pages

mHatnote.formatPages(...)

Formats a list of pages using the _formatLink function, and returns the result as an array. For example, the code mHatnote.formatPages('Lion', 'Category:Lions', 'Lion#Etymology') would produce an array like {'[[Lion]]', '[[:Category:Lions]]', '[[Lion#Etymology|Lion § Etymology]]'}.

Format page tables

mHatnote.formatPageTables(...)

Takes a list of page/display tables, formats them with the _formatLink function, and returns the result as an array. Each item in the list must be a table. The first value in the table is the link, and is required. The second value in the table is the display value, and is optional. For example, the code mHatnote.formatPages({'Lion', 'the Lion article'}, {'Category:Lions'}, {'Lion#Etymology', 'the etymology of lion'}) would produce an array like {'[[Lion|the Lion article]]', '[[:Category:Lions]]', '[[Lion#Etymology|the etymology of lion]]'}.

Find namespace id

mHatnote.findNamespaceId(link, removeColon)

Finds the namespace id of the string link, which should be a valid page name, with or without the section name. This function will not work if the page name is enclosed with square brackets. When trying to parse the namespace name, colons are removed from the start of the link by default. This is helpful if users have specified colons when they are not strictly necessary. If you do not need to check for initial colons, set removeColon to false.

Examples
mHatnote.findNamespaceId('Lion') → 0
mHatnote.findNamespaceId('Category:Lions') → 14
mHatnote.findNamespaceId(':Category:Lions') → 14
mHatnote.findNamespaceId(':Category:Lions', false) → 0 (the namespace is detected as ":Category", rather than "Category")

Make wikitext error

mHatnote.makeWikitextError(msg, helpLink, addTrackingCategory)

Formats the string msg as a red wikitext error message, with optional link to a help page helpLink. Normally this function also adds Category:Hatnote templates with errors; however, if addTrackingCategory is not false after being passed through Module:Yesno, then the category is suppressed. This means that the category can be suppressed with addTrackingCategory values including "no", "n", 0, "false", and false.

Examples:

mHatnote.makeWikitextError('an error has occurred')Error: an error has occurred.
mHatnote.makeWikitextError('an error has occurred', 'Template:Example#Errors')Error: an error has occurred (help).

Examples

For examples of how this module is used in other Lua modules, see the following (listed in order of complexity):