Module:Hatnote: Difference between revisions
From Chalo Chatu, Zambia online encyclopedia
Jump to navigationJump to search
http://www.chalochatu.org/>Ahecht Revert |
Chalochatu (talk | contribs) No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 21: | Line 21: | ||
local function getArgs(frame) | local function getArgs(frame) | ||
mArguments = require('Module:Arguments') | mArguments = require('Module:Arguments') | ||
return mArguments.getArgs(frame, {parentOnly = true}) | return mArguments.getArgs(frame, {parentOnly = true}) | ||
| Line 28: | Line 26: | ||
local function removeInitialColon(s) | local function removeInitialColon(s) | ||
return s:match('^:?(.*)') | return s:match('^:?(.*)') | ||
end | end | ||
function p.defaultClasses(inline) | function p.defaultClasses(inline) | ||
return (inline == 1 and 'hatnote-inline' or 'hatnote') .. ' navigation-not-searchable' | |||
return | |||
end | end | ||
function p.disambiguate(page, disambiguator) | function p.disambiguate(page, disambiguator) | ||
checkType('disambiguate', 1, page, 'string') | checkType('disambiguate', 1, page, 'string') | ||
checkType('disambiguate', 2, disambiguator, 'string', true) | checkType('disambiguate', 2, disambiguator, 'string', true) | ||
| Line 50: | Line 41: | ||
function p.findNamespaceId(link, removeColon) | function p.findNamespaceId(link, removeColon) | ||
checkType('findNamespaceId', 1, link, 'string') | checkType('findNamespaceId', 1, link, 'string') | ||
checkType('findNamespaceId', 2, removeColon, 'boolean', true) | checkType('findNamespaceId', 2, removeColon, 'boolean', true) | ||
| Line 70: | Line 57: | ||
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title) | function p.makeWikitextError(msg, helpLink, addTrackingCategory, title) | ||
checkType('makeWikitextError', 1, msg, 'string') | checkType('makeWikitextError', 1, msg, 'string') | ||
checkType('makeWikitextError', 2, helpLink, 'string', true) | checkType('makeWikitextError', 2, helpLink, 'string', true) | ||
yesno = require('Module:Yesno') | yesno = require('Module:Yesno') | ||
title = title or mw.title.getCurrentTitle() | title = title or mw.title.getCurrentTitle() | ||
local helpText | local helpText | ||
if helpLink then | if helpLink then | ||
| Line 85: | Line 68: | ||
helpText = '' | helpText = '' | ||
end | end | ||
local category | local category | ||
if not title.isTalkPage | if not title.isTalkPage | ||
and title.namespace ~= 2 | and title.namespace ~= 2 | ||
and yesno(addTrackingCategory) ~= false | and yesno(addTrackingCategory) ~= false | ||
then | then | ||
category = 'Hatnote templates with errors' | category = 'Hatnote templates with errors' | ||
| Line 100: | Line 83: | ||
category = '' | category = '' | ||
end | end | ||
return mw.ustring.format( | return mw.ustring.format( | ||
'<strong class="error">Error: %s%s.</strong>%s', | '<strong class="error">Error: %s%s.</strong>%s', | ||
| Line 110: | Line 94: | ||
local curNs = mw.title.getCurrentTitle().namespace | local curNs = mw.title.getCurrentTitle().namespace | ||
p.missingTargetCat = | p.missingTargetCat = | ||
((curNs == 0) or (curNs == 14)) and | ((curNs == 0) or (curNs == 14)) and | ||
'Articles with hatnote templates targeting a nonexistent page' or nil | 'Articles with hatnote templates targeting a nonexistent page' or nil | ||
function p.quote(title) | function p.quote(title) | ||
local quotationMarks = { | local quotationMarks = { | ||
["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true | ["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true | ||
} | } | ||
local quoteLeft, quoteRight = | local quoteLeft, quoteRight = | ||
quotationMarks[string.sub(title, 1, 1)], | quotationMarks[string.sub(title, 1, 1)], | ||
quotationMarks[string.sub(title, -1, -1)] | quotationMarks[string.sub(title, -1, -1)] | ||
| Line 133: | Line 114: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Hatnote | -- Hatnote | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
p[''] = function (frame) return p.hatnote(frame:newChild{ title = "Template:Hatnote" }) end | |||
p[''] = function (frame) | |||
return p.hatnote(frame:newChild{ title = "Template:Hatnote" }) | |||
end | |||
function p.hatnote(frame) | function p.hatnote(frame) | ||
| Line 172: | Line 154: | ||
:wikitext(s) | :wikitext(s) | ||
return | return tostring(hatnote) | ||
end | end | ||
return p | return p | ||
Latest revision as of 18:51, 2 August 2025
Documentation for this module may be created at Module:Hatnote/doc
--------------------------------------------------------------------------------
-- Module:Hatnote --
-- --
-- This module produces hatnote links and links to related articles. It --
-- implements the {{hatnote}} and {{format link}} meta-templates and includes --
-- helper functions for other Lua hatnote modules. --
--------------------------------------------------------------------------------
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- lazily initialise [[Module:Arguments]]
local yesno -- lazily initialise [[Module:Yesno]]
local formatLink -- lazily initialise [[Module:Format link]] ._formatLink
local p = {}
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
local function getArgs(frame)
mArguments = require('Module:Arguments')
return mArguments.getArgs(frame, {parentOnly = true})
end
local function removeInitialColon(s)
return s:match('^:?(.*)')
end
function p.defaultClasses(inline)
return (inline == 1 and 'hatnote-inline' or 'hatnote') .. ' navigation-not-searchable'
end
function p.disambiguate(page, disambiguator)
checkType('disambiguate', 1, page, 'string')
checkType('disambiguate', 2, disambiguator, 'string', true)
disambiguator = disambiguator or 'disambiguation'
return mw.ustring.format('%s (%s)', page, disambiguator)
end
function p.findNamespaceId(link, removeColon)
checkType('findNamespaceId', 1, link, 'string')
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
if removeColon ~= false then
link = removeInitialColon(link)
end
local namespace = link:match('^(.-):')
if namespace then
local nsTable = mw.site.namespaces[namespace]
if nsTable then
return nsTable.id
end
end
return 0
end
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title)
checkType('makeWikitextError', 1, msg, 'string')
checkType('makeWikitextError', 2, helpLink, 'string', true)
yesno = require('Module:Yesno')
title = title or mw.title.getCurrentTitle()
local helpText
if helpLink then
helpText = ' ([[' .. helpLink .. '|help]])'
else
helpText = ''
end
local category
if not title.isTalkPage
and title.namespace ~= 2
and yesno(addTrackingCategory) ~= false
then
category = 'Hatnote templates with errors'
category = mw.ustring.format(
'[[%s:%s]]',
mw.site.namespaces[14].name,
category
)
else
category = ''
end
return mw.ustring.format(
'<strong class="error">Error: %s%s.</strong>%s',
msg,
helpText,
category
)
end
local curNs = mw.title.getCurrentTitle().namespace
p.missingTargetCat =
((curNs == 0) or (curNs == 14)) and
'Articles with hatnote templates targeting a nonexistent page' or nil
function p.quote(title)
local quotationMarks = {
["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true
}
local quoteLeft, quoteRight =
quotationMarks[string.sub(title, 1, 1)],
quotationMarks[string.sub(title, -1, -1)]
if quoteLeft or quoteRight then
title = mw.html.create("span"):wikitext(title)
end
if quoteLeft then title:css("padding-left", "0.15em") end
if quoteRight then title:css("padding-right", "0.15em") end
return '"' .. tostring(title) .. '"'
end
--------------------------------------------------------------------------------
-- Hatnote
--------------------------------------------------------------------------------
p[''] = function (frame)
return p.hatnote(frame:newChild{ title = "Template:Hatnote" })
end
function p.hatnote(frame)
local args = getArgs(frame)
local s = args[1]
if not s then
return p.makeWikitextError(
'no text specified',
'Template:Hatnote#Errors',
args.category
)
end
return p._hatnote(s, {
extraclasses = args.extraclasses,
selfref = args.selfref
})
end
function p._hatnote(s, options)
checkType('_hatnote', 1, s, 'string')
checkType('_hatnote', 2, options, 'table', true)
options = options or {}
local inline = options.inline
local hatnote = mw.html.create(inline == 1 and 'span' or 'div')
local extraclasses
if type(options.extraclasses) == 'string' then
extraclasses = options.extraclasses
end
hatnote
:attr('role', 'note')
:addClass(p.defaultClasses(inline))
:addClass(extraclasses)
:addClass(options.selfref and 'selfref' or nil)
:wikitext(s)
return tostring(hatnote)
end
return p