Difference between revisions 51792 and 51793 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)----------------------------------------------------------------------------
-- Start box
----------------------------------------------------------------------------

p.startBox = makeInvokeFunc('_startBox')

function p._startBox(args, env)
	
local title = env.title
	local subjectSpace = env.subjectSpace

	-- Arg processing from {{documentation}}.
	local preload = args[message('preloadArg', 'string')] -- Allow custom preloads.
	local heading = args[message('headingArg', 'string')] -- Blank values are not removed.
	local headingStyle = args[message('headingStyleArg', 'string')]
	local content = args[message('contentArg', 'string')]
	local docspace = env.docspace
	local docname = args[1] -- Other docname, if fed.
	local templatePage = env.templatePage

	-- Arg processing from {{documentation/start box2}}.
	local docpage
	if docname then
		docpage = docname
	else
		local namespace = docspace or title.nsText
		local pagename = templatePage or title.text
		docpage = namespace .. ':' .. pagename .. '/' .. message('docSubpage', 'string')
	end
	local docTitle = mw.title.new(docpage)
	local docExist = docTitle.exists
	
	-- Output from {{documentation/start box}}.

	-- First, check the heading parameter.
	if heading == '' then
		-- Heading is defined but blank, so do nothing.
		return nil
	end

	-- Build the start box div.
	local sbox = htmlBuilder.create('div')
	sbox
		.css('padding-bottom', '3px')
		.css('border-bottom', '1px solid #aaa')
		.css('margin-bottom', '1ex')
		.newline()

	-- Make the heading.
	local hspan = sbox.tag('span')
	if headingStyle then
		hspan.cssText(headingStyle)
	elseif subjectSpace == 10 then
		-- We are in the template or template talk namespaces.
		hspan
			.css('font-weight', 'bold')
			.css('font-size', '125%')
	else
		hspan.css('font-size', '150%')
	end
	if heading then
		-- "heading" has data.
		hspan.wikitext(heading)
	elseif subjectSpace == 10 then -- Template namespace
		hspan.wikitext(message('documentationIconWikitext', 'string') .. ' ' .. message('templateNamespaceHeading', 'string'))
	elseif subjectSpace == 828 then -- Module namespace
		hspan.wikitext(message('documentationIconWikitext', 'string') .. ' ' .. message('moduleNamespaceHeading', 'string'))
	elseif subjectSpace == 6 then -- File namespace
		hspan.wikitext(message('fileNamespaceHeading', 'string'))
	else
		hspan.wikitext(message('otherNamespacesHeading', 'string'))
	end

	-- Add the [view][edit][history][purge] or [create] links.
	-- Check for the content parameter first, as we don't need the links if the documentation
	-- content is being entered directly onto the template page.
	if not content then
		local lspan = sbox.tag('span') -- lspan is short for "link span".
		lspan
			.addClass(message('startBoxLinkclasses', 'string'))
			.attr('id', message('startBoxLinkId', 'string'))
		if docExist then
			local viewLink = makeWikilink(docpage, message('viewLinkDisplay', 'string'))
			local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, message('editLinkDisplay', 'string'))
			local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, message('historyLinkDisplay', 'string'))
			local purgeLink = makeUrlLink(title:fullUrl{action = 'purge'}, message('purgeLinkDisplay', 'string'))
			local text = '[%s] [%s] [%s] [%s]'
			text = text:gsub('%[', '[') -- Replace square brackets with HTML entities.
			text = text:gsub('%]', ']')
			lspan.wikitext(mw.ustring.format(text, viewLink, editLink, historyLink, purgeLink))
		else
			if not preload then
				if subjectSpace == 6 then -- File namespace
					preload = message('fileDocpagePreload', 'string')
				else
					preload = message('docpagePreload', 'string')
				end
			end
			lspan.wikitext(makeUrlLink(docTitle:fullUrl{action = 'edit', preload = preload}, message('createLinkDisplay', 'string')))
		end
	end

	return tostring(sbox)-- Generate [view][edit][history][purge] or [create] links.
	local links
	local content = args[message('contentArg', 'string')]
	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
(contracted; show full)
		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
		
lspan.wikitext(makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay))
	endret = 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')] -- 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('headingStyleArg', '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)
	if data.showLlocal links = data.links
	if links then
		sbox.tag('span')
			.addClass(data.linksClass)
			.attr('id', data.linksId)
			.wikitext(data.links)
	end
	return tostring(sbox)
end

----------------------------------------------------------------------------
-- Documentation content
----------------------------------------------------------------------------
(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