Jump to content
MediaWiki

Module:Section link

From mediawiki.org
Module documentation
This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing.

Implements {{Section link}}.

Usage

[edit ]

{{#invoke:Section link|main|page_name|section_name|...}}

The above documentation is transcluded from Module:Section link/doc. (edit | history)
Editors can experiment in this module’s sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.

 -- This module implements {{section link}}.

 localcheckType=require('libraryUtil').checkType
 localyesno=require('Module:Yesno')

 localp={}

 localfunctionmakeSectionLink(page,section,display)
 display=displayorsection
 page=pageor''
 returnstring.format('[[%s#%s|%s]]',page,section,display)
 end

 functionp._main(page,sections,options,title)
 -- Validate input.
 checkType('_main',1,page,'string',true)
 checkType('_main',3,options,'table',true)
 ifsections==nilthen
 sections={}
 elseiftype(sections)=='string'then
 sections={sections}
 elseiftype(sections)~='table'then
 error(string.format(
 "type error in argument #2 to '_main' "..
 "(string, table or nil expected, got %s)",
 type(sections)
 ),2)
 end
 options=optionsor{}
 title=titleormw.title.getCurrentTitle()

 -- Deal with blank page names elegantly
 ifpageandnotpage:find('%S')then
 page=nil
 options.nopage=true
 end

 -- Make the link(s).
 localisShowingPage=notyesno(options.nopage)
 if#sections<=1then
 locallinkPage=pageor''
 localsection=sections[1]or'Notes'
 localdisplay='§&nbsp;'..section
 ifisShowingPagethen
 page=pageortitle.prefixedText
 display=page..' '..display
 end
 returnmakeSectionLink(linkPage,section,display)
 else
 -- Multiple sections. First, make a list of the links to display.
 localret={}
 fori,sectioninipairs(sections)do
 ret[i]=makeSectionLink(page,section)
 end

 -- Assemble the list of links into a string with mw.text.listToText.
 -- We use the default separator for mw.text.listToText, but a custom
 -- conjunction. There is also a special case conjunction if we only
 -- have two links.
 localconjunction
 if#sections==2then
 conjunction='&#8203; and '
 else
 conjunction=', and '
 end
 ret=mw.text.listToText(ret,nil,conjunction)

 -- Add the intro text.
 localintro='§§&nbsp;'
 ifisShowingPagethen
 intro=(pageortitle.prefixedText)..' '..intro
 end
 ret=intro..ret

 returnret
 end
 end

 functionp.main(frame)
 localargs=require('Module:Arguments').getArgs(frame,{
 wrappers='Template:Section link',
 valueFunc=function(key,value)
 value=value:match('^%s*(.-)%s*$')-- Trim whitespace
 -- Allow blank first parameters, as the wikitext template does this.
 ifvalue~=''orkey==1then
 returnvalue
 end
 end
 })

 -- Sort the arguments.
 localpage
 localsections,options={},{}
 fork,vinpairs(args)do
 ifk==1then
 -- Doing this in the loop because of a bug in [[Module:Arguments]]
 -- when using pairs with deleted arguments.
 page=v
 elseiftype(k)=='number'then
 sections[k]=v
 else
 options[k]=v
 end
 end

 -- Compress the sections array.
 localfunctioncompressArray(t)
 localnums,ret={},{}
 fornuminpairs(t)do
 nums[#nums+1]=num
 end
 table.sort(nums)
 fori,numinipairs(nums)do
 ret[i]=t[num]
 end
 returnret
 end
 sections=compressArray(sections)

 returnp._main(page,sections,options)
 end

 returnp

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