Jump to content

Module:Section link

From Meta, a Wikimedia project coordination wiki
Module documentation

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

Usage

[edit]

From wikitext

[edit]

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

From Lua

[edit]

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

[edit]
mSectionLink('Paris'){{section link|Paris}}Paris § Notes
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

Source

[edit]

Copied from en:Module:Section link.


-- 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,"{","&#x7B;")section=string.gsub(section,"}","&#x7D;")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''localsection=sections[1]or'Notes'localdisplay='§&nbsp;'..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='&#8203; and 'elseconjunction=', and 'endret=mw.text.listToText(ret,nil,conjunction)-- Add the intro text.localintro='§§&nbsp;'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)returnp._main(page,sections,options)endreturnp
close