Modul:Hatnote

---------------------------------------------------------------------------------- Module:Hatnote ---- ---- This module produces hatnote links and links to related articles. It ---- implements the {{hatnote}} and {{format hatnote link}} meta-templates, and ---- includes helper functions for other Lua hatnote modules. ----------------------------------------------------------------------------------locallibraryUtil=require('libraryUtil')localcheckType=libraryUtil.checkTypelocalmArguments-- 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})endlocalfunctionremoveInitialColon(s)-- Removes the initial colon from a string, if present.returns:match('^:?(.*)')endfunctionp.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~=falsethenlink=removeInitialColon(link)endlocalnamespace=link:match('^(.-):')ifnamespacethenlocalnsTable=mw.site.namespaces[namespace]ifnsTablethenreturnnsTable.idendendreturn0endfunctionp.formatPages(...)-- Formats a list of pages using formatLink and returns it as an array. Nil-- values are not allowed.localpages={...}localret={}fori,pageinipairs(pages)doret[i]=p._formatLink(page)endreturnretendfunctionp.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)docheckType('formatPageTables',i,t,'table')locallink=t[1]localdisplay=t[2]links[i]=p._formatLink(link,display)endreturnlinksendfunctionp.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.localhelpTextifhelpLinkthenhelpText=' ([['..helpLink..'|help]])'elsehelpText=''end-- Make the category text.localcategoryifnottitle.isTalkPageandyesno(addTrackingCategory)~=falsethencategory='Hatnote templates with errors'category=string.format('[[%s:%s]]',mw.site.namespaces[14].name,category)elsecategory=''endreturnstring.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]ifnotlinkthenreturnp.makeWikitextError('no link specified','Template:Format hatnote link#Errors',args.category)endreturnp._formatLink(link,display)endfunctionp._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)localcolonifnamespace==6ornamespace==14thencolon=':'elsecolon=''end-- Find the display value.ifnotdisplaythenlocalpage,section=link:match('^(.-)#(.*)$')ifpagethendisplay=page..' § '..sectionendend-- Assemble the link.ifdisplaythenreturnstring.format('[[%s%s|%s]]',colon,link,display)elsereturnstring.format('[[%s%s]]',colon,link)endend---------------------------------------------------------------------------------- Hatnote---- Produces standard hatnote text. Implements the {{hatnote}} template.--------------------------------------------------------------------------------functionp.hatnote(frame)localargs=getArgs(frame)locals=args[1]localoptions={}ifnotsthenreturnp.makeWikitextError('no text specified','Template:Hatnote#Errors',args.category)endoptions.extraclasses=args.extraclassesoptions.selfref=args.selfrefreturnp._hatnote(s,options)endfunctionp._hatnote(s,options)checkType('_hatnote',1,s,'string')checkType('_hatnote',2,options,'table',true)localclasses={'hatnote'}localextraclasses=options.extraclasseslocalselfref=options.selfrefiftype(extraclasses)=='string'thenclasses[#classes+1]=extraclassesendifselfrefthenclasses[#classes+1]='selfref'endreturnstring.format('<div class="%s">%s</div>',table.concat(classes,' '),s)endreturnp