Difference between revisions 51810 and 51811 on wikimaniawiki

-- This module implements {{documentation}}.

-- Get required modules.
local getArgs = require('Module:Arguments').getArgs
local htmlBuilder = require('Module:HtmlBuilder')
local messageBox = require('Module:Message box')

-- Get the config table.
(contracted; show full)
	-- Set up the metatable. If a nil value is called, we call that function in the envFuncs table and memoize it
	-- in the env table so we don't have to call any of the functions more than once.
	setmetatable(env, {
		__index = function (t, key)
			local envFunc = envFuncs[key]
			if envFunc then
				local 
val = success, val = pcall(envFunc()
				env[key] = val
if success then
					env[key] = val -- Memoise the value.
					return val
			else
		end
			end
			return nil
			end
		end
	})	

	function envFuncs.title()
		-- The title object for the current page, or a test page passed with args.page.
		local title
		local titleArg = args.page
		if titleArg then
			title = mw.title.new(titleArg)
			if not title then
				error(message('titleArgError', 'string', {titleArg}))
			end
		else
			title = mw.title.getCurrentTitle()
		end
		return title
	end

	function envFuncs.subjectSpace()
		-- The subject namespace number.
		return mw.site.namespaces[env.title.namespace].subject.id
	end

	function envFuncs.docspace()
		-- The name of the documentation namespace.
		local subjectSpace = env.subjectSpace
		if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
			-- Pages in the Article, File, MediaWiki or Category namespaces must have their
			-- /doc, /sandbox and /testcases pages in talk space.
			return mw.site.namespaces[subjectSpace].talk.name 
		else
			return env.title.subjectNsText
		end
	end

	function envFuncs.templatePage()
		-- The template page with no namespace or interwiki prefixes.
		local title = env.title
		local subpage = title.subpageText
		if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
			return title.baseText
		else
			return title.text
		end
	end

	function envFuncs.templateTitle()
		-- The template (or module, etc.) title object.
		local title = env.title
		local subpage = title.subpageText
		local ret
		if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
			ret =urn title.basePageTitle
		else
			ret = title
		end
		if not ret then
			error(message('titleArgError', 'string', {titleArg}))
		end
		return return title
		end
	end

	function envFuncs.docTitle()
		-- Title object of the /doc subpage.
		local title = env.title
		local docname = args[1] -- User-specified doc page.
		local docpage
		if docname then
			docpage = docname
		else
			docpage = env.docpageRoot .. '/' .. message('docSubpage', 'string')
		end
		return mw.title.new(docpage)
	end
	
	function envFuncs.docpageRoot()
		-- The base page of the /doc, /sandbox, and /testcases subpages.
		-- For some namespaces this is the talk page, rather than the template page.
		local title = env.title
		return (env.docspace or title.nsText) .. ':' .. (env.templatePage or title.text)
	end
	
	function envFuncs.sandboxTitle()
		-- Title object for the /sandbox subpage.
		local titleArg = return mw.title.new(env.docpageRoot .. '/' .. message('sandboxSubpage', 'string')
		local title = mw.title.new(titleArg)
		if not title then
			error(message('titleArgError', 'string', {titleArg}))
		end
		return title)
	end
	
	function envFuncs.testcasesTitle()
		-- Title object for the /testcases subpage.
		local titleArg = return mw.title.new(env.docpageRoot .. '/' .. message('testcasesSubpage', 'string')
		local title = mw.title.new(titleArg)
		if not title then
			error(message('titleArgError', 'string', {titleArg}))
		end
		return title)
	end
	
	function envFuncs.printTitle()
		-- Title object for the /Print subpage.
		local titleArg = return mw.title.new(env.templatePage .. '/' .. message('printSubpage', 'string')
		local title = mw.title.new(titleArg)
		if not title then
			error(message('titleArgError', 'string', {titleArg}))
		end
		return title
	end
	
	function env:grab(key)
		local success, val = pcall(function() return self[key] end)
		return success, val)
	end

	return env
end	

----------------------------------------------------------------------------
-- Auxiliary templates
(contracted; show full)		return nil
	end
end

function p.makeStartBoxLinksData(args, env)
	local data = {}
	-- Get title objects.
	local title
Success, title = env:grab('title')
	if titleSuccess then
		data.title = title
	else
		return err(title)
	end
	local docTitleSuccess, = env.title
	local docTitle = env:grab('.docTitle')
	if docTitleSuccess then
		data.docTitle =not title or not docTitle
	else then
		return err(docTitle)nil
	end
	-- View, display, edit, and purge links if /doc exists.
	data.viewLinkDisplay = message('viewLinkDisplay', 'string')
	data.editLinkDisplay = message('editLinkDisplay', 'string')
	data.historyLinkDisplay = message('historyLinkDisplay', 'string')
	data.purgeLinkDisplay = message('purgeLinkDisplay', 'string')
	-- Create link if /doc doesn't exist.
(contracted; show full)	else
		ret = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay)
	end
end

function p.makeStartBoxData(args, env, links)
	local subjectSpace = env.subjectSpace

	if not subjectSpace then
		return nil
	end
	local data = {}
	
	-- Heading
	local heading = args.heading -- Blank values are not removed.
	if heading == '' then
		-- Don't display the start box if the heading arg is defined but blank.
		return nil
	end
(contracted; show full)-- Documentation content
----------------------------------------------------------------------------

p.content = makeInvokeFunc('_content')

