Module:Section link
Appearance
![]() | This Lua module is used in MediaWiki:Abusefilter-warning-notwallofshame, and on approximately 90,000 pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, 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 form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia 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. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
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 character used is U+00A7 §SECTION SIGN (§)
-- This module implements {{section link}}.require('strict');localcheckType=require('libraryUtil').checkTypelocalp={}localfunctionmakeSectionLink(page,section,display)display=displayorsectionpage=pageor''-- MediaWiki doesn't allow these in 'page', so only need to do for 'section'iftype(section)=='string'thensection=string.gsub(section,"{","{")section=string.gsub(section,"}","}")endreturnstring.format('[[%s#%s|%s]]',page,section,display)endlocalfunctionnormalizeTitle(title)title=mw.ustring.gsub(mw.ustring.gsub(title,"'",""),'"','')title=mw.ustring.gsub(title,"%b<>","")returnmw.title.new(title).prefixedTextendfunctionp._main(page,sections,options,title)-- Validate input.checkType('_main',1,page,'string',true)checkType('_main',3,options,'table',true)ifsections==nilthensections={}elseiftype(sections)=='string'thensections={sections}elseiftype(sections)~='table'thenerror(string.format("type error in argument #2 to '_main' ".."(string, table or nil expected, got %s)",type(sections)),2)endoptions=optionsor{}title=titleormw.title.getCurrentTitle()-- Deal with blank page names elegantlyifpageandnotpage:find('%S')thenpage=niloptions.nopage=trueend-- Make the link(s).localisShowingPage=notoptions.nopageif#sections<=1thenlocallinkPage=pageor''ifoptions.permalinkthen-- when oldid providedlinkPage='Special:Permalink/'..options.permalink;-- make Special:Permalink/oldid wikilinkendlocalsection=sections[1];-- sections[1] must have a valuelocaldisplay='§ '..sectionifisShowingPagethenpage=pageortitle.prefixedTextifoptions.displayandoptions.display~=''thenifnormalizeTitle(options.display)==normalizeTitle(page)thendisplay=options.display..' '..displayelseerror(string.format('Display title "%s" was ignored since it is '.."not equivalent to the page's actual title",options.display),0)endelsedisplay=page..' '..displayendendreturnmakeSectionLink(linkPage,section,display)else-- Multiple sections. First, make a list of the links to display.localret={}fori,sectioninipairs(sections)doret[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.localconjunctionif#sections==2thenconjunction='​ and 'elseconjunction=', and 'endret=mw.text.listToText(ret,nil,conjunction)-- Add the intro text.localintro='§§ 'ifisShowingPagethenintro=(pageortitle.prefixedText)..' '..introendret=intro..retreturnretendendfunctionp.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==1thenreturnvalueendend})fork,vinpairs(args)do-- replace underscores in the positional parameter valuesif'number'==type(k)thenifnotyesno(args['keep-underscores'])then-- unless |keep-underscores=yesargs[k]=mw.uri.decode(v,'WIKI');-- percent-decode; replace underscores with space characterselseargs[k]=mw.uri.decode(v,'PATH');-- percent-decode; retain underscoresendendend-- Sort the arguments.localpagelocalsections,options={},{}fork,vinpairs(args)doifk==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'thensections[k]=velseoptions[k]=vendendoptions.nopage=yesno(options.nopage);-- make boolean-- Extract section from page, if presentifpagethenlocalp,s=page:match('^(.-)#(.*)$')ifpthenpage,sections[1]=p,sendend-- Compress the sections array.localfunctioncompressArray(t)localnums,ret={},{}fornuminpairs(t)donums[#nums+1]=numendtable.sort(nums)fori,numinipairs(nums)doret[i]=t[num]endreturnretendsections=compressArray(sections)ifnotsections[1]then-- empty section parameter makes {{Section link}} meaninglessreturn('<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 abandonendreturnp._main(page,sections,options)endreturnp