Documentation icon Moduldokumentation[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.                            ----------------------------------------------------------------------------------local libraryUtil = require('libraryUtil')local checkType = libraryUtil.checkTypelocal mArguments -- lazily initialise [[Module:Arguments]]local yesno -- lazily initialise [[Module:Yesno]]local p = {}---------------------------------------------------------------------------------- Helper functions--------------------------------------------------------------------------------local function getArgs(frame)-- Fetches the arguments from the parent frame. Whitespace is trimmed and-- blanks are removed.mArguments = require('Module:Arguments')return mArguments.getArgs(frame, {parentOnly = true})endlocal function removeInitialColon(s)-- Removes the initial colon from a string, if present.return s:match('^:?(.*)')endfunction p.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)if removeColon ~= false thenlink = removeInitialColon(link)endlocal namespace = link:match('^(.-):')if namespace thenlocal nsTable = mw.site.namespaces[namespace]if nsTable thenreturn nsTable.idendendreturn 0endfunction p.formatPages(...)-- Formats a list of pages using formatLink and returns it as an array. Nil-- values are not allowed.local pages = {...}local ret = {}for i, page in ipairs(pages) doret[i] = p._formatLink(page)endreturn retendfunction p.formatPageTables(...)-- Takes a list of page/display tables and returns it as a list of-- formatted links. Nil values are not allowed.local pages = {...}local links = {}for i, t in ipairs(pages) docheckType('formatPageTables', i, t, 'table')local link = t[1]local display = t[2]links[i] = p._formatLink(link, display)endreturn linksendfunction p.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')local title = mw.title.getCurrentTitle()-- Make the help link text.local helpTextif helpLink thenhelpText = ' ([[' .. helpLink .. '|help]])'elsehelpText = ''end-- Make the category text.local categoryif not title.isTalkPage and yesno(addTrackingCategory) ~= false thencategory = 'Hatnote templates with errors'category = string.format('[[%s:%s]]',mw.site.namespaces[14].name,category)elsecategory = ''endreturn string.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.--------------------------------------------------------------------------------function p.formatLink(frame)local args = getArgs(frame)local link = args[1]local display = args[2]if not link thenreturn p.makeWikitextError('no link specified','Template:Format hatnote link#Errors',args.category)endreturn p._formatLink(link, display)endfunction p._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)local namespace = p.findNamespaceId(link, false)local colonif namespace == 6 or namespace == 14 thencolon = ':'elsecolon = ''end-- Find whether a faux display value has been added with the {{!}} magic-- word.if not display thenlocal prePipe, postPipe = link:match('^(.-)|(.*)$')link = prePipe or linkdisplay = postPipeend-- Find the display value.if not display thenlocal page, section = link:match('^(.-)#(.*)$')if page thendisplay = page .. ' § ' .. sectionendend-- Assemble the link.if display thenreturn string.format('[[%s%s|%s]]', colon, link, display)elsereturn string.format('[[%s%s]]', colon, link)endend---------------------------------------------------------------------------------- Hatnote---- Produces standard hatnote text. Implements the {{hatnote}} template.--------------------------------------------------------------------------------function p.hatnote(frame)local args = getArgs(frame)local s = args[1]local options = {}if not s thenreturn p.makeWikitextError('no text specified','Template:Hatnote#Errors',args.category)endoptions.extraclasses = args.extraclassesoptions.selfref = args.selfrefreturn p._hatnote(s, options)endfunction p._hatnote(s, options)checkType('_hatnote', 1, s, 'string')checkType('_hatnote', 2, options, 'table', true)local classes = {'hatnote'}local extraclasses = options.extraclasseslocal selfref = options.selfrefif type(extraclasses) == 'string' thenclasses[#classes + 1] = extraclassesendif selfref thenclasses[#classes + 1] = 'selfref'endreturn string.format('<div class="%s">%s</div>',table.concat(classes, ' '),s)endreturn p