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 a few of    --
-- helper functions for other Lua hatnote modules.                           --
+
-- the more popular templates they depend on, including {{main}},            --
 +
-- {{see also}}, {{further}} and {{details}}.                                 --
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
local libraryUtil = require('libraryUtil')
+
local mTableTools = require('Module:TableTools')
local checkType = libraryUtil.checkType
+
local mArguments = require('Module:Arguments')
local mArguments -- lazily initialise [[Module:Arguments]]
 
local yesno -- lazily initialise [[Module:Yesno]]
 
 
 
local p = {}
 
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
-- Helper functions
+
-- Argument processing
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
local function getArgs(frame)
+
--[[
-- Fetches the arguments from the parent frame. Whitespace is trimmed and
+
-- The p table is for functions to be returned from #invoke, and for functions
-- blanks are removed.
+
-- to be used from other Lua modules. The f table is for functions acting as a
mArguments = require('Module:Arguments')
+
-- bridge between #invoke functions and Lua module functions. #invoke functions
return mArguments.getArgs(frame, {parentOnly = true})
+
-- are connected to f table functions through the makeInvokeFunction function.
 +
-- Functions for use from other Lua modules have names beginning with an
 +
-- underscore.
 +
--]]
 +
local p, f = {}, {}
 +
 
 +
local function makeInvokeFunction(func)
 +
return function(frame)
 +
local args = mArguments.getArgs(frame, {parentOnly = true})
 +
return func(args)
 +
end
 
end
 
end
  
local function removeInitialColon(s)
+
--------------------------------------------------------------------------------
-- Removes the initial colon from a string, if present.
+
-- Helper functions
return s:match('^:?(.*)')
+
--------------------------------------------------------------------------------
end
 
  
function p.findNamespaceId(link, removeColon)
+
local function findNamespaceId(link)
 
-- 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 or if
-- are trimmed from the start of the link by default. To skip colon
+
-- the link has been escaped with the colon trick.
-- trimming, set the removeColon parameter to false.
 
checkType('findNamespaceId', 1, link, 'string')
 
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
 
if removeColon ~= false then
 
link = removeInitialColon(link)
 
end
 
 
local namespace = link:match('^(.-):')
 
local namespace = link:match('^(.-):')
 
if namespace then
 
if namespace then
Line 50: Line 50:
 
end
 
end
  
function p.formatPages(...)
+
local function formatLink(link, display)
 +
-- 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 " § " as a separator rather than the standard
 +
-- MediaWiki "#".
 +
 
 +
-- 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.
 +
local namespace = findNamespaceId(link)
 +
local colon
 +
if namespace == 6 or namespace == 14 then
 +
colon = ':'
 +
else
 +
colon = ''
 +
end
 +
 
 +
-- Find the display value.
 +
if not display then
 +
local page, section = link:match('^(.-)#(.*)$')
 +
if page then
 +
display = page .. ' § ' .. section
 +
end
 +
end
 +
 
 +
-- Assemble the link.
 +
if display then
 +
return string.format('[[%s%s|%s]]', colon, link, display)
 +
else
 +
return string.format('[[%s%s]]', colon, link)
 +
end
 +
end
 +
 
 +
local function 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 56: Line 89:
 
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] = formatLink(page)
 
end
 
end
 
return ret
 
return ret
 
end
 
end
 +
  
function p.formatPageTables(...)
+
local function makeWikitextError(msg)
-- Takes a list of page/display tables and returns it as a list of
+
-- Formats an error message to be returned to wikitext.
-- formatted links. Nil values are not allowed.
+
return string.format('<strong class="error">Error: %s.</strong>', msg)
local pages = {...}
+
end
local links = {}
+
 
for i, t in ipairs(pages) do
+
--------------------------------------------------------------------------------
checkType('formatPageTables', i, t, 'table')
+
-- Hatnote
local link = t[1]
+
--
local display = t[2]
+
-- Produces standard hatnote text. Implements the {{hatnote}} template.
links[i] = p._formatLink(link, display)
+
--------------------------------------------------------------------------------
 +
 
 +
function p._hatnote(s)
 +
return string.format('<div class="dablink">%s</div>', s)
 +
end
 +
 
 +
function f.hatnote(args)
 +
