Difference between pages "Module:Hatnote" and "Module:Unsubst"

From Kailasapedia
(Difference between pages)
Jump to navigation Jump to search
m (1 revision imported)
 
m (1 revision imported)
 
Line 1: Line 1:
--------------------------------------------------------------------------------
+
local checkType = require('libraryUtil').checkType
--                              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 mArguments -- lazily initialise [[Module:Arguments]]
 
local yesno -- lazily initialise [[Module:Yesno]]
 
  
 
local p = {}
 
local p = {}
  
--------------------------------------------------------------------------------
+
local BODY_PARAM = '$B'
-- Helper functions
 
--------------------------------------------------------------------------------
 
  
local function getArgs(frame)
+
local specialParams = {
-- Fetches the arguments from the parent frame. Whitespace is trimmed and
+
['$params'] = 'parameter list',
-- blanks are removed.
+
['$aliases'] = 'parameter aliases',
mArguments = require('Module:Arguments')
+
['$flags'] = 'flags',
return mArguments.getArgs(frame, {parentOnly = true})
+
['$B'] = 'template content'
end
+
}
  
local function removeInitialColon(s)
+
function p.main(frame, body)
-- Removes the initial colon from a string, if present.
+
-- If we are substing, this function returns a template invocation, and if
return s:match('^:?(.*)')
+
-- not, it returns the template body. The template body can be specified in
end
+
-- the body parameter, or in the template parameter defined in the
 +
-- BODY_PARAM variable. This function can be called from Lua or from
 +
-- #invoke.
  
function p.findNamespaceId(link, removeColon)
+
-- Return the template body if we aren't substing.
-- Finds the namespace id (namespace number) of a link or a pagename. This
+
if not mw.isSubsting() then
-- function will not work if the link is enclosed in double brackets. Colons
+
if body ~= nil then
-- are trimmed from the start of the link by default. To skip colon
+
return body
-- trimming, set the removeColon parameter to false.
+
elseif frame.args[BODY_PARAM] ~= nil then
checkType('findNamespaceId', 1, link, 'string')
+
return frame.args[BODY_PARAM]
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
+
else
if removeColon ~= false then
+
error(string.format(
link = removeInitialColon(link)
+
"no template content specified (use parameter '%s' from #invoke)",
end
+
BODY_PARAM
local namespace = link:match('^(.-):')
+
), 2)
if namespace then
 
local nsTable = mw.site.namespaces[namespace]
 
if nsTable then
 
return nsTable.id
 
 
end
 
end
 
end
 
end
return 0
 
end
 
  
function p.formatPages(...)
+
-- Sanity check for the frame object.
-- Formats a list of pages using formatLink and returns it as an array. Nil
+
if type(frame) ~= 'table'
-- values are not allowed.
+
or type(frame.getParent) ~= 'function'
local pages = {...}
+
or not frame:getParent()
local ret = {}
+
then
for i, page in ipairs(pages) do
+
error(
ret[i] = p._formatLink(page)
+
"argument #1 to 'main' must be a frame object with a parent " ..
 +
"frame available",
 +
2
 +
)
 
end
 
end
return ret
 
end
 
  
function p.formatPageTables(...)
+
-- Find the invocation name.
-- Takes a list of page/display tables and returns it as a list of
+
local mTemplateInvocation = require('Module:Template invocation')
-- formatted links. Nil values are not allowed.
+
local name = mTemplateInvocation.name(frame:getParent():getTitle())
local pages = {...}
 
local links = {}
 
for i, t in ipairs(pages) do
 
checkType('formatPageTables', i, t, 'table')
 
local link = t[1]
 
local display = t[2]
 
links[i] = p._formatLink(link, display)
 
end
 
return links
 
end
 
  
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title)
+
-- Combine passed args with passed defaults
-- Formats an error message to be returned to wikitext. If
+
local args = {}
-- addTrackingCategory is not false after being returned from
+
if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*override%s*,' ) then
-- [[Module:Yesno]], and if we are not on a talk page, a tracking category
+
for k, v in pairs( frame:getParent().args ) do
-- is added.
+
args[k] = v
checkType('makeWikitextError', 1, msg, 'string')
+
end
checkType('makeWikitextError', 2, helpLink, 'string', true)
+
for k, v in pairs( frame.args ) do
yesno = require('Module:Yesno')
+
if not specialParams[k] then
title = title or mw.title.getCurrentTitle()
+
if v == '__DATE__' then
-- Make the help link text.
+
v = mw.getContentLanguage():formatDate( 'F Y' )
local helpText
+
end
if helpLink then
+
args[k] = v
helpText = ' ([[' .. helpLink .. '|help]])'
+
end
 +
end
 
else
 
else
helpText = ''
+
for k, v in pairs( frame.args ) do
 +
if not specialParams[k] then
 +
if v == '__DATE__' then
 +
v = mw.getContentLanguage():formatDate( 'F Y' )
 +
end
 +
args[k] = v
 +
end
 +
end
 +
for k, v in pairs( frame:getParent().args ) do
 +
