Modul:Hatnote: Unterschied zwischen den Versionen

K
59 Versionen von wpen:Module:Hatnote importiert
(don't categorise talk namespaces)
K (59 Versionen von wpen:Module:Hatnote importiert)
 
(35 dazwischenliegende Versionen von 12 Benutzern werden nicht angezeigt)
Zeile 3: Zeile 3:
--                                                                            --
--                                                                            --
-- This module produces hatnote links and links to related articles. It      --
-- This module produces hatnote links and links to related articles. It      --
-- implements the {{hatnote}} and {{format hatnote link}} meta-templates, and --
-- implements the {{hatnote}} and {{format link}} meta-templates and includes --
-- includes helper functions for other Lua hatnote modules.                   --
-- helper functions for other Lua hatnote modules.                           --
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Zeile 11: Zeile 11:
local mArguments -- lazily initialise [[Module:Arguments]]
local mArguments -- lazily initialise [[Module:Arguments]]
local yesno -- lazily initialise [[Module:Yesno]]
local yesno -- lazily initialise [[Module:Yesno]]
local mCategoryHandler -- lazily initialise [[Module:Category handler]]


local p = {}
local p = {}
Zeile 35: Zeile 34:
-- function will not work if the link is enclosed in double brackets. Colons
-- 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
-- are trimmed from the start of the link by default. To skip colon
-- trimming, set the removeColon parameter to true.
-- trimming, set the removeColon parameter to false.
checkType('findNamespaceId', 1, link, 'string')
checkType('findNamespaceId', 1, link, 'string')
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
Zeile 76: Zeile 75:
end
end


function p.makeWikitextError(msg, demo)
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title)
-- Formats an error message to be returned to wikitext. If demo is not nil
-- Formats an error message to be returned to wikitext. If
-- or false, no error category is added.
-- 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', 1, msg, 'string')
checkType('makeWikitextError', 2, helpLink, 'string', true)
yesno = require('Module:Yesno')
yesno = require('Module:Yesno')
mCategoryHandler = require('Module:Category handler')
title = title or mw.title.getCurrentTitle()
local errorCategory = 'Hatnote templates with errors'
-- Make the help link text.
local errorCategoryLink = string.format(
local helpText
'[[%s:%s]]',
if helpLink then
mw.site.namespaces[14].name,
helpText = ' ([[' .. helpLink .. '|help]])'
errorCategory
else
)
helpText = ''
-- Feed the category link through [[Module:Category handler]] so we can
end
-- use its blacklist.
-- Make the category text.
errorCategoryLink = mCategoryHandler.main{
local category
talk = '', -- Don't categorise talk namespaces.
if not title.isTalkPage and yesno(addTrackingCategory) ~= false then
other = errorCategoryLink,
category = 'Hatnote templates with errors'
nocat = demo
category = string.format(
}
'[[%s:%s]]',
errorCategoryLink = errorCategoryLink or ''
mw.site.namespaces[14].name,
category
)
else
category = ''
end
return string.format(
return string.format(
'<strong class="error">Error: %s.</strong>%s',
'<strong class="error">Error: %s%s.</strong>%s',
msg,
msg,
errorCategoryLink
helpText,
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
end


Zeile 117: Zeile 134:
local display = args[2]
local display = args[2]
if not link then
if not link then
return p.makeWikitextError('no link specified')
return p.makeWikitextError(
'no link specified',
'Template:Format hatnote link#Errors',
args.category
)
end
end
return p._formatLink(link, display)
return p._formatLink(link, display)
Zeile 123: Zeile 144:


function 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', 1, link, 'string')
checkType('_formatLink', 2, display, 'string', true)
checkType('_formatLink', 2, display, 'string', true)
-- Remove the initial colon for links where it was specified manually.
link = removeInitialColon(link)
link = removeInitialColon(link)
local namespace = p.findNamespaceId(link, false)
 
local colon
-- Find whether a faux display value has been added with the {{!}} magic
if namespace == 6 or namespace == 14 then
-- word.
colon = ':'
if not display then
else
local prePipe, postPipe = link:match('^(.-)|(.*)$')
colon = ''
link = prePipe or link
display = postPipe
end
end


Zeile 141: Zeile 162:
local page, section = link:match('^(.-)#(.*)$')
local page, section = link:match('^(.-)#(.*)$')
if page then
if page then
display = page .. ' § ' .. section
display = page .. ' §&nbsp;' .. section
end
end
end
end
Zeile 147: Zeile 168:
-- Assemble the link.
-- Assemble the link.
if display then
if display then
return string.format('[[%s%s|%s]]', colon, link, display)
return string.format(
'[[:%s|%s]]',
string.gsub(link, '|(.*)$', ''), --display overwrites manual piping
display
)
else
else
return string.format('[[%s%s]]', colon, link)
return string.format('[[:%s]]', link)
end
end
end
end
Zeile 164: Zeile 189:
local options = {}
local options = {}
if not s then
if not s then
return p.makeWikitextError('no text specified')
return p.makeWikitextError(
'no text specified',
'Template:Hatnote#Errors',
args.category
)
end
end
options.extraclasses = args.extraclasses
options.extraclasses = args.extraclasses
Zeile 174: Zeile 203:
checkType('_hatnote', 1, s, 'string')
checkType('_hatnote', 1, s, 'string')
checkType('_hatnote', 2, options, 'table', true)
checkType('_hatnote', 2, options, 'table', true)
local classes = {'hatnote'}
options = options or {}
local classes = {'hatnote', 'navigation-not-searchable'}
local extraclasses = options.extraclasses
local extraclasses = options.extraclasses
local selfref = options.selfref
local selfref = options.selfref
Zeile 184: Zeile 214:
end
end
return string.format(
return string.format(
'<div class="%s">%s</div>',
'<div role="note" class="%s">%s</div>',
table.concat(classes, ' '),
table.concat(classes, ' '),
s
s