Module:Hatnote: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(add type check for makeWikitextError)
(make makeWikitextError use Module:Yesno to parse the demo parameter, and remove underscores from all the function names, as that's normally done for private functions, whereas these are public)
Line 10:
local checkType = libraryUtil.checkType
local mArguments -- lazily initialise [[Module:Arguments]]
local yesno -- lazily initialise [[Module:Yesno]]
 
local p = {}
Line 29 ⟶ 30:
end
 
function p._findNamespaceIdfindNamespaceId(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('_findNamespaceIdfindNamespaceId', 1, link, 'string')
checkType('_findNamespaceIdfindNamespaceId', 2, removeColon, 'boolean', true)
if removeColon ~= false then
link = removeInitialColon(link)
Line 49 ⟶ 50:
end
 
function p._formatPagesformatPages(...)
-- Formats a list of pages using formatLink and returns it as an array. Nil
-- values are not allowed.
Line 55 ⟶ 56:
local ret = {}
for i, page in ipairs(pages) do
ret[i] = p._formatLinkformatLink(page)
end
return ret
end
 
function p._formatPageTablesformatPageTables(...)
-- Takes a list of page/display tables and returns it as a list of
-- formatted links. Nil values are not allowed.
Line 66 ⟶ 67:
local links = {}
for i, t in ipairs(pages) do
checkType('_formatPageTablesformatPageTables', i, t, 'table')
local link = t[1]
local display = t[2]
links[i] = p._formatLinkformatLink(link, display)
end
return links
end
 
function p._makeWikitextErrormakeWikitextError(msg, demo)
-- Formats an error message to be returned to wikitext. If demo is not nil
-- or false, no error category is added.
checkType('_makeWikitextErrormakeWikitextError', 1, msg, 'string')
yesno = require('Module:Yesno')
checkType('_makeWikitextError', 2, demo, 'boolean', true)
local errorCategory = 'Hatnote templates with errors'
local errorCategoryLink
if yesno(demo) then
errorCategoryLink = string.format(
'[[%s:%s]]',
Line 110 ⟶ 111:
local display = args[2]
if not link then
return p._makeWikitextErrormakeWikitextError('no link specified')
end
return p._formatLinkformatLink(link, display)
end
 
function p._formatLinkformatLink(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('_formatLinkformatLink', 1, link, 'string')
checkType('_formatLinkformatLink', 2, display, 'string', true)
link = removeInitialColon(link)
local namespace = p._findNamespaceIdfindNamespaceId(link, false)
local colon
if namespace == 6 or namespace == 14 then
Line 157 ⟶ 158:
local options = {}
if not s then
return p._makeWikitextErrormakeWikitextError('no text specified')
end
options.extraclasses = args.extraclasses