args[k] = v
 +
end
 
end
 
end
-- Make the category text.
+
 
local category
+
-- Trim parameters, if not specified otherwise
if not title.isTalkPage and yesno(addTrackingCategory) ~= false then
+
if not string.find( ','..(frame.args['$flags'] or '')..',', ',%s*keep%-whitespace%s*,' ) then
category = 'Hatnote templates with errors'
+
for k, v in pairs( args ) do args[k] = mw.ustring.match(v, '^%s*(.*)%s*$') or '' end
category = string.format(
 
'[[%s:%s]]',
 
mw.site.namespaces[14].name,
 
category
 
)
 
else
 
category = ''
 
 
end
 
end
return string.format(
 
'<strong class="error">Error: %s%s.</strong>%s',
 
msg,
 
helpText,
 
category
 
)
 
end
 
  
function p.disambiguate(page, disambiguator)
+
-- Pull information from parameter aliases
-- Formats a page title with a disambiguation parenthetical,
+
local aliases = {}
-- i.e. "Example" → "Example (disambiguation)".
+
if frame.args['$aliases'] then
checkType('disambiguate', 1, page, 'string')
+
local list = mw.text.split( frame.args['$aliases'], '%s*,%s*' )
checkType('disambiguate', 2, disambiguator, 'string', true)
+
for k, v in ipairs( list ) do
disambiguator = disambiguator or 'disambiguation'
+
local tmp = mw.text.split( v, '%s*>%s*' )
return string.format('%s (%s)', page, disambiguator)
+
aliases[tonumber(mw.ustring.match(tmp[1], '^[1-9][0-9]*$')) or tmp[1]] = ((tonumber(mw.ustring.match(tmp[2], '^[1-9][0-9]*$'))) or tmp[2])
end
+
end
 
 
--------------------------------------------------------------------------------
 
-- Format link
 
--
 
-- 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 "#". Used in
 
-- the {{format hatnote link}} template.
 
--------------------------------------------------------------------------------
 
 
 
function p.formatLink(frame)
 
local args = getArgs(frame)
 
local link = args[1]
 
local display = args[2]
 
if not link then
 
return p.makeWikitextError(
 
'no link specified',
 
'Template:Format hatnote link#Errors',
 
args.category
 
)
 
 
end
 
end
return p._formatLink(link, display)
+
for k, v in pairs( aliases ) do
end
+
if args[k] and ( not args[v] or args[v] == '' ) then
 
+
args[v] = args[k]
function p._formatLink(link, display)
+
end
checkType('_formatLink', 1, link, 'string')
+
args[k] = nil
checkType('_formatLink', 2, display, 'string', true)
 
 
 
-- Remove the initial colon for links where it was specified manually.
 
link = removeInitialColon(link)
 
 
 
-- Find whether a faux display value has been added with the {{!}} magic
 
-- word.
 
if not display then
 
local prePipe, postPipe = link:match('^(.-)|(.*)$')
 
link = prePipe or link
 
display = postPipe
 
 
end
 
end
  
-- Find the display value.
+
-- Remove empty parameters, if specified
if not display then
+
if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*remove%-empty%s*,' ) then
local page, section = link:match('^(.-)#(.*)$')
+
local tmp = 0
if page then
+
for k, v in ipairs( args ) do
display = page .. ' §&nbsp;' .. section
+
if v ~= '' or ( args[k+1] and args[k+1] ~= '' ) or ( args[k+2] and args[k+2] ~= '' ) then
 +
tmp = k
 +
else
 +
break
 +
end
 +
end
 +
for k, v in pairs( args ) do
 +
if v == '' then
 +
if not (type(k) == 'number' and k < tmp) then args[k] = nil end
 +
end
 
end
 
end
 
end
 
end
  
-- Assemble the link.
+
-- Order parameters
if display then
+
if frame.args['$params'] then
return string.format(
+
local params, tmp = mw.text.split( frame.args['$params'], '%s*,%s*' ), {}
'[[:%s|%s]]',
+
for k, v in ipairs(params) do
string.gsub(link, '|(.*)$', ''), --display overwrites manual piping
+
v = tonumber(mw.ustring.match(v, '^[1-9][0-9]*$')) or v
display
+
if args[v] then tmp[v], args[v] = args[v], nil end
)
+
end
else
+
for k, v in pairs(args) do tmp[k], args[k] = args[k], nil end
return string.format('[[:%s]]', link)
+
args = tmp
 
end
 
end
end
 
  
--------------------------------------------------------------------------------
+
return mTemplateInvocation.invocation(name, args)
-- Hatnote
 
--
 
-- Produces standard hatnote text. Implements the {{hatnote}} template.
 
--------------------------------------------------------------------------------
 
 
 
function p.hatnote(frame)
 
local args = getArgs(frame)
 
local s = args[1]
 
local options = {}
 
if not s then
 
return p.makeWikitextError(
 
'no text specified',
 
'Template:Hatnote#Errors',
 
args.category
 
)
 
end
 
options.extraclasses = args.extraclasses
 
options.selfref = args.selfref
 
