Jump to content
Wikimedia Meta-Wiki

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.


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.
Please add categories to the /doc subpage. 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''
 localsection=sections[1]or'Notes'
 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)

 returnp._main(page,sections,options)
 end

 returnp

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