Editing Module:Hatnote
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 3: | Line 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 {{ | -- implements the {{rellink}} and {{hatnote}} meta-templates, and includes -- | ||
-- helper functions for other Lua hatnote | -- helper functions for other Lua modules to format hatnote links. -- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 10: | Line 10: | ||
local checkType = libraryUtil.checkType | local checkType = libraryUtil.checkType | ||
local mArguments -- lazily initialise [[Module:Arguments]] | local mArguments -- lazily initialise [[Module:Arguments]] | ||
local p = {} | local p = {} | ||
Line 30: | Line 29: | ||
end | end | ||
function p. | function p._findNamespaceId(link, removeColon) | ||
-- Finds the namespace id (namespace number) of a link or a pagename. This | -- 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. | -- function will not work if the link is enclosed in double brackets. If the | ||
-- | -- removeColon parameter is set to true, the function will remove initial | ||
-- | -- colons from the link. | ||
checkType(' | checkType('_findNamespaceId', 1, link, 'string') | ||
checkType(' | checkType('_findNamespaceId', 2, removeColon, 'boolean', true) | ||
if removeColon | if removeColon then | ||
link = removeInitialColon(link) | link = removeInitialColon(link) | ||
end | end | ||
Line 50: | Line 49: | ||
end | end | ||
function p. | function p._formatPages(...) | ||
-- Formats a list of pages using formatLink and returns it as an array. Nil | -- Formats a list of pages using formatLink and returns it as an array. Nil | ||
-- values are not allowed. | -- values are not allowed. | ||
Line 61: | Line 60: | ||
end | end | ||
function p. | function p._formatPageTables(pages) | ||
-- Takes a list of page/display tables and returns it as a list of | -- Takes a list of page/display tables and returns it as a list of | ||
-- formatted links. Nil values are not allowed. | -- formatted links. Nil values are not allowed. | ||
checkType('_formatPageTables', 1, pages, 'table') | |||
local links = {} | local links = {} | ||
for i, t in ipairs(pages) do | for i, t in ipairs(pages) do | ||
local link = t[1] | local link = t[1] | ||
local display = t[2] | local display = t[2] | ||
Line 75: | Line 73: | ||
end | end | ||
function p. | function p._makeWikitextError(msg) | ||
-- Formats an error message to be returned to wikitext | -- Formats an error message to be returned to wikitext. | ||
checkType('_makeWikitextError', 1, msg, 'string') | |||
return string.format('<strong class="error">Error: %s.</strong>', msg) | |||
checkType(' | |||
return string.format( | |||
end | end | ||
Line 125: | Line 84: | ||
-- Makes a wikilink from the given link and display values. Links are escaped | -- 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 colons if necessary, and links to sections are detected and displayed | ||
-- with " § " as a separator rather than the standard MediaWiki "#" | -- with " § " as a separator rather than the standard MediaWiki "#". | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 134: | Line 92: | ||
local display = args[2] | local display = args[2] | ||
if not link then | if not link then | ||
return p. | return p._makeWikitextError('no link specified') | ||
end | end | ||
return p._formatLink(link, display) | return p._formatLink(link, display) | ||
Line 144: | Line 98: | ||
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) | ||
link = removeInitialColon(link) | link = removeInitialColon(link) | ||
local namespace = p._findNamespaceId(link) | |||
local colon | |||
if namespace == 6 or namespace == 14 then | |||
if | colon = ':' | ||
else | |||
colon = '' | |||
end | end | ||
Line 162: | Line 116: | ||
local page, section = link:match('^(.-)#(.*)$') | local page, section = link:match('^(.-)#(.*)$') | ||
if page then | if page then | ||
display = page .. ' § | display = page .. ' § ' .. section | ||
end | end | ||
end | end | ||
Line 168: | Line 122: | ||
-- Assemble the link. | -- Assemble the link. | ||
if display then | if display then | ||
return string.format( | return string.format('[[%s%s|%s]]', colon, link, display) | ||
else | else | ||
return string.format('[[ | return string.format('[[%s%s]]', colon, link) | ||
end | end | ||
end | end | ||
Line 187: | Line 137: | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local s = args[1] | local s = args[1] | ||
if not s then | if not s then | ||
return p. | return p._makeWikitextError('no text specified') | ||
end | end | ||
return p._hatnote(s) | |||
return p._hatnote(s | |||
end | end | ||
function p._hatnote(s | function p._hatnote(s) | ||
checkType('_hatnote', 1, s, 'string') | checkType('_hatnote', 1, s, 'string') | ||
return string.format('<div class="dablink">%s</div>', s) | |||
end | |||
local | |||
local | -------------------------------------------------------------------------------- | ||
local | -- Rellink | ||
if | -- | ||
-- Produces a standard link to a related article. Implements the {{rellink}} | |||
-- template. | |||
-------------------------------------------------------------------------------- | |||
function p.rellink(frame) | |||
local args = getArgs(frame) | |||
local s = args[1] | |||
local extraclasses = args.extraclasses | |||
if not s then | |||
return p._makeWikitextError('no text specified') | |||
end | end | ||
if | return p._rellink(s, extraclasses) | ||
end | |||
function p._rellink(s, extraclasses) | |||
checkType('_rellink', 1, s, 'string') | |||
checkType('_rellink', 2, extraclasses, 'string', true) | |||
if extraclasses then | |||
extraclasses = ' ' .. extraclasses | |||
else | |||
extraclasses = '' | |||
end | end | ||
return string.format( | return string.format('<div class="rellink%s">%s</div>', extraclasses, s) | ||
end | end | ||
return p | return p |