Module:Hatnote: Difference between revisions

m
49 revisions imported from templatewiki:Module:Hatnote
(don't categorise talk namespaces)
m (49 revisions imported from templatewiki:Module:Hatnote)
 
(21 intermediate revisions by 10 users not shown)
Line 3:
-- --
-- This module produces hatnote links and links to related articles. It --
-- implements the {{hatnote}} and {{format hatnote link}} meta-templates, and includes --
-- includes helper functions for other Lua hatnote modules. --
--------------------------------------------------------------------------------
 
Line 11:
local mArguments -- lazily initialise [[Module:Arguments]]
local yesno -- lazily initialise [[Module:Yesno]]
local mCategoryHandler -- lazily initialise [[Module:Category handler]]
 
local p = {}
Line 35 ⟶ 34:
-- 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)
Line 76 ⟶ 75:
end
 
function p.makeWikitextError(msg, demohelpLink, addTrackingCategory, title)
-- Formats an error message to be returned to wikitext. If demo is not nil
-- addTrackingCategory is not false after being returned from
-- or false, no error category is added.
-- [[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 = title or mw.title.getCurrentTitle()
mCategoryHandler = require('Module:Category handler')
-- Make the help link text.
local errorCategory = 'Hatnote templates with errors'
local colonhelpText
local errorCategoryLink = string.format(
if helpLink then
'[[%s:%s]]',
helpText = ' ([[' .. helpLink .. '|help]])'
mw.site.namespaces[14].name,
else
errorCategory
colonhelpText = ''
)
end
-- Feed the category link through [[Module:Category handler]] so we can
-- useMake itsthe blacklistcategory text.
local category
errorCategoryLink = mCategoryHandler.main{
if not title.isTalkPage and yesno(addTrackingCategory) ~= false then
talk = '', -- Don't categorise talk namespaces.
local errorCategory category = 'Hatnote templates with errors'
other = errorCategoryLink,
local errorCategoryLink category = string.format(
nocat = demo
'[[%s:%s]]',
}
mw.site.namespaces[14].name,
errorCategoryLink = errorCategoryLink or ''
category
)
else
coloncategory = ':'
end
return string.format(
'<strong class="error">Error: %s%s.</strong>%s',
msg,
helpText,
errorCategoryLink
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
 
Line 117 ⟶ 134:
local display = args[2]
if not link then
return p.makeWikitextError('no link specified')
'no link specified',
'Template:Format hatnote link#Errors',
args.category
)
end
return p._formatLink(link, display)
Line 123 ⟶ 144:
 
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 then
if not display then
colon = ':'
local prePipe, postPipe = link:match('^(.-)|(.*)$')
else
link = prePipe or link
colon = ''
display = postPipe
end
 
Line 141 ⟶ 162:
local page, section = link:match('^(.-)#(.*)$')
if page then
display = page .. ' § &nbsp;' .. section
end
end
Line 147 ⟶ 168:
-- Assemble the link.
if display then
return string.format('[[%s%s|%s]]', colon, link, display)
'[[:%s|%s]]',
string.gsub(link, '|(.*)$', ''), --display overwrites manual piping
display
)
else
return string.format('[[%s:%s]]', colon, link)
end
end
Line 164 ⟶ 189:
local options = {}
if not s then
return p.makeWikitextError('no text specified')
'no text specified',
'Template:Hatnote#Errors',
args.category
)
end
options.extraclasses = args.extraclasses
Line 174 ⟶ 203:
checkType('_hatnote', 1, s, 'string')
checkType('_hatnote', 2, options, 'table', true)
options = options or {}
local classes = {'hatnote', 'navigation-not-searchable'}
local extraclasses = options.extraclasses
local selfref = options.selfref
Line 184 ⟶ 214:
end
return string.format(
'<div role="note" class="%s">%s</div>',
table.concat(classes, ' '),
s