Open main menu

Changes

Module:Hatnote

748 bytes added, 01:33, 10 September 2019
m
46 revisions imported
-- --
-- This module produces hatnote links and links to related articles. It --
-- implements the {{rellinkhatnote}} and {{hatnoteformat link}} meta-templates, and includes ---- helper functions for other Lua hatnote modules to format hatnote links. --
--------------------------------------------------------------------------------
local mArguments -- lazily initialise [[Module:Arguments]]
local yesno -- lazily initialise [[Module:Yesno]]
local mCategoryHandler -- lazily initialise [[Module:Category handler]]
local p = {}
-- 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 truefalse.
checkType('findNamespaceId', 1, link, 'string')
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
end
function p.makeWikitextError(msg, demohelpLink, addTrackingCategory, title) -- Formats an error message to be returned to wikitext. If demo -- addTrackingCategory is not nilfalse after being returned from -- or false[[Module:Yesno]], and if we are not on a talk page, no error a tracking category -- is added.
checkType('makeWikitextError', 1, msg, 'string')
checkType('makeWikitextError', 2, helpLink, 'string', true)
yesno = require('Module:Yesno')
mCategoryHandler title = requiretitle or mw.title.getCurrentTitle() -- Make the help link text. local helpText if helpLink then helpText = ' ([['Module:Category handler.. helpLink .. '|help]])' else helpText = '' end -- Make the category text. local errorCategory category if not title.isTalkPage and yesno(addTrackingCategory) ~= false then category = 'Hatnote templates with errors' local errorCategoryLink category = string.format( '[[%s:%s]]', mw.site.namespaces[14].name, category errorCategory ) -- Feed the category link through [[Module:Category handler]] so we can -- use its blacklist. errorCategoryLink = mCategoryHandler.main{else all category = errorCategoryLink, nocat = demo }'' errorCategoryLink = errorCategoryLink or ''end
return string.format(
'<strong class="error">Error: %s%s.</strong>%s',
msg,
errorCategoryLinkhelpText, category
)
end
 
function p.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 = disambiguator or 'disambiguation'
return string.format('%s (%s)', page, disambiguator)
end
-- 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.
--------------------------------------------------------------------------------
local display = args[2]
if not link then
return p.makeWikitextError( 'no link specified', 'Template:Format hatnote link#Errors', args.category )
end
return p._formatLink(link, display)
function 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)
 
-- Remove the initial colon for links where it was specified manually.
link = removeInitialColon(link)
  local namespace = p.findNamespaceId(link, false)-- Find whether a faux display value has been added with the {{!}} magic local colon-- word. if namespace == 6 or namespace == 14 not display then colon local prePipe, postPipe = link:match(':^(.-)|(.*)$') else link = prePipe or link colon display = ''postPipe
end
local page, section = link:match('^(.-)#(.*)$')
if page then
display = page .. ' § &nbsp;' .. section
end
end
-- Assemble the link.
if display then
return string.format( '[[%s:%s|%s]]', colon string.gsub(link, link'|(.*)$', ''), --display overwrites manual piping display )
else
return string.format('[[%s:%s]]', colon, link)
end
end
local options = {}
if not s then
return p.makeWikitextError( 'no text specified', 'Template:Hatnote#Errors', args.category )
end
options.extraclasses = args.extraclasses
checkType('_hatnote', 1, s, 'string')
checkType('_hatnote', 2, options, 'table', true)
options = options or {} local classes = {'rellinkhatnote', 'navigation-not-searchable'}
local extraclasses = options.extraclasses
local selfref = options.selfref
end
return string.format(
'<div role="note" class="%s">%s</div>',
table.concat(classes, ' '),
s