Difference between revisions 51802 and 51803 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
		end
	})	

	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')].page
		if titleArg then
			title = mw.title.new(titleArg)
			if not title then
				error(message('titleArgError', 'string', {titleArg}))
			end
		else
			title = mw.title.getCurrentTitle()
(contracted; show full)	end
	local frame = mw.getCurrentFrame()
	local notice = htmlBuilder.create()
	notice
		.tag('div')
			.css('clear', 'both')
			.done()
		.wikitext(frame:expandTemplate{title = sandboxNoticeTemplate, args = {[message('sandboxNoticeLivepageParam')] = args
[message('livepageArg', 'string')].livepage}})
	return tostring(notice)
end

function p.protectionTemplate(env)
	local title = env.title
	local protectionTemplate = message('protectionTemplate', 'string')
	if not (protectionTemplate and title.namespace == 10) then
		-- Don't display the protection template if we are not in the template namespace.
		return nil
	end
	local frame = mw.getCurrentFrame()
	local function getProtectionLevel(protectionType, page)
		-- Gets the protection level for page, or for the current page if page is not specified.
		local level = frame:callParserFunction('PROTECTIONLEVEL', protectionType, page)
		if level ~= '' then
			return level
		else
			return nil -- The parser function returns the blank string if there is no match.
		end
	end
	local prefixedTitle = title.prefixedText
	if getProtectionLevel('move', prefixedTitle) == 'sysop' or getProtectionLevel('edit', prefixedTitle) then
		-- The page is full-move protected, or full, template, or semi-protected.
		return frame:expandTemplate{title = protectionTemplate, args = message('protectionTemplateArgs', 'table')}
	end
	return nil
end

----------------------------------------------------------------------------
-- Start box
----------------------------------------------------------------------------

p.startBox = makeInvokeFunc('_startBox')

function p._startBox(args, env)
	-- Generate [view][edit][history][purge] or [create] links.
	local links
	local content = args[message('contentArg', 'string')].content
	if not content then
		-- No need to include the links if the documentation is on the template page itself.
		local linksData = p.makeStartBoxLinksData(args, env)
		links = p.renderStartBoxLinks(linksData)
	end
	-- Generate the start box html.
	local data = p.makeStartBoxData(args, env, links)
	if type(data) == 'table' then
		return p.renderStartBox(data)
	elseif type(data) == 'string' then
		-- data is an error message.
		return data
	else
		-- User specified no heading.
		return nil
	end
end

function p.makeStartBoxLinksData(args, env)
	local data = {}
	-- Get title objects.
	local titleSuccess, title = env:grab('title')
	if titleSuccess then
		data.title = title
	else
		return err(title)
	end
	local docTitleSuccess, docTitle = env:grab('docTitle')
	if docTitleSuccess then
		data.docTitle = docTitle
	else
		return err(docTitle)
	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.
	local preload = args[message('preloadArg', 'string')].preload
	if not preload then
		if env.subjectSpace == 6 then -- File namespace
			preload = message('fileDocpagePreload', 'string')
		else
			preload = message('docpagePreload', 'string')
		end
	end
	data.preload = preload
	data.createLinkDisplay = message('createLinkDisplay', 'string')
	return data
end

function p.renderStartBoxLinks(data)
	-- Render the [view][edit][history][purge] or [create] links.
	local ret
	local docTitle = data.docTitle
	local title = data.title
	if docTitle.exists then
		local viewLink = makeWikilink(docTitle.prefixedText, data.viewLinkDisplay)
		local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, data.editLinkDisplay)
		local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, data.historyLinkDisplay)
		local purgeLink = makeUrlLink(title:fullUrl{action = 'purge'}, data.purgeLinkDisplay)
		ret = '[%s] [%s] [%s] [%s]'
		ret = ret:gsub('%[', '[') -- Replace square brackets with HTML entities.
		ret = ret:gsub('%]', ']')
		ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink)
	else
		ret = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay)
	end
end

function p.makeStartBoxData(args, env, links)
	local subjectSpace = env.subjectSpace
	local data = {}
	
	-- Heading
	local heading = args[message('headingArg', 'string')].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
	if heading then
		data.heading = heading
	elseif subjectSpace == 10 then -- Template namespace
		data.heading = message('documentationIconWikitext', 'string') .. ' ' .. message('templateNamespaceHeading', 'string')
	elseif subjectSpace == 828 then -- Module namespace
		data.heading = message('documentationIconWikitext', 'string') .. ' ' .. message('moduleNamespaceHeading', 'string')
	elseif subjectSpace == 6 then -- File namespace
		data.heading = message('fileNamespaceHeading', 'string')
	else
		data.heading = message('otherNamespacesHeading', 'string')
	end
	
	-- Heading CSS
	local headingStyle = args[message('headingS-styleArg', 'string')']
	if headingStyle then
		data.headingStyleText = headingStyle
	elseif subjectSpace == 10 then
		-- We are in the template or template talk namespaces.
		data.headingFontWeight = 'bold'
		data.headingFontSize = '125%'
	else
		data.headingFontSize = '150%'
	end
	
	-- [view][edit][history][purge] or [create] links.
	if links then
		data.linksClass = message('startBoxLinkclasses', 'string')
		data.linksId = message('startBoxLinkId', 'string')
		data.links = links
	end
	
	return data
end

function p.renderStartBox(data)
	-- Renders the start box html.
	local sbox = htmlBuilder.create('div')
	sbox
		.css('padding-bottom', '3px')
		.css('border-bottom', '1px solid #aaa')
		.css('margin-bottom', '1ex')
		.newline()
		.tag('span')
			.cssText(data.headingStyleText)
			.css('font-weight', data.headingFontWeight)
			.css('font-size', data.headingFontSize)
			.wikitext(data.heading)
	local links = data.links
	if links then
		sbox.tag('span')
			.addClass(data.linksClass)
			.attr('id', data.linksId)
			.wikitext(links)
	end
	return tostring(sbox)
end

----------------------------------------------------------------------------
-- Documentation content
----------------------------------------------------------------------------

p.content = makeInvokeFunc('_content')

function p._content(args, env)
	local content = args[message('contentArg', 'string')].content
	if not content then
		local docpage = args[1]
		if docpage and mw.title.new(docpage).exists then
			local frame = mw.getCurrentFrame()
			content = frame:preprocess('{{ ' .. docpage .. ' }}')
		else
			docpage = env.docspace .. ':' .. env.templatePage .. '/' .. message('docSubpage', 'string')
			if mw.title.new(docpage).exists then
				local frame = mw.getCurrentFrame()
				content = frame:preprocess('{{ ' .. docpage .. ' }}')
			end
		end
	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)
	local title = env.title
	local subjectSpace = env.subjectSpace

	-- Argument processing in {{documentation}}.
	local content = args[message('contentArg', 'string')]
	local linkBox = args[message('linkBoxArg', 'string').content
	local linkBox = args['link box'] -- So "link box=off" works.
	local docspace = env.docspace
	local docname = args[1] -- Other docname, if fed.
	local templatePage = env.templatePage

	-- Argument processing in {{documentation/end box2}}.
	local docpageRoot = (docspace or title.nsText) .. ':' .. (templatePage or title.text)
	local docpage
(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