Spring til indhold

Modul:Hatnote

Page semibeskyttet
Fra Wikipedia, den frie encyklopædi
Documentation iconModuldokumentation[opret]
--------------------------------------------------------------------------------
-- 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
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 true.
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(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,display)
end
returnlinks
end

functionp.makeWikitextError(msg,helpLink,addTrackingCategory)
-- 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')
localtitle=mw.title.getCurrentTitle()
-- Make the help link text.
localhelpText
ifhelpLinkthen
helpText=' ([['..helpLink..'|help]])'
else
helpText=''
end
-- Make the category text.
localcategory
ifnottitle.isTalkPageandyesno(addTrackingCategory)~=falsethen
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

--------------------------------------------------------------------------------
-- 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 hatnote link}} template.
--------------------------------------------------------------------------------

functionp.formatLink(frame)
localargs=getArgs(frame)
locallink=args[1]
localdisplay=args[2]
ifnotlinkthen
returnp.makeWikitextError(
'no link specified',
'Template:Format hatnote link#Errors',
args.category
)
end
returnp._formatLink(link,display)
end

functionp._formatLink(link,display)
-- Find whether we need to use the colon trick or not. We need to use the
-- colon trick for categories and files, as otherwise category links
-- categorise the page and file links display the file.
checkType('_formatLink',1,link,'string')
checkType('_formatLink',2,display,'string',true)
link=removeInitialColon(link)
localnamespace=p.findNamespaceId(link,false)
localcolon
ifnamespace==6ornamespace==14then
colon=':'
else
colon=''
end

-- Find whether a faux display value has been added with the {{!}} magic
-- word.
ifnotdisplaythen
localprePipe,postPipe=link:match('^(.-)|(.*)$')
link=prePipeorlink
display=postPipe
end

-- Find the display value.
ifnotdisplaythen
localpage,section=link:match('^(.-)#(.*)$')
ifpagethen
display=page..' § '..section
end
end

-- Assemble the link.
ifdisplaythen
returnstring.format('[[%s%s|%s]]',colon,link,display)
else
returnstring.format('[[%s%s]]',colon,link)
end
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)
localclasses={'hatnote'}
localextraclasses=options.extraclasses
localselfref=options.selfref
iftype(extraclasses)=='string'then
classes[#classes+1]=extraclasses
end
ifselfrefthen
classes[#classes+1]='selfref'
end
returnstring.format(
'<div class= "%s" >%s</div>',
table.concat(classes,' '),
s
)
end

returnp