local s = args[1]
 +
if not s then
 +
return makeWikitextError('no text specified')
 
end
 
end
return links
+
return p._hatnote(s)
 
end
 
end
  
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title)
+
p.hatnote = makeInvokeFunction(f.hatnote)
-- Formats an error message to be returned to wikitext. If
+
 
-- addTrackingCategory is not false after being returned from
+
--------------------------------------------------------------------------------
-- [[Module:Yesno]], and if we are not on a talk page, a tracking category
+
-- Rellink
-- is added.
+
--
checkType('makeWikitextError', 1, msg, 'string')
+
-- Produces a standard link to a related article. Implements the {{rellink}}
checkType('makeWikitextError', 2, helpLink, 'string', true)
+
-- template.
yesno = require('Module:Yesno')
+
--------------------------------------------------------------------------------
title = title or mw.title.getCurrentTitle()
+
 
-- Make the help link text.
+
function p._rellink(s, extraclasses)
local helpText
+
if extraclasses then
if helpLink then
+
extraclasses = ' ' .. extraclasses
helpText = ' ([[' .. helpLink .. '|help]])'
 
 
else
 
else
helpText = ''
+
extraclasses = ''
 
end
 
end
-- Make the category text.
+
return string.format('<div class="rellink%s">%s</div>', extraclasses, s)
local category
+
end
if not title.isTalkPage and yesno(addTrackingCategory) ~= false then
+
 
category = 'Hatnote templates with errors'
+
function f.rellink(args)
category = string.format(
+
local s = args[1]
'[[%s:%s]]',
+
local extraclasses = args.extraclasses
mw.site.namespaces[14].name,
+
if not s then
category
+
return makeWikitextError('no text specified')
)
 
else
 
category = ''
 
 
end
 
end
return string.format(
+
return p._rellink(s, extraclasses)
'<strong class="error">Error: %s%s.</strong>%s',
 
msg,
 
helpText,
 
category
 
)
 
 
end
 
end
  
function p.disambiguate(page, disambiguator)
+
p.rellink = makeInvokeFunction(f.rellink)
-- 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
 
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
-- Format link
+
-- Details
 
--
 
--
-- Makes a wikilink from the given link and display values. Links are escaped
+
-- Produces a "For more details on this topic" link. the first parameter is the
-- with colons if necessary, and links to sections are detected and displayed
+
-- page linked to, and if the second parameter is present it is used instead
-- with " § " as a separator rather than the standard MediaWiki "#". Used in
+
-- of the "this topic" text.
-- the {{format hatnote link}} template.
 
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
function p.formatLink(frame)
+
function p._details(page, topic)
local args = getArgs(frame)
+
page = formatLink(page)
local link = args[1]
+
topic = topic or 'this topic'
local display = args[2]
+
local text = string.format('For more details on %s, see %s.', topic, page)
if not link then
+
local extraclasses = 'boilerplate seealso'
return p.makeWikitextError(
+
return p._rellink(text, extraclasses)
'no link specified',
+
end
'Template:Format hatnote link#Errors',
+
 
args.category
+
function f.details(args)
)
+
local page = args[1]
 +
local topic = args[2]
 +
if not page then
 +
return makeWikitextError('no page specified')
 
end
 
end
return p._formatLink(link, display)
+
return p._details(page, topic)
 
end
 
end
  
function p._formatLink(link, display)
+
p.details = makeInvokeFunction(f.details)
checkType('_formatLink', 1, link, 'string')
 
checkType('_formatLink', 2, display, 'string', true)
 
  
-- Remove the initial colon for links where it was specified manually.
+
--------------------------------------------------------------------------------
link = removeInitialColon(link)
+
-- Further
 +
--
 +
-- Produces a "Further information: a, b and c" link. It accepts an unlimited
 +
-- number of positional parameters, each of which is a page name.
 +
--------------------------------------------------------------------------------
  
-- Find whether a faux display value has been added with the {{!}} magic
+
function p._further(...)
-- word.
+
local links = formatPages(...)
if not display then
+
local text = 'Further information: ' .. mw.text.listToText(links)
local prePipe, postPipe = link:match('^(.-)|(.*)$')
+
return p._rellink(text)
link = prePipe or link
+
end
display = postPipe
 