return p._hatnote(s, options)
 
 
end
 
end
  
function p._hatnote(s, options)
+
p[''] = p.main -- For backwards compatibility
checkType('_hatnote', 1, s, 'string')
 
checkType('_hatnote', 2, options, 'table', true)
 
options = options or {}
 
local classes = {'hatnote', 'navigation-not-searchable'}
 
local extraclasses = options.extraclasses
 
local selfref = options.selfref
 
if type(extraclasses) == 'string' then
 
classes[#classes + 1] = extraclasses
 
end
 
if selfref then
 
classes[#classes + 1] = 'selfref'
 
end
 
return string.format(
 
'<div role="note" class="%s">%s</div>',
 
table.concat(classes, ' '),
 
s
 
)
 
end
 
  
 
return p
 
return p

Latest revision as of 16:11, 5 February 2019

Documentation for this module may be created at Module:Unsubst/doc

local checkType = require('libraryUtil').checkType

local p = {}

local BODY_PARAM = '$B'

local specialParams = {
	['$params'] = 'parameter list',
	['$aliases'] = 'parameter aliases',
	['$flags'] = 'flags',
	['$B'] = 'template content'
}

function p.main(frame, body)
	-- If we are substing, this function returns a template invocation, and if
	-- not, it returns the template body. The template body can be specified in
	-- the body parameter, or in the template parameter defined in the
	-- BODY_PARAM variable. This function can be called from Lua or from
	-- #invoke.

	-- Return the template body if we aren't substing.
	if not mw.isSubsting() then
		if body ~= nil then
			return body
		elseif frame.args[BODY_PARAM] ~= nil then
			return frame.args[BODY_PARAM]
		else
			error(string.format(
				"no template content specified (use parameter '%s' from #invoke)",
				BODY_PARAM
			), 2)
		end
	end

	-- Sanity check for the frame object.
	if type(frame) ~= 'table'
		or type(frame.getParent) ~= 'function'
		or not frame:getParent()
	then
		error(
			"argument #1 to 'main' must be a frame object with a parent " ..
			"frame available",
			2
		)
	end

	-- Find the invocation name.
	local mTemplateInvocation = require('Module:Template invocation')
	local name = mTemplateInvocation.name(frame:getParent():getTitle())

	-- Combine passed args with passed defaults
	local args = {}
	if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*override%s*,' ) then
		for k, v in pairs( frame:getParent().args ) do
			args[k] = v
		end
		for k, v in pairs( frame.args ) do
			if not specialParams[k] then
				if v == '__DATE__' then
					v = mw.getContentLanguage():formatDate( 'F Y' )
				end
				args[k] = v
			end
		end
	else
		for k, v in pairs( frame.args ) do
			if not specialParams[k] then
				if v == '__DATE__' then
					v = mw.getContentLanguage():formatDate( 'F Y' )
				end
				args[k] = v
			end
		end
		for k, v in pairs( frame:getParent().args ) do
			args[k] = v
		end
	end

	-- Trim parameters, if not specified otherwise
	if not string.find( ','..(frame.args['$flags'] or '')..',', ',%s*keep%-whitespace%s*,' ) then
		for k, v in pairs( args ) do args[k] = mw.ustring.match(v, '^%s*(.*)%s*$') or '' end
	end

	-- Pull information from parameter aliases
	local aliases = {}
	if frame.args['$aliases'] then
		local list = mw.text.split( frame.args['$aliases'], '%s*,%s*' )
		for k, v in ipairs( list ) do
			local tmp = mw.text.split( v, '%s*>%s*' )
			aliases[tonumber(mw.ustring.match(tmp[1], '^[1-9][0-9]*$')) or tmp[1]] = ((tonumber(mw.ustring.match(tmp[2], '^[1-9][0-9]*$'))) or tmp[2])
		end
	end
	for k, v in pairs( aliases ) do
		if args[k] and ( not args[v] or args[v] == '' ) then
			args[v] = args[k]
		end
		args[k] = nil
	end

	-- Remove empty parameters, if specified
	if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*remove%-empty%s*,' ) then
		local tmp = 0
		for k, v in ipairs( args ) do
			if v ~= '' or ( args[k+1] and args[k+1] ~= '' ) or ( args[k+2] and args[k+2] ~= '' ) then
				tmp = k
			else
				break
			end
		end
		for k, v in pairs( args ) do
			if v == '' then
				if not (type(k) == 'number' and k < tmp) then args[k] = nil end
			end
		end
	end

	-- Order parameters
	if frame.args['$params'] then
		local params, tmp = mw.text.split( frame.args['$params'], '%s*,%s*' ), {}
		for k, v in ipairs(params) do
			v = tonumber(mw.ustring.match(v, '^[1-9][0-9]*$')) or v
			if args[v] then tmp[v], args[v] = args[v], nil end
		end
		for k, v in pairs(args) do tmp[k], args[k] = args[k], nil end
		args = tmp
	end

	return mTemplateInvocation.invocation(name, args)
end

p[''] = p.main -- For backwards compatibility

return p