Difference between revisions 51784 and 51785 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

	-- 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 currentTtitle.nsText
		local pagename = templatePage or currentTtitle.text
		docpage = namespace .. ':' .. pagename .. '/' .. message('docSubpage', 'string')
	end
	local docTitle = mw.title.new(docpage)
	local docExist = docTitle.exists
	
	-- Output from {{documentation/start box}}.

(contracted; show full)
		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(
currentTtitle: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)
end

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

p.content = makeInvokeFunc('_content')

function p._content(args, env)
	local content = args[message('contentArg', 'string')]
	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 = penv.docspace() .. ':' .. penv.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')] -- So "link box=off" works.
	local docspace = penv.docspace()
	local docname = args[1] -- Other docname, if fed.
	local templatePage = penv.templatePage()

	-- Argument processing in {{documentation/end box2}}.
	local docpageRoot = (docspace or currentTtitle.nsText) .. ':' .. (templatePage or currentTtitle.text)
	local docpage
	if docname then
		docpage = docname
	else
		docpage = docpageRoot .. '/' .. message('docSubpage', 'string')
	end
	local docTitle = mw.title.new(docpage)
	local docExist = docTitle.exists
	local docnameFed = args[1] and true
	local sandbox = docpageRoot .. '/' .. message('sandboxSubpage', 'string')
	local testcases = docpageRoot .. '/' .. message('testcasesSubpage', 'string')
	templatePage = currentTtitle.nsText .. ':' .. templatePage

	-- Output from {{documentation/end box}}
	
	-- First, check whether we should output the end box at all. Add the end box by default if the documentation
	-- exists or if we are in the user, module or template namespaces.
	if linkBox == message('linkBoxOff', 'string') or not (docExist or subjectSpace == 2 or subjectSpace == 828 or subjectSpace == 10) then
		return nil
(contracted; show full)	return messageBox.main('fmbox', fmargs)
end

----------------------------------------------------------------------------
-- Tracking categories
----------------------------------------------------------------------------

function p.addTrackingCategories(
env)
	-- Check if {{documentation}} is transcluded on a /doc or /testcases page.
	local title = env.title
	local ret = ''
	local subpage = currentTtitle.subpageText
	if message('displayStrangeUsageCategory', 'boolean') and (subpage == message('docSubpage', 'string') or subpage == message('testcasesSubpage', 'string')) then
		local sort = (currentTtitle.namespace == 0 and message('strangeUsageCategoryMainspaceSort', 'string') or '') .. currentTtitle.prefixedText -- Sort on namespace.
		ret = ret .. makeCategoryLink(message('strangeUsageCategory', 'string'), sort)
	end
	return ret
end

return p