Editing Module:Hatnote
From MINR.ORG WIKI
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save 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. 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 | + | -- trimming, set the removeColon parameter to true. |
− | checkType(' | + | checkType('_findNamespaceId', 1, link, 'string') |
− | checkType(' | + | checkType('_findNamespaceId', 2, removeColon, 'boolean', true) |
if removeColon ~= false then | if removeColon ~= false then | ||
link = removeInitialColon(link) | link = removeInitialColon(link) | ||
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(...) |
-- 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. | ||
Line 67: | Line 66: | ||
local links = {} | local links = {} | ||
for i, t in ipairs(pages) do | for i, t in ipairs(pages) do | ||
− | checkType(' | + | checkType('_formatPageTables', i, t, 'table') |
local link = t[1] | local link = t[1] | ||
local display = t[2] | local display = t[2] | ||
Line 75: | Line 74: | ||
end | end | ||
− | function p. | + | function p._makeWikitextError(msg, demo) |
− | -- Formats an error message to be returned to wikitext. If | + | -- Formats an error message to be returned to wikitext. If demo is not nil |
− | + | -- or false, no error category is added. | |
− | -- | + | checkType('_makeWikitextError', 1, msg, 'string') |
− | + | checkType('_makeWikitextError', 2, demo, 'boolean', true) | |
− | checkType(' | + | local errorCategory = 'Hatnote templates with errors' |
− | checkType(' | + | local errorCategoryLink |
− | + | if demo then | |
− | + | errorCategoryLink = string.format( | |
− | |||
− | local | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | local | ||
− | if | ||
− | |||
− | |||
'[[%s:%s]]', | '[[%s:%s]]', | ||
mw.site.namespaces[14].name, | mw.site.namespaces[14].name, | ||
− | + | errorCategory | |
) | ) | ||
else | else | ||
− | + | errorCategoryLink = '' | |
end | end | ||
return string.format( | return string.format( | ||
− | '<strong class="error">Error: | + | '<strong class="error">Error: %s.</strong>%s', |
msg, | msg, | ||
− | + | errorCategoryLink | |
− | |||
) | ) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 125: | Line 102: | ||
-- 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 110: | ||
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 116: | ||
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, false) | |
− | + | local colon | |
− | + | if namespace == 6 or namespace == 14 then | |
− | if | + | colon = ':' |
− | + | else | |
− | + | colon = '' | |
− | |||
end | end | ||
Line 162: | Line 134: | ||
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 140: | ||
-- 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 189: | Line 157: | ||
local options = {} | local options = {} | ||
if not s then | if not s then | ||
− | return p. | + | return p._makeWikitextError('no text specified') |
− | |||
− | |||
− | |||
− | |||
end | end | ||
options.extraclasses = args.extraclasses | options.extraclasses = args.extraclasses | ||
Line 203: | Line 167: | ||
checkType('_hatnote', 1, s, 'string') | checkType('_hatnote', 1, s, 'string') | ||
checkType('_hatnote', 2, options, 'table', true) | checkType('_hatnote', 2, options, 'table', true) | ||
− | + | local classes = {'rellink'} | |
− | local classes = {' | ||
local extraclasses = options.extraclasses | local extraclasses = options.extraclasses | ||
local selfref = options.selfref | local selfref = options.selfref | ||
Line 214: | Line 177: | ||
end | end | ||
return string.format( | return string.format( | ||
− | '<div | + | '<div class="%s">%s</div>', |
table.concat(classes, ' '), | table.concat(classes, ' '), | ||
s | s |