Difference between revisions 51796 and 51797 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)				return val
			else
				return nil
			end
		end
	})	


	-- Get the title.
	function envFuncs.title()
		-- The title object for the current page, or a test page passed with args.page.
		local title
		local titleArg = args[message('titleArg', 'string')]
		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

	-- Get the subject namespace number.
	function envFuncs.subjectSpace()function envFuncs.subjectSpace()
		-- The subject namespace number.
		return mw.site.namespaces[env.title.namespace].subject.id
	end
	
	-- Get tfunction envFuncs.docspace()
		-- The name of the documentation namespace.
	function envFuncs.docspace()
		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
	
	-- Get tfunction envFuncs.templatePage()
		-- The template page with no namespace or interwiki prefixes.
	function envFuncs.templatePage()
		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.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 = 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 = 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 = 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	

(contracted; show full)
	-- Assemble the fmbox text field.
	local text = ''
	if linkBox then
		-- Use custom link box content if it is defined.
		text = text .. linkBox
	else
		
if docExist then
			-- /doc exists; link to it.
			local docLink = makeWikilink(docpage)
			local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, message('editLinkDisplay', 'string'))
			local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, message('historyLinkDisplay', 'string'))
			text = text .. message('transcludedFromBlurb', 'string', {docLink}) .. ' ' .. makeToolbar(editLink, historyLink) .. '<br />'
		elseif subjectSpace == 828 then
			-- /doc does not exist; ask to create it.
			local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = message('modulePreload', 'string')}, message('createLinkDisplay', 'string'))
			text = text .. message('createModuleDocBlurb', 'string', {createLink}) .. '<br />'
		endtext = text .. p.makeDocPageBlurb(args, env)
		-- Add links to /sandbox and /testcases when appropriate.
		if subjectSpace == 2 or subjectSpace == 828 or subjectSpace == 10 then
			-- We are in the user, module or template namespaces. 
			text = text .. p.makeEndBoxExperimentBlurb(args, env)
			text = text .. '<br />'
			-- Show the categories text, but not if "content" fed or "docname fed" since then it is unclear where to add the categories.
			if not content and not docnameFed then
(contracted; show full)	end
	fmargs.text = text

	-- Return the fmbox output.
	return messageBox.main('fmbox', fmargs)
end


function p.makeDocPageBlurb(args, env)
	-- Get the title object.
	local success, docTitle = env:grab('docTitle')
	if not success then
		-- docTitle is the error message.
		return docTitle
	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 sandboxSuccess, sandboxTitle = env:grab('sandboxTitle')
	if not sandboxSuccess then
		return err(sandboxTitle)
(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