Module:Main/sandbox

From blackwiki
< Module:Main
Revision as of 19:49, 1 July 2016 by test>Nihiltres (Prototype improvement: offload common stuff onto Module:Labelled list hatnote)
Jump to navigation Jump to search

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

--[[
-- This module produces a link to a main article or articles. It implements the
-- template {{main}}.
-- 
-- If the module is used in category or category talk space, it produces "The
-- main article for this category is xxx". Otherwise, it produces
-- "Main article: xxx".
--]]

local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
local mLabelledList = require('Module:Labelled list hatnote')
local mArguments -- lazily initialise
local p = {}

function p.main(frame)
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs(frame, {parentOnly = true})
	local pages = mLabelledList.preprocessDisplays(args)
	local options = {
		selfref = args.selfref,
		category = args.category
	}
	return p._main(pages, options)
end

function p._main(pages, options)
	-- Get the list of pages. If no first page was specified we use the current
	-- page name.
	local currentTitle = mw.title.getCurrentTitle()
	if #pages == 0 then
		if mw.title.getCurrentTitle().namespace == 0 then
			return mHatnote.makeWikitextError(
				'no page names specified',
				'Template:Main#Errors',
				options.category
			)
		else
			pages = {currentTitle.text}
		end
	end
	local firstPage = string.gsub(pages[1], '|.*$', '')
	-- Find the pagetype.
	local pageType = mHatnote.findNamespaceId(firstPage) == 0 and 'article' or 'page'
	local curNs = currentTitle.namespace
	if (curNs == 14) or (curNs == 15) then --category/talk namespaces
		local mainForm = #pages > 1 and
			'The main %ss for this [[Wikipedia:Categorization|category]] are %s.'
			or
			'The main %s for this [[Wikipedia:Categorization|category]] is %s.'
		return mHatnote._hatnote(
			string.format(mainForm, pageType, mHatlist.andList(pages, true)),
			{selfref = options.selfref}
		)
	else
		return mLabelledList._labelledList(
			pages,
			{
				string.format('Main %s', pageType),
				string.format('Main %ss', pageType)
			},
			{
				category = options.category,
				selfref = options.selfref,
				template = 'Template:Main'
			}
		)
	end
end

return p