--------------------------------------------------------------------------------
-- Module:Hatnote --
-- --
-- This module produces hatnote links and links to related articles. It --
-- implements the {{hatnote}} and {{format link}} meta-templates and includes --
-- helper functions for other Lua hatnote modules. --
--------------------------------------------------------------------------------
locallibraryUtil=require('libraryUtil')
localcheckType=libraryUtil.checkType
localcheckTypeForNamedArg=libraryUtil.checkTypeForNamedArg
localmArguments-- lazily initialise [[Module:Arguments]]
localyesno-- lazily initialise [[Module:Yesno]]
localp={}
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
localfunctiongetArgs(frame)
-- Fetches the arguments from the parent frame. Whitespace is trimmed and
-- blanks are removed.
mArguments=require('Module:Arguments')
returnmArguments.getArgs(frame,{parentOnly=true})
end
localfunctionremoveInitialColon(s)
-- Removes the initial colon from a string, if present.
returns:match('^:?(.*)')
end
functionp.findNamespaceId(link,removeColon)
-- Finds the namespace id (namespace number) of a link or a pagename. This
-- function will not work if the link is enclosed in double brackets. Colons
-- are trimmed from the start of the link by default. To skip colon
-- trimming, set the removeColon parameter to false.
checkType('findNamespaceId',1,link,'string')
checkType('findNamespaceId',2,removeColon,'boolean',true)
ifremoveColon~=falsethen
link=removeInitialColon(link)
end
localnamespace=link:match('^(.-):')
ifnamespacethen
localnsTable=mw.site.namespaces[namespace]
ifnsTablethen
returnnsTable.id
end
end
return0
end
functionp.formatPages(...)
-- Formats a list of pages using formatLink and returns it as an array. Nil
-- values are not allowed.
localpages={...}
localret={}
fori,pageinipairs(pages)do
ret[i]=p._formatLink{link=page}
end
returnret
end
functionp.formatPageTables(...)
-- Takes a list of page/display tables and returns it as a list of
-- formatted links. Nil values are not allowed.
localpages={...}
locallinks={}
fori,tinipairs(pages)do
checkType('formatPageTables',i,t,'table')
locallink=t[1]
localdisplay=t[2]
links[i]=p._formatLink{link=link,display=display}
end
returnlinks
end
functionp.makeWikitextError(msg,helpLink,addTrackingCategory,title)
-- Formats an error message to be returned to wikitext. If
-- addTrackingCategory is not false after being returned from
-- [[Module:Yesno]], and if we are not on a talk page, a tracking category
-- is added.
checkType('makeWikitextError',1,msg,'string')
checkType('makeWikitextError',2,helpLink,'string',true)
yesno=require('Module:Yesno')
title=titleormw.title.getCurrentTitle()
-- Make the help link text.
localhelpText
ifhelpLinkthen
helpText=' ([['..helpLink..'|help]])'
else
helpText=''
end
-- Make the category text.
localcategory
ifnottitle.isTalkPage-- Don't categorise talk pages
andtitle.namespace~=2-- Don't categorise userspace
andyesno(addTrackingCategory)~=false-- Allow opting out
then
category='Hatnote templates with errors'
category=string.format(
'[[%s:%s]]',
mw.site.namespaces[14].name,
category
)
else
category=''
end
returnstring.format(
'<strong class="error">Error: %s%s.</strong>%s',
msg,
helpText,
category
)
end
functionp.disambiguate(page,disambiguator)
-- Formats a page title with a disambiguation parenthetical,
-- i.e. "Example" → "Example (disambiguation)".
checkType('disambiguate',1,page,'string')
checkType('disambiguate',2,disambiguator,'string',true)
disambiguator=disambiguatoror'disambiguation'
returnstring.format('%s (%s)',page,disambiguator)
end
--------------------------------------------------------------------------------
-- Format link
--
-- Makes a wikilink from the given link and display values. Links are escaped
-- with colons if necessary, and links to sections are detected and displayed
-- with " § " as a separator rather than the standard MediaWiki "#". Used in
-- the {{format link}} template.
--------------------------------------------------------------------------------
functionp.formatLink(frame)
-- The formatLink export function, for use in templates.
yesno=require('Module:Yesno')
localargs=getArgs(frame)
locallink=args[1]
ifnotlinkthen
returnp.makeWikitextError(
'no link specified',
'Template:Format link#Errors',
args.category
)
end
returnp._formatLink{
link=link,
display=args[2],
italicizePage=yesno(args.italicizepage),
italicizeSection=yesno(args.italicizesection),
}
end
localfunctionitalicize(s)
-- Italicize a string.
return'<i>'..s..'</i>'
end
localfunctionmaybeItalicize(s,shouldItalicize)
-- italicize s if s is a string and the shouldItalicize parameter is true.
ifsandshouldItalicizethen
returnitalicize(s)
else
returns
end
end
localfunctionparseLink(link)
-- Parse a link and return a table with the link's components.
-- These components are:
-- - link: the link, stripped of any initial colon (always present)
-- - page: the page name (always present)
-- - section: the page name (may be nil)
-- - display: the display text, if manually entered after a pipe (may be nil)
link=removeInitialColon(link)
-- Find whether a faux display value has been added with the {{!}} magic
-- word.
localprePipe,display=link:match('^(.-)|(.*)$')
link=prePipeorlink
-- Find the page, if it exists.
-- For links like [[#Bar]], the page will be nil.
localpreHash,postHash=link:match('^(.-)#(.*)$')
localpage
ifnotpreHashthen
-- We have a link like [[Foo]].
page=link
elseifpreHash~=''then
-- We have a link like [[Foo#Bar]].
page=preHash
end
-- Find the section, if it exists.
localsection
ifpostHashandpostHash~=''then
section=postHash
end
return{
link=link,
page=page,
section=section,
display=display,
}
end
functionp._formatLink(options)
-- The formatLink export function, for use in modules.
checkType('_formatLink',1,options,'table')
checkTypeForNamedArg('_formatLink','link',options.link,'string',false)
checkTypeForNamedArg(
'_formatLink',
'display',
options.display,
'string',
true
)
checkTypeForNamedArg(
'_formatLink',
'italicizePage',
options.italicizePage,
'boolean',
true
)
checkTypeForNamedArg(
'_formatLink',
'italicizeSection',
options.italicizeSection,
'boolean',
true
)
localparsed=parseLink(options.link)
localdisplay=options.displayorparsed.display
-- Deal with the case where we don't have to pipe the link
ifnotdisplayandnotparsed.sectionandnotoptions.italicizePagethen
returnstring.format('[[:%s]]',parsed.link)
end
-- Find the display text for piped links
ifnotdisplaythen
localpage=maybeItalicize(parsed.page,options.italicizePage)
localsection=maybeItalicize(parsed.section,options.italicizeSection)
ifnotpagethen
display=string.format('§ %s',section)
elseifsectionthen
display=string.format('%s § %s',page,section)
else
display=page
end
end
returnstring.format('[[:%s|%s]]',parsed.link,display)
end
--------------------------------------------------------------------------------
-- Hatnote
--
-- Produces standard hatnote text. Implements the {{hatnote}} template.
--------------------------------------------------------------------------------
functionp.hatnote(frame)
localargs=getArgs(frame)
locals=args[1]
localoptions={}
ifnotsthen
returnp.makeWikitextError(
'no text specified',
'Template:Hatnote#Errors',
args.category
)
end
options.extraclasses=args.extraclasses
options.selfref=args.selfref
returnp._hatnote(s,options)
end
functionp._hatnote(s,options)
checkType('_hatnote',1,s,'string')
checkType('_hatnote',2,options,'table',true)
options=optionsor{}
localclasses={'hatnote','navigation-not-searchable'}
localextraclasses=options.extraclasses
localselfref=options.selfref
iftype(extraclasses)=='string'then
classes[#classes+1]=extraclasses
end
ifselfrefthen
classes[#classes+1]='selfref'
end
returnstring.format(
'<div role="note" class="%s">%s</div>',
table.concat(classes,' '),
s
)
end
returnp