Difference between revisions 51759 and 51760 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')

local p = {}

-- Constants.
local currentTitle = mw.title.getCurrentTitle()
local subjectSpace = mw.site.namespaces[currentTitle.namespace].subject.id

----------------------------------------------------------------------------
-- Helper functions
----------------------------------------------------------------------------

local function makeWikilink(page, display)
	if display then
		return mw.ustring.format('[[%s|%s]]', page, display)
	else
		return mw.ustring.format('[[%s]]', page)
	end
end

local function makeUrlLink(url, display)
	return mw.ustring.format('[%s %s]', url, display)
end

local function makeToolbar(...)
	local ret = {}
	local lim = select('#', ...)
	if lim < 1 then
		return nil
	end
	for i = 1, lim do
		ret[#ret + 1] = select(i, ...)
	end
	return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
end	

----------------------------------------------------------------------------
-- Main functions
----------------------------------------------------------------------------

function p.main(frame)
	local args = getArgs(frame, {
		valueFunc = function (key, value)
			if type(value) == 'string' then
(contracted; show full)
		text = text .. linkBox
	else
		if docExist then
			-- /doc exists; link to it.
			local docLink = makeWikilink(docpage)
			local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, 'edit')
			local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, 'history')
			text = text .. 'The above [[Wikipedia:Template documentation|documentation]] is [[Wikipedia:Transclusion|transcluded]] from ' .. docLink .. '.
  '
				.. ' <small style="font-style: normal;">(' .. editLink .. ' &#124; ' ..makeToolbar(editLink, historyLink) .. ')</small> <br />'
		elseif subjectSpace == 828 then
			-- /doc does not exist; ask to create it.
			local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = 'Template:Documentation/preload-module-doc'}, 'create')
			text = text .. 'You might want to ' .. createLink .. ' a documentation page for this [[Wikipedia:Lua|Scribunto module]].<br />'
		end
		-- Add links to /sandbox and /testcases when appropriate.
		if subjectSpace == 2 or subjectSpace == 828 or subjectSpace == 10 then
			-- We are in the user, module or template namespaces. 
			local pagePossessive = subjectSpace == 828 and "module's" or "template's"
			text = text .. 'Editors can experiment in this ' .. pagePossessive .. ' '
			local sandboxTitle = mw.title.new(sandbox)
			if sandboxTitle.exists then
				local sandboxLink = makeWikilink(sandbox, 'sandbox')
				local sandboxEditLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit'}, 'edit')
				local compareLink = makeUrlLink(mw.title.new('Special:ComparePages'):fullUrl{page1 = templatePage, page2 = sandbox}, 'diff')
				text = text .. sandboxLink .. ' <small style="font-style: normal">(' .. sandboxEditLink .. ' | ' .. compareLink .. ')</small>'' .. makeToolbar(sandboxEditLink, compareLink)
			else
				local sandboxPreload = 'Template:Documentation/preload-' .. (subjectSpace == 828 and 'module-' or '') .. 'sandbox'
				local sandboxCreateLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload}, 'create')
				local mirrorSummary = 'Create sandbox version of ' .. makeWikilink(templatePage)
				local mirrorLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = templatePage, summary = mirrorSummary}, 'mirror')
				text = text .. 'sandbox <small style="font-style: normal">(' .. sandboxCreateLink .. ' | ' .. mirrorLink .. ')</small>'' .. makeToolbar(sandboxCreateLink, mirrorLink)
			end
			text = text .. ' and '
			local testcaseTitle = mw.title.new(testcases)
			if testcaseTitle.exists then
				local testcasesLink = makeWikilink(testcases, 'testcases')
				local testcasesEditLink = makeUrlLink(testcaseTitle:fullUrl{action = 'edit'}, 'edit')
				text = text .. testcasesLink .. ' <small style="font-style: normal">(' .. ' .. makeToolbar(testcasesEditLink .. ')</small>')
			else
				local testcasesPreload = 'Template:Documentation/preload-' .. (subjectSpace == 828 and 'module-' or '') .. 'testcases'
				local testcasesCreateLink = makeUrlLink(testcaseTitle:fullUrl{action = 'edit', preload = testcasesPreload}, 'create')
				text = text .. 'testcases <small style="font-style: normal">(' .. ' .. makeToolbar(testcasesCreateLink .. ')</small>')
			end
			text = text .. ' pages. <br />'
			-- Show the categories text, but not if "content" fed or "docname fed" since then it is unclear where to add the categories.
			if not content and not docnameFed then
				text = text .. 'Please add categories to the ' .. makeWikilink(docpage, '/doc') .. ' subpage.'
			end
			-- Show the "subpages" link.
(contracted; show full)	if subpage == 'sandbox' or subpage == 'testcases' then
		return currentTitle.baseText
	else
		return currentTitle.text
	end
end

return p