end
 
 
 
-- Find the display value.
 
if not display then
 
local page, section = link:match('^(.-)#(.*)$')
 
if page then
 
display = page .. ' §&nbsp;' .. section
 
end
 
end
 
  
-- Assemble the link.
+
function f.further(args)
if display then
+
local pages = mTableTools.compressSparseArray(args)
return string.format(
+
if #pages < 1 then
'[[:%s|%s]]',
+
return makeWikiTextError('no pages specified')
string.gsub(link, '|(.*)$', ''), --display overwrites manual piping
 
display
 
)
 
else
 
return string.format('[[:%s]]', link)
 
 
end
 
end
 +
return p._further(unpack(pages))
 
end
 
end
 +
 +
p.further = makeInvokeFunction(f.further)
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
-- Hatnote
+
-- Main
 
--
 
--
-- Produces standard hatnote text. Implements the {{hatnote}} template.
+
-- Produces a link to a main article or articles. If used in category or
 +
-- category talk space, produces "The main article for this category is xxx".
 +
-- Otherwise, produces "Main article: xxx". Accepts a table of arguments.
 +
-- Numerical keys for this table are the page names. If the first page name is
 +
-- not in mainspace, uses "page" instead of "article". If more than one page is
 +
-- specified, the function uses plural forms. Display names can be specified for
 +
-- each page name by using the arguments l1, l2, etc.
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
function p.hatnote(frame)
+
function p._main(args)
local args = getArgs(frame)
+
-- Initialize variables.
local s = args[1]
+
local links, firstPage
local options = {}
+
local currentTitle = mw.title.getCurrentTitle()
if not s then
+
 
return p.makeWikitextError(
+
-- Make the list of formatted links and find the link for the first page.
'no text specified',
+
local nums = mTableTools.numKeys(args)
'Template:Hatnote#Errors',
+
if nums[1] then
args.category
+
firstPage = args[nums[1]]
)
+
links = {}
 +
else
 +
firstPage = currentTitle.text
 +
links = {formatLink(firstPage)}
 +
end
 +
for i, num in ipairs(nums) do
 +
local link = args[num]
 +
local display = args['l' .. tostring(num)]
 +
links[#links + 1] = formatLink(link, display)
 
end
 
end
options.extraclasses = args.extraclasses
 
options.selfref = args.selfref
 
return p._hatnote(s, options)
 
end
 
  
function p._hatnote(s, options)
+
-- Find the pagetype.
checkType('_hatnote', 1, s, 'string')
+
local firstPageNs = findNamespaceId(firstPage)
checkType('_hatnote', 2, options, 'table', true)
+
local pagetype = firstPageNs == 0 and 'article' or 'page'
options = options or {}
+
 
local classes = {'hatnote', 'navigation-not-searchable'}
+
-- Build the text.
local extraclasses = options.extraclasses
+
local isPlural = #links > 1
local selfref = options.selfref
+
local currentNs = currentTitle.namespace
if type(extraclasses) == 'string' then
+
local isCategoryNamespace = currentNs - currentNs % 2 == 14
classes[#classes + 1] = extraclasses
+
links = mw.text.listToText(links)
 +
local stringToFormat
 +
if isCategoryNamespace then
 +
if isPlural then
 +
stringToFormat = 'The main %ss for this'
 +
.. ' [[Wikipedia:Categorization|category]] are %s'
 +
else
 +
stringToFormat = 'The main %s for this'
 +
.. ' [[Wikipedia:Categorization|category]] is %s'
 +
end
 +
else
 +
if isPlural then
 +
stringToFormat = 'Main %ss: %s'
 +
else
 +
stringToFormat = 'Main %s: %s'
 +
end
 
end
 
end
if selfref then
+
local text = string.format(stringToFormat, pagetype, links)
classes[#classes + 1] = 'selfref'
+
 
end
+
-- Pass the text to p._rellink.
return string.format(
+
local extraclasses = 'relarticle mainarticle'
'<div role="note" class="%s">%s</div>',
+
return p._rellink(text, extraclasses)
table.concat(classes, ' '),
 
s
 
)
 
 
end
 
end
 +
 +
p.main = makeInvokeFunction(p._main)
  
 
return p
 
return p

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)