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 {{hatnote}} and {{format link}} meta-templates and includes --
+
-- implements the {{rellink}} and {{hatnote}} meta-templates, and includes   --
-- helper functions for other Lua hatnote modules.                           --
+
-- helper functions for other Lua modules to format hatnote links.           --
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
Line 34: Line 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 false.
+
-- trimming, set the removeColon parameter to true.
 
checkType('findNamespaceId', 1, link, 'string')
 
checkType('findNamespaceId', 1, link, 'string')
 
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
 
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
Line 56: Line 56:
 
local ret = {}
 
local ret = {}
 
for i, page in ipairs(pages) do
 
for i, page in ipairs(pages) do
ret[i] = p._formatLink(page)
+
ret[i] = p.formatLink(page)
 
end
 
end
 
return ret
 
return ret
Line 70: Line 70:
 
local link = t[1]
 
local link = t[1]
 
local display = t[2]
 
local display = t[2]
links[i] = p._formatLink(link, display)
+
links[i] = p.formatLink(link, display)
 
end
 
end
 
return links
 
return links
 
end
 
end
  
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title)
+
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
-- 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', 1, msg, 'string')
checkType('makeWikitextError', 2, helpLink, 'string', true)
 
 
yesno = require('Module:Yesno')
 
yesno = require('Module:Yesno')
title = title or mw.title.getCurrentTitle()
+
local errorCategory = 'Hatnote templates with errors'
-- Make the help link text.
+
local errorCategoryLink
local helpText
+
if yesno(demo) then
if helpLink then
+
errorCategoryLink = ''
helpText = ' ([[' .. helpLink .. '|help]])'
 
 
else
 
else
helpText = ''
+
errorCategoryLink = string.format(
end
 
-- Make the category text.
 
local category
 
if not title.isTalkPage and yesno(addTrackingCategory) ~= false then
 
category = 'Hatnote templates with errors'
 
category = string.format(
 
 
'[[%s:%s]]',
 
'[[%s:%s]]',
 
mw.site.namespaces[14].name,
 
mw.site.namespaces[14].name,
category
+
errorCategory
 
)
 
)
else
 
category = ''
 
 
end
 
end
 
return string.format(
 
return string.format(
'<strong class="error">Error: %s%s.</strong>%s',
+
'<strong class="error">Error: %s.</strong>%s',
 
msg,
 
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
 
end
  
Line 125: Line 103:
 
-- 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 "#". Used in
+
-- with " § " as a separator rather than the standard MediaWiki "#".
-- the {{format hatnote link}} template.
 
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
Line 134: Line 111:
 
local display = args[2]
 
local display = args[2]
 
if not link then
 
if not link then
return p.makeWikitextError(
+
return p.makeWikitextError('no link specified')
'no link specified',
 
'Template:Format hatnote link#Errors',
 
args.category
 
)
 
 
end
 
end
 
return p._formatLink(link, display)
 
return p._formatLink(link, display)
Line 144: Line 117:
  
 
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)
-- 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
 
end
  
Line 162: Line 135:
 
local page, section = link:match('^(.-)#(.*)$')
 
local page, section = link:match('^(.-)#(.*)$')
 
if page then
 
if page then
display = page .. ' §&nbsp;' .. section
+
display = page .. ' § ' .. section
 
end
 
end
 
end
 
end
Line 168: Line 141:
 
-- Assemble the link.
 
-- Assemble the link.
 
if display then
 
if display then
return string.format(
+
return string.format('[[%s%s|%s]]', colon, link, display)
'[[:%s|%s]]',
 
string.gsub(link, '|(.*)$', ''), --display overwrites manual piping
 
display
 
)
 
 
else
 
else
return string.format('[[:%s]]', link)
+
return string.format('[[%s%s]]', colon, link)
 
end
 
end
 
end
 
end
Line 189: Line 158:
 
local options = {}
 
local options = {}
 
if not s then
 
if not s then
return p.makeWikitextError(
+
return p.makeWikitextError('no text specified')
'no text specified',
 
'Template:Hatnote#Errors',
 
args.category
 
)
 
 
end
 
end
 
options.extraclasses = args.extraclasses
 
options.extraclasses = args.extraclasses
Line 203: Line 168:
 
checkType('_hatnote', 1, s, 'string')
 
checkType('_hatnote', 1, s, 'string')
 
checkType('_hatnote', 2, options, 'table', true)
 
checkType('_hatnote', 2, options, 'table', true)
options = options or {}
+
local classes = {'rellink'}
local classes = {'hatnote', 'navigation-not-searchable'}
 
 
local extraclasses = options.extraclasses
 
local extraclasses = options.extraclasses
 
local selfref = options.selfref
 
local selfref = options.selfref
Line 214: Line 178:
 
end
 
end
 
return string.format(
 
return string.format(
'<div role="note" class="%s">%s</div>',
+
'<div class="%s">%s</div>',
 
table.concat(classes, ' '),
 
table.concat(classes, ' '),
 
s
 
s

Please note that all contributions to MINR.ORG WIKI may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see MINR.ORG WIKI:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)