Jump to content
Wikipedia The Free Encyclopedia

Module:Labelled list hatnote

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
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.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
[画像:Warning] This Lua module is used in MediaWiki:Wantedpages-summary , and on approximately 608,000 pages.
Changes to it can cause immediate changes to the Wikipedia user interface.
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. Please discuss changes on the talk page before implementing them.
This module depends on the following other modules:
This module provides a handful of functions that make it easy to implement hatnotes that take the form of a label in front of a list of pages, e.g.
LABEL: A, B, and C

Usage

labelledList

Invoking the labelledList() function is enough to implement most such templates:

{{#invoke:Labelled list hatnote|labelledList|Universal label}}

or

{{#invoke:Labelled list hatnote|labelledList|Singular label|Plural label}}

For example, providing "See also" instead of "Universal label" duplicates the functionality of {{see also }}, while providing "Main article" and "Main articles" instead of "Singular label" and "Plural label" duplicates the (article namespace) functionality of {{main }}.

If third and fourth labels are provided, they'll be used in the case where any of the target pages are outside the article namespace, so e.g. {{main }} can be implemented thus:

{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}}

preprocessDisplays

The preprocessDisplays() function takes a raw list of arguments and combines in any display arguments. For example, {{see also|1|l1=One}} initially has the arguments table {'1', ['l1'] = 'One'}; this table would combine those into the table {'1|One'}. It overrides manual piping (e.g. {{see also|1{{!}}2|l1=One}}{'1|One'}) and compresses sparse arrays if a parameter is skipped or left empty.

Example:
localmLabelledList=require('Module:Labelled list hatnote')
localpages=mLabelledList.preprocessDisplays(args)

_labelledList

For modules that need to modify the functionality slightly while still using it, _labelledList() provides some flexibility. It takes three parameters:

  1. A pages list, preferably preprocessed and compressed by preprocessDisplays
  2. A labels table, where the first item is the singular or universal label, and the second either a plural label or a copy of the first.
  3. An options table, preferably containing:
    • a template string with the full title of the template. Defaults to the title of this module.
    • a category string (or nil) as taken by makeWikitextError from Module:Hatnote, to optionally disable error categories
    • a selfref string (or nil) as taken by _hatnote to enable the selfref option
Example:
localmLabelledList=require('Module:Labelled list hatnote')
returnmLabelledList._labelledList(pages,labels,options)

Errors

This module causes templates based on it to produce an error message if no page names are provided as template parameters. Normally, these should lead back to "Errors" sections in the documentation of those templates. However, if those templates use a module with _labelledList() and don't provide a template item in their options table, that error defaults to leading back here. The error can be solved by providing at least one valid page-name parameter to the template in question; the problem in the template can be fixed by providing some value to the template item of the _labelledList() options table.

The above documentation is transcluded from Module:Labelled list hatnote/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit) pages.
Subpages of this module.

 --------------------------------------------------------------------------------
 -- Labelled list --
 -- --
 -- This module does the core work of creating a hatnote composed of a list --
 -- prefixed by a colon-terminated label, i.e. "LABEL: [andList of pages]", --
 -- for {{see also}} and similar templates. --
 --------------------------------------------------------------------------------

 localmHatnote=require('Module:Hatnote')
 localmHatlist=require('Module:Hatnote list')
 localmArguments--initialize lazily
 localyesno--initialize lazily
 localp={}

 -- Defaults global to this module
 localdefaults={
 label='See also',--Final fallback for label argument
 labelForm='%s: %s',
 prefixes={'label','label ','l'},
 template='Module:Labelled list hatnote'
 }

 -- Localizable message strings
 localmsg={
 errorSuffix='#Errors',
 noInputWarning='no page names specified',
 noOutputWarning=
 "'''[[%s]] — no output: none of the target pages exist.'''"
 }

 -- Helper function that pre-combines display parameters into page arguments.
 -- Also compresses sparse arrays, as a desirable side-effect.
 functionp.preprocessDisplays(args,prefixes)
 -- Prefixes specify which parameters, in order, to check for display options
 -- They each have numbers auto-appended, e.g. 'label1', 'label 1', & 'l1'
 prefixes=prefixesordefaults.prefixes
 localindices={}
 localsparsePages={}
 fork,vinpairs(args)do
 iftype(k)=='number'then
 indices[#indices+1]=k
 localdisplay
 fori=1,#prefixesdo
 display=args[prefixes[i]..k]
 ifdisplaythenbreakend
 end
 sparsePages[k]=displayand
 string.format('%s|%s',string.gsub(v,'|.*$',''),display)orv
 end
 end
 table.sort(indices)
 localpages={}
 fork,vinipairs(indices)dopages[#pages+1]=sparsePages[v]end
 returnpages
 end

 --Helper function to get a page target from a processed page string
 --e.g. "Page|Label" → "Page" or "Target" → "Target"
 localfunctiongetTarget(pagename)
 localpipe=string.find(pagename,'|')
 returnstring.sub(pagename,0,pipeandpipe-1ornil)
 end

 -- Produces a labelled pages-list hatnote.
 -- The main frame (template definition) takes 1 or 2 arguments, for a singular
 -- and (optionally) plural label respectively:
 -- * {{#invoke:Labelled list hatnote|labelledList|Singular label|Plural label}}
 -- The resulting template takes pagename & label parameters normally.
 functionp.labelledList(frame)
 mArguments=require('Module:Arguments')
 yesno=require('Module:Yesno')
 locallabels={frame.args[1]ordefaults.label}
 labels[2]=frame.args[2]orlabels[1]
 labels[3]=frame.args[3]--no defaulting
 labels[4]=frame.args[4]--no defaulting
 localtemplate=frame:getParent():getTitle()
 localargs=mArguments.getArgs(frame,{parentOnly=true})
 localpages=p.preprocessDisplays(args)
 localoptions={
 category=yesno(args.category),
 extraclasses=frame.args.extraclasses,
 ifexists=yesno(frame.args.ifexists),
 namespace=frame.args.namespaceorargs.namespace,
 selfref=yesno(frame.args.selfreforargs.selfref),
 template=template
 }
 returnp._labelledList(pages,labels,options)
 end
 localfunctionexists(title)
 localsuccess,result=pcall(function()returntitle.existsend)
 ifsuccessthen
 returnresult
 else
 returntrue
 end
 end

 functionp._labelledList(pages,labels,options)
 localremovednonexist=false
 ifoptions.ifexiststhen
 fork=#pages,1,-1do--iterate backwards to allow smooth removals
 localv=pages[k]
 ifmw.ustring.sub(mw.text.trim(v),1,1)~="#"then
 localtitle=mw.title.new(getTarget(v),namespace)
 if(v=='')or(title==nil)ornotexists(title)then
 table.remove(pages,k)
 removednonexist=true
 end
 end
 end
 end
 labels=labelsor{}
 label=(#pages==1andlabels[1]orlabels[2])ordefaults.label
 fork,vinpairs(pages)do
 ifmHatnote.findNamespaceId(v)~=0then
 label=
 (
 #pages==1and
 (labels[3]orlabels[1]ordefaults.label)or
 (labels[4]orlabels[2]ordefaults.label)
 )ordefaults.label
 end
 end
 if#pages==0then
 ifremovednonexistthen
 mw.addWarning(
 string.format(
 msg.noOutputWarning,options.templateordefaults.template
 )
 )
 return''
 else
 returnmHatnote.makeWikitextError(
 msg.noInputWarning,
 (options.templateordefaults.template)..msg.errorSuffix,
 options.category
 )
 end
 end
 localtext=string.format(
 options.labelFormordefaults.labelForm,
 label,
 mHatlist.andList(pages,true)
 )
 localhnOptions={
 extraclasses=options.extraclasses,
 selfref=options.selfref
 }
 returnmHatnote._hatnote(text,hnOptions)
 end

 returnp

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