Difference between revisions of "Module:See also"

From CWS Planet
Jump to navigation Jump to search
m (1 revision imported)
 
m (1 revision imported)
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
--[[
--[[
-- This module produces a "See also: a, b and c" link. It implements the
-- This module produces a "See also: a, b, and c" link. It implements the
-- template {{see also}}.
-- template {{see also}}.
--]]
--]]


local mHatnote = require('Module:Hatnote')
local mHatnote = require('Module:Hatnote')
local mTableTools -- lazily initialise
local mHatlist = require('Module:Hatnote list')
local mArguments -- lazily initialise
local mArguments -- lazily initialise
 
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}
local p = {}


function p.seealso(frame)
function p.seeAlso(frame)
mTableTools = require('Module:TableTools')
mArguments = require('Module:Arguments')
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame, {parentOnly = true})
local args = mArguments.getArgs(frame, {parentOnly = true})
Line 17: Line 17:
for k, v in pairs(args) do
for k, v in pairs(args) do
if type(k) == 'number' then
if type(k) == 'number' then
local numstring = tostring(k)
local display = args['label ' .. k] or args['l' .. k]
local display = args['label ' .. numstring]
local page = display and
or args['l' .. numstring]
string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
local page = {v, display}
pages[#pages + 1] = page
pages[k] = page
end
end
end
end
pages = mTableTools.compressSparseArray(pages)
if not pages[1] then
if not pages[1] then
return mHatnote.makeWikitextError(
return mHatnote.makeWikitextError(
Line 33: Line 31:
end
end
local options = {
local options = {
altphrase = args.altphrase,
selfref = args.selfref
selfref = args.selfref
}
}
return p._seealso(options, unpack(pages))
return p._seeAlso(pages, options)
end
end


function p._seealso(options, ...)
function p._seeAlso(args, options)
local altphrase = options and options.altphrase or 'See also'
checkType('_seeAlso', 1, args, 'table')
local links = mHatnote.formatPageTables(...)
checkType('_seeAlso', 2, options, 'table', true)
links = mw.text.listToText(links)
options = options or {}
local text = altphrase .. ': ' .. links
local list = mHatlist.andList(args, true)
 
local text = string.format('See also: %s', list)
-- Pass options through.
-- Pass options through.
local hnOptions = {}
local hnOptions = {
hnOptions.selfref = options.selfref
selfref = options.selfref
 
}
return mHatnote._hatnote(text, hnOptions)
return mHatnote._hatnote(text, hnOptions)
end
end


return p
return p

Latest revision as of 02:14, 11 August 2016

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

--[[
-- This module produces a "See also: a, b, and c" link. It implements the
-- template {{see also}}.
--]]

local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
local mArguments -- lazily initialise
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}

function p.seeAlso(frame)
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs(frame, {parentOnly = true})
	local pages = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			local display = args['label ' .. k] or args['l' .. k]
			local page = display and
				string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
			pages[#pages + 1] = page
		end
	end
	if not pages[1] then
		return mHatnote.makeWikitextError(
			'no page names specified',
			'Template:See also#Errors',
			args.category
		)
	end
	local options = {
		selfref = args.selfref
	}
	return p._seeAlso(pages, options)
end

function p._seeAlso(args, options)
	checkType('_seeAlso', 1, args, 'table')
	checkType('_seeAlso', 2, options, 'table', true)
	options = options or {}
	local list = mHatlist.andList(args, true)
	local text = string.format('See also: %s', list)
	-- Pass options through.
	local hnOptions = {
		selfref = options.selfref
	}
	return mHatnote._hatnote(text, hnOptions)
end

return p