Jump to content
Wikipedia The Free Encyclopedia

Module:Section link

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
[画像:Warning] This Lua module is used in MediaWiki:Abusefilter-warning-notwallofshame , and on approximately 112,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 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.

This module creates links to sections, nicely formatted with the "§" symbol instead of the default "#".

Usage

From wikitext

From wikitext, this module should be used via the template {{section link }}. Please see the template page for documentation.

From Lua

First, load the module:

localmSectionLink=require('Module:Section link')

You can then make section links via the _main function.

mSectionLink._main(page,sections,options,title)

Parameters:

  • page - the page name to link to. Defaults to the full page name of title, or the current title if that is not specified.
  • sections - an array of section names to link to, or a string to link to just one section name.
  • options - a table of options. Accepts the following fields:
    • nopage - set this to true to avoid displaying the base page name in links.
  • title - a default mw.title object to use instead of the current title. Intended for testing purposes.

All parameters are optional.

Examples

Lua code Wikitext code Result
mSectionLink('Paris') {{section link|Paris}} {{Section link}}: required section parameter(s) missing
mSectionLink('Paris','Architecture') {{section link|Paris|Architecture}} Paris § Architecture
mSectionLink('Paris',{'Architecture','Culture'}) {{section link|Paris|Architecture|Culture}} Paris §§ Architecture​ and Culture
mSectionLink('Paris',{'Architecture','Culture','Sport'}) {{section link|Paris|Architecture|Culture|Sport}} Paris §§ Architecture, Culture, and Sport
mSectionLink('Paris',{'Architecture','Culture','Sport'},{nopage=true}) {{section link|Paris|Architecture|Culture|Sport|nopage=yes}} §§ Architecture, Culture, and Sport

See also

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

 -- This module implements {{section link}}.
 require('strict');

 localcheckType=require('libraryUtil').checkType

 localp={}

 localfunctionmakeSectionLink(page,section,display)
 display=displayorsection
 page=pageor''
 -- MediaWiki doesn't allow these in 'page', so only need to do for 'section'
 iftype(section)=='string'then
 section=string.gsub(section,"{","{")
 section=string.gsub(section,"}","}")
 end
 returnstring.format('[[%s#%s|%s]]',page,section,display)
 end

 localfunctionnormalizeTitle(title)
 title=mw.ustring.gsub(mw.ustring.gsub(title,"'",""),'"','')
 title=mw.ustring.gsub(title,"%b<>","")
 returnmw.title.new(title).prefixedText
 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=notoptions.nopage
 if#sections<=1then
 locallinkPage=pageor''
 ifoptions.permalinkthen-- when oldid provided
 linkPage='Special:Permalink/'..options.permalink;-- make Special:Permalink/oldid wikilink
 end
 localsection=sections[1];-- sections[1] must have a value
 localdisplay='§&nbsp;'..section
 ifisShowingPagethen
 page=pageortitle.prefixedText
 ifoptions.displayandoptions.display~=''then
 ifnormalizeTitle(options.display)==normalizeTitle(page)then
 display=options.display..' '..display
 else
 error(string.format(
 'Display title "%s" was ignored since it is '..
 "not equivalent to the page's actual title",
 options.display
 ),0)
 end
 else
 display=page..' '..display
 end
 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)
 localyesno=require('Module:Yesno')
 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
 })

 fork,vinpairs(args)do-- replace underscores in the positional parameter values
 if'number'==type(k)then
 ifnotyesno(args['keep-underscores'])then-- unless |keep-underscores=yes
 args[k]=mw.uri.decode(v,'WIKI');-- percent-decode; replace underscores with space characters
 else
 args[k]=mw.uri.decode(v,'PATH');-- percent-decode; retain underscores
 end
 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=mw.text.decode(v,true)
 elseiftype(k)=='number'then
 sections[k]=v
 else
 options[k]=v
 end
 end

 options.nopage=yesno(options.nopage);-- make boolean

 -- Extract section from page, if present
 ifpagethen
 localp,s=page:match('^(.-)#(.*)$')
 ifpthenpage,sections[1]=p,send
 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)

 ifnotsections[1]then-- empty section parameter makes {{Section link}} meaningless
 return('<span style="color:#d33"><span style="font-family:monospace">{{[[Template:Section link|Section link]]}}</span>: required section parameter(s) missing</span>');-- emit error message and abandon
 end

 returnp._main(page,sections,options)
 end

 returnp

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