Difference between revisions 51828 and 51829 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)----------------------------------------------------------------------------
-- Main function
----------------------------------------------------------------------------

p.main = makeInvokeFunc('_main')

function p._main(args)

	--[[
	-- This function defines logic flow for the module.
	-- @args - table of arguments passed by the user
	-- 
	-- Messages:
	-- 'main-div-id' --> 'template-documentation'
	-- 'main-div-classes' --> 'template-documentation iezoomfix'
	--]]
	local env = p.getEnvironment(args)
	local root = htmlBuilder.create()
	root
		.wikitext(p.protectionTemplate(env))
		.wikitext(p.sandboxNotice(args, env))
		 -- This div tag is from {{documentation/start box}}, but moving it here
		 -- so that we don't have to worry about unclosed tags.
		.tag('div')
			.attr('id', message('main-div-id'))
			.addClass(message('main-div-classes'))
			.newline()
			.wikitext(p._startBox(args, env))
			.wikitext(p._content(args, env))
			.tag('div')
				.css('clear', 'both') -- So right or left floating items don't stick out of the doc box.
				.newline()
				.done()
			.done()
		.wikitext(p._endBox(args, env))
		.newline()
		.wikitext(p.addTrackingCategories(env))
	return tostring(root)
end

----------------------------------------------------------------------------
-- Environment settings
----------------------------------------------------------------------------

function p.getEnvironment(args)
	--[[
	-- Returns a table with information about the environment, including title objects and other namespace- or
	-- path-related data.
	-- @args - table of arguments passed by the user
	--
	-- Title objects include:
	-- env.title - the page we are making documentation for (usually the current title)
	-- env.templateTitle - the template (or module, file, etc.)
	-- env.docTitle - the /doc subpage.
	-- env.sandboxTitle - the /sandbox subpage.
	-- env.testcasesTitle - the /testcases subpage.
	-- env.printTitle - the print version of the template, located at the /Print subpage.
(contracted; show full)		else
			title = mw.title.getCurrentTitle()
		end
		return title
	end

	function envFuncs.templateTitle()

		--[[
		-- The template (or module, etc.) title object.
		-- Messages:
		-- 'sandbox-subpage' --> 'sandbox'
		-- 'testcases-subpage' --> 'testcases'
		--]]
		local title = env.title
		local subpage = title.subpageText
		if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage') then
			return title.basePageTitle
		else
			return title
		end
	end

	function envFuncs.docTitle()
		--[[
		-- Title object of the /doc subpage.
		-- Messages:
		-- 'doc-subpage' --> 'doc'
		--]]
		local title = env.title
		local docname = args[1] -- User-specified doc page.
		local docpage
		if docname then
			docpage = docname
		else
			docpage = env.docpageRoot .. '/' .. message('doc-subpage')
		end
		return mw.title.new(docpage)
	end
	
	function envFuncs.sandboxTitle()
		--[[
		-- Title object for the /sandbox subpage.
		-- Messages:
		-- 'sandbox-subpage' --> 'sandbox'
		--]]
		return mw.title.new(env.docpageRoot .. '/' .. message('sandbox-subpage'))
	end
	
	function envFuncs.testcasesTitle()
		--[[
		-- Title object for the /testcases subpage.
		-- Messages:
		-- 'testcases-subpage' --> 'testcases'
		--]]
		return mw.title.new(env.docpageRoot .. '/' .. message('testcases-subpage'))
	end
	
	function envFuncs.printTitle()
		--[[
		-- Title object for the /Print subpage.
		-- Messages:
		-- 'print-subpage' --> 'Print'
		--]]
		return env.templateTitle:subPageTitle(message('print-subpage'))
	end

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

(contracted; show full)end	

----------------------------------------------------------------------------
-- Auxiliary templates
----------------------------------------------------------------------------

function p.sandboxNotice(args, env)

	--[=[
	-- Generates a sandbox notice for display above sandbox pages.
	-- @args - a table of arguments passed by the user
	-- @env - environment table containing title objects, etc., generated with p.getEnvironment
	-- 
	-- Messages:
	-- 'sandbox-notice-image' --> '[[Image:Sandbox.png|50px|alt=|link=]]'
	-- 'sandbox-notice-blurb' --> 'This is the [[Wikipedia:Template test cases|template sandbox]] page for $1.'
	-- 'sandbox-notice-diff-blurb' --> 'This is the [[Wikipedia:Template test cases|template sandbox]] page for $1 ($2).'
	-- 'sandbox-notice-compare-link-display' --> 'diff'
	-- 'sandbox-notice-testcases-blurb' --> 'See also the companion subpage for $1.'
	-- 'sandbox-notice-testcases-link-display' --> 'test cases'
	-- 'sandbox-category' --> 'Template sandboxes'
	--]=]
	local title = env.title
	local sandboxTitle = env.sandboxTitle
	local templateTitle = env.templateTitle
	if not (title and sandboxTitle and templateTitle and mw.title.equals(title, sandboxTitle)) then
		return nil
	end
	local omargs = {} -- Args for {{ombox}}.
	-- Get the image wikitext.-- Build the table of arguments to pass to {{ombox}}. We need just two fields, "image" and "text".
	local omargs = {}
	omargs.image = message('sandbox-notice-image')
	-- Get the text. We start with the opening blurb, which is something like
	-- "This is the template sandbox for [[Template:Foo]] (diff)."
	local text = ''
	local frame = mw.getCurrentFrame()
	local isPreviewing = frame:preprocess('{{REVISIONID}}') == '' -- True if the page is being previewed.
	local templateLink = makeWikilink(templateTitle.prefixedText)
	local compareUrl = env.compareUrl
	if isPreviewing or not compareUrl then
		-- 'This is the [[Wikipedia:Template test cases|template sandbox]] page for $1.'
		text = text .. message('sandbox-notice-blurb', {templateLink})
	else
		-- 'This is the [[Wikipedia:Template test cases|template sandbox]] page for $1 ($2).'text = text .. message('sandbox-notice-blurb', {templateLink})
	else
		local compareDisplay = message('sandbox-notice-compare-link-display')
		local compareLink = makeUrlLink(compareUrl, compareDisplay)
		text = text .. message('sandbox-notice-diff-blurb', {templateLink, compareLink})
	end
	-- Get the test cases page blurb if the page exists. This is something like
	-- "See also the companion subpage for [[Template:Foo/testcases|test cases]]."
	local testcasesTitle = env.testcasesTitle
	if testcasesTitle and testcasesTitle.exists then
		local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display')
		local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay)
		text = text .. '<br />' .. message('sandbox-notice-testcases-blurb', {testcasesLink})
	end
	-- Add the page to [[Category:Template sandboxes]]sandbox to the sandbox category.
	text = text .. makeCategoryLink(message('sandbox-category'))
	omargs.text = text
	return messageBox.main('ombox', omargs)
end

function p.protectionTemplate(env)
	local title = env.title
(contracted; show full)	if message('display-strange-usage-category', nil, 'boolean') and (subpage == message('doc-subpage') or subpage == message('testcases-subpage')) then
		local sort = (title.namespace == 0 and message('strange-usage-category-mainspace-sort') or '') .. title.prefixedText -- Sort on namespace.
		ret = ret .. makeCategoryLink(message('strange-usage-category'), sort)
	end
	return ret
end

return p