Difference between revisions 51785 and 51786 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)	end
	for i = 1, lim do
		ret[#ret + 1] = select(i, ...)
	end
	return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
end	


local function err(msg)
	return string.format(
		'<strong class="error">[[Module:Documentation]] error: %s</strong>%s',
		msg,
		makeCategoryLink('Documentation template invocations with errors')
	)
end

----------------------------------------------------------------------------
-- Argument processing
----------------------------------------------------------------------------

local function makeInvokeFunc(funcName)
	return function (frame)
		local headingArg = message('headingArg', 'string')
		local args = getArgs(frame, {
			valueFunc = function (key, value)
				if type(value) == 'string' then
					value = value:match('^%s*(.-)%s*$') -- Remove whitespace.
					if key == headingArg or value ~= '' then
						return value
					else
						return nil
					end
				else
					return value
				end
			end
		})
		return p[funcName](args)
	end
end

----------------------------------------------------------------------------
-- Main function
----------------------------------------------------------------------------

p.main = makeInvokeFunc('_main')

function p._main(args)
	-- Get environment data, using pcall in case we get any errors.
	local success,local env = pcall(p.getEnv, args)
	if not success then
		return string.format('<strong class="error">[[Module:Documentation]] error: %s</strong>', env) -- If there's an error, env is the error message.
	end
	-- Build the documentation.ironment(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('mainDivId', 'string'))
			.addClass(message('mainDivClasses', 'string'))
			.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 the title to use, the subject namespace, etc.
	-- This is called from p._main using pcall in case we get any errors from exceeding the expensive function count
	-- limit, or other perils unknown.
	--
	-- Data includes:
	-- env.title - the title object of the page we are making documentation for (usually the current title)
	-- env.subjectSpace - the number of the title's subject namespace.
	-- env.docspace - the name of the namespace the title puts its documentation in.
	-- env.templatePage - the name of the template page with no namespace or interwiki prefixes.
	local env = {}

	-- Get the title.
, envFuncs = {}, {}

	-- 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 = envFunc()
				env[key] = val
				return val
			else
				return nil
			end
		end
	})	

	-- Get the title.
	function envFuncs.title()
		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
	env.title =	return title
	end

	-- Get the subject namespace number.
	local function envFuncs.subjectSpace =()
		return mw.site.namespaces[env.title.namespace].subject.id
	env.subjectSpace = subjectSpaced
	
	-- Get the name of the documentation namespace.
	local docsfunction 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.
		docspace =	return mw.site.namespaces[subjectSpace].talk.name 
			else
		docspace = 	return env.title.subjectNsText
			end
	env.docspace = docspaced
	
	-- Get the template page with no namespace or interwiki prefixes.
	local templatePage
function envFuncs.templatePage()
		local title = env.title
		local subpage = title.subpageText
			if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
		templatePage =	return title.baseText
			else
		templatePage =	return title.text
			end
	env.templatePage = templatePaged

	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)
		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