Jump to content

Module:Other uses: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Protected "Module:Other uses": High-risk Lua module ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite))
Removed long comment block redundant to docs, simplified a default's form, and restructured a string concatenation as a better string.format
Line 4: Line 4:
local libraryUtil = require('libraryUtil')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkType = libraryUtil.checkType

local p = {}
local p = {}


--Produces standard {{other uses}} implementation
-- Produces standard {{other uses}} implementation
function p.otheruses(frame)
function p.otheruses(frame)
mArguments = require('Module:Arguments')
mArguments = require('Module:Arguments')
Line 16: Line 15:
end
end


--Main generator
-- Main generator
--Two parameters, args and options, each tables:
-- * args: can be empty or nil, in which case the defaults kick in.
-- Otherwise assumed to be a table of unbracketed page strings, no table gaps
-- * options: must not be nil; it must have in a "defaultPage" or "title" option,
-- since this function doesn't use a frame object to do the defaulting itself.
-- Summary of options available:
-- * defaultPage: sets full default "other uses" target; overrides title and disambiguator
-- * title: sets title assumed that gets the suffix added
-- * disambiguator: sets default text appearing in parens, defaults to "disambiguation"
-- * otherText: allows setting more "other" options, defaults to "uses"
function p._otheruses(args, options)
function p._otheruses(args, options)
--Type-checks and defaults
checkType('_otheruses', 1, args, 'table', true)
checkType('_otheruses', 1, args, 'table', true)
if not args then args = {} end
args = args or {}
checkType('_otheruses', 2, options, 'table')
checkType('_otheruses', 2, options, 'table')
if not (options.defaultPage or options.title) then
if not (options.defaultPage or options.title) then
Line 40: Line 30:
}
}
end
end
--should switch to using _forSee from [[Module:Hatnote list]] once that's out of alpha
local forOther = 'For other ' .. (options.otherText or 'uses') .. ', '
local text = string.format(
local see = 'see ' .. mw.text.listToText(mHatnote.formatPages(unpack(args))) .. "."
'For other %s, see %s.',
local text = forOther .. see
(options.otherText or 'uses'),
--should switch to using andList from [[Module:Hatnote list]] once that's out of alpha
mw.text.listToText(mHatnote.formatPages(unpack(args)))
)
return mHatnote._hatnote(text)
return mHatnote._hatnote(text)
end
end

Revision as of 16:42, 5 May 2016

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

-- Produces standard {{other uses}} implementation
function p.otheruses(frame)
	mArguments = require('Module:Arguments')
	mTableTools = require('Module:TableTools')
	local args = mTableTools.compressSparseArray(mArguments.getArgs(frame))
	local title = mw.title.getCurrentTitle().text
	return p._otheruses(args, {title=title})
end

-- Main generator
function p._otheruses(args, options)
	--Type-checks and defaults
	checkType('_otheruses', 1, args, 'table', true)
	args = args or {}
	checkType('_otheruses', 2, options, 'table')
	if not (options.defaultPage or options.title) then
		error('No default title data provided in "_otheruses" options table', 2)
	end
	if #args == 0 then
		args = {
			options.defaultPage or
			(options.title .. '  (' .. (options.disambiguator or 'disambiguation') .. ')')
		}
	end
	--should switch to using _forSee from [[Module:Hatnote list]] once that's out of alpha
	local text = string.format(
		'For other %s, see %s.',
		(options.otherText or 'uses'),
		--should switch to using andList from [[Module:Hatnote list]] once that's out of alpha
		mw.text.listToText(mHatnote.formatPages(unpack(args)))
	)
	return mHatnote._hatnote(text)
end

return p