function p._content(args, env)
	-- Get the /doc title object
	local
 success, docTitle = env:grab('.docTitle')
	if not successdocTitle then
		return err(docTitle) -- docTitle is an error messagenil
	end
	-- Get the documentation content.
	local content = args.content
	if not content and docTitle.exists then
		local frame = mw.getCurrentFrame()
		content = frame:preprocess('{{ ' .. docTitle.prefixedText .. ' }}')
	end
	-- The line breaks below are necessary so that "=== Headings ===" at the start and end
	-- of docs are interpreted correctly.
	return '\n' .. (content or '') .. '\n' 
end

----------------------------------------------------------------------------
-- End box
----------------------------------------------------------------------------

p.endBox = makeInvokeFunc('_endBox')

function p._endBox(args, env)
	-- This function generates the end box (also known as the link box).
	
	-- Get environment data.
	local subjectSpace = env.subjectSpace
	local success, docTitle = env:grab('.docTitle')
	if not success then
		return err(docTitle) -- Error messagebjectSpace or not docTitle then
		return nil
	end
		
	-- Check whether we should output the end box at all. Add the end
	-- box by default if the documentation exists or if we are in the
	-- user, module or template namespaces.
	if linkBox == 'off'
		or not (
(contracted; show full)
	-- Return the fmbox output.
	return messageBox.main('fmbox', fmargs)
end

function p.makePrintBlurb(args, env)
	-- Get the /Print title object
	local
 success, printTitle = env:grab('.printTitle')
	if not successprintTitle then
		return err(printTitle) -- Error messagenil
	end
	-- Make the print blurb.
	local ret
	if printTitle.exists then
		local printLink = makeWikilink(printTitle.prefixedText, message('printLinkDisplay', 'string'))
		ret = message('printBlurb', 'string', {printLink})
		local displayPrintCategory = message('displayPrintCategory', 'boolean')
		if displayPrintCategory then
			ret = ret .. makeCategoryLink(message('printCategory', 'string'))
		end
	end
	return ret
end

function p.makeSubpagesBlurb(args, env)
	-- Get the template title object
	local success, templateTitle = env:grab('.templateTitle')
	if not successtemplateTitle then
		return err(templateTitle) -- Error message.nil
	end
	-- Make the subpages blurb.
	local pagetype
	if subjectSpace == 10 then
		pagetype = message('templatePagetype', 'string')
	elseif subjectSpace == 828 then
		pagetype = message('modulePagetype', 'string')
	else
		pagetype = message('defaultPagetype', 'string')
	end
	return makeWikilink(
		'Special:PrefixIndex/' .. templateTitle.prefixedText .. '/',
		message('subpagesLinkDisplay', 'string', {pagetype})
	)
end

function p.makeCategoriesBlurb(args, env)
	-- Get the title object.
	local success, docTitle = env:grab('.docTitle')
	if not successdocTitle then
		return err(docTitle) -- Error messagenil
	end
	-- Make the blurb.
	local docPathLink = makeWikilink(docTitle.prefixedText, message('docLinkDisplay', 'string'))
	return message('addCategoriesBlurb', 'string', {docPathLink})
end

function p.makeDocPageBlurb(args, env)
	-- Get the title object.
	local success, docTitle = env:grab('.docTitle')
	if not successdocTitle then
		return err(docTitle) -- Error messagenil
	end
	-- Make the blurb.
	local ret
	if docTitle.exists then
		-- /doc exists; link to it.
		local docLink = makeWikilink(docTitle.prefixedText)
		local editUrl = docTitle:fullUrl{action = 'edit'}
		local editDisplay = message('editLinkDisplay', 'string')
		local editLink = makeUrlLink(editUrl, editDisplay)
		local historyUrl = docTitle:fullUrl{action = 'history'}
		local historyDisplay = message('historyLinkDisplay', 'string')
		local historyLink = makeUrlLink(historyUrl, historyDisplay)
		ret = message('transcludedFromBlurb', 'string', {docLink})
			.. ' '
			.. makeToolbar(editLink, historyLink)
			.. '<br />'
	elseif env.subjectSpace == 828 then
		-- /doc does not exist; ask to create it.
		local createUrl = docTitle:fullUrl{action = 'edit', preload = message('modulePreload', 'string')}
		local createDisplay = message('createLinkDisplay', 'string')
		local createLink = makeUrlLink(createUrl, createDisplay)
		ret = message('createModuleDocBlurb', 'string', {createLink})
			.. '<br />'
	end
	return ret
end

function p.makeEndBoxExperimentBlurb(args, env)
	-- Renders the text "Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages."
	local subjectSpace = env.subjectSpace
	local templatePage = env.templatePage
	-- Get title objects.
	local templateSuccess, templateTitle = env:grab('templateTitle')
	if not templateSuccess then
		return err(Title = env.templateTitle)
	end
	local sandboxSuccess, sandboxTitle = env:grab('.sandboxTitle')
	if not sandboxSuccess then
		return err(sandboxTitle)
	end
	local testcasesSuccess, testcasesTitle = env:grab('testcasesTitle')
	if
	local testcasesTitle = env.testcasesTitle
	if not templateTitle or not sandboxTitle or not testcasesSuccessTitle then
		return err(testcasesTitle)nil
	end
	-- Make links.
	local sandboxLinks, testcasesLinks
	if sandboxTitle.exists then
		local sandboxPage = sandboxTitle.prefixedText
		local sandboxDisplay = message('sandboxLinkDisplay', 'string')
		local sandboxLink = makeWikilink(sandboxPage, sandboxDisplay)
(contracted; show full)
		local sort = (title.namespace == 0 and message('strangeUsageCategoryMainspaceSort', 'string') or '') .. title.prefixedText -- Sort on namespace.
		ret = ret .. makeCategoryLink(message('strangeUsageCategory', 'string'), sort)
	end
	return ret
end

return p