Module:Documentation: Difference between revisions

    From Nonbinary Wiki
    templatewiki>Rob Kam
    m (1 revision imported)
    m (1 revision imported)
    Line 334: Line 334:
    -- 'sandbox-notice-blurb' --> 'This is the $1 for $2.'
    -- 'sandbox-notice-blurb' --> 'This is the $1 for $2.'
    -- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).'
    -- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).'
    -- 'sandbox-notice-pagetype-template' --> '[[Wikipedia:Template test cases|template sandbox]] page'
    -- 'sandbox-notice-pagetype-template' --> '[[w:Wikipedia:Template sandbox and test cases|template sandbox]] page'
    -- 'sandbox-notice-pagetype-module' --> '[[Wikipedia:Template test cases|module sandbox]] page'
    -- 'sandbox-notice-pagetype-module' --> '[[w:Wikipedia:Template sandbox and test cases|module sandbox]] page'
    -- 'sandbox-notice-pagetype-other' --> 'sandbox page'
    -- 'sandbox-notice-pagetype-other' --> 'sandbox page'
    -- 'sandbox-notice-compare-link-display' --> 'diff'
    -- 'sandbox-notice-compare-link-display' --> 'diff'
    Line 445: Line 445:
    local links
    local links
    local content = args.content
    local content = args.content
    if not content or args[1] then
    if not content then
    -- No need to include the links if the documentation is on the template page itself.
    -- No need to include the links if the documentation is on the template page itself.
    local linksData = p.makeStartBoxLinksData(args, env)
    local linksData = p.makeStartBoxLinksData(args, env)
    Line 757: Line 757:
    -- 'history-link-display' --> 'history'
    -- 'history-link-display' --> 'history'
    -- 'transcluded-from-blurb' -->  
    -- 'transcluded-from-blurb' -->  
    -- 'The above [[Wikipedia:Template documentation|documentation]]  
    -- 'The above [[w:Wikipedia:Template documentation|documentation]]  
    -- is [[Wikipedia:Transclusion|transcluded]] from $1.'
    -- is [[w:Wikipedia:Transclusion|transcluded]] from $1.'
    -- 'module-preload' --> 'Template:Documentation/preload-module-doc'
    -- 'module-preload' --> 'Template:Documentation/preload-module-doc'
    -- 'create-link-display' --> 'create'
    -- 'create-link-display' --> 'create'
    -- 'create-module-doc-blurb' -->
    -- 'create-module-doc-blurb' -->
    -- 'You might want to $1 a documentation page for this [[Wikipedia:Lua|Scribunto module]].'
    -- 'You might want to $1 a documentation page for this [[w:Wikipedia:Lua|Scribunto module]].'
    --]=]
    --]=]
    local docTitle = env.docTitle
    local docTitle = env.docTitle

    Revision as of 06:12, 1 April 2020

    Documentation for this module may be created at Module:Documentation/doc

    -- This module implements {{documentation}}.
    
    -- Get required modules.
    local getArgs = require('Module:Arguments').getArgs
    local messageBox = require('Module:Message box')
    
    -- Get the config table.
    local cfg = mw.loadData('Module:Documentation/config')
    
    local p = {}
    
    -- Often-used functions.
    local ugsub = mw.ustring.gsub
    
    ----------------------------------------------------------------------------
    -- Helper functions
    --
    -- These are defined as local functions, but are made available in the p
    -- table for testing purposes.
    ----------------------------------------------------------------------------
    
    local function message(cfgKey, valArray, expectType)
    	--[[
    	-- Gets a message from the cfg table and formats it if appropriate.
    	-- The function raises an error if the value from the cfg table is not
    	-- of the type expectType. The default type for expectType is 'string'.
    	-- If the table valArray is present, strings such as $1, $2 etc. in the
    	-- message are substituted with values from the table keys [1], [2] etc.
    	-- For example, if the message "foo-message" had the value 'Foo $2 bar $1.',
    	-- message('foo-message', {'baz', 'qux'}) would return "Foo qux bar baz."
    	--]]
    	local msg = cfg[cfgKey]
    	expectType = expectType or 'string'
    	if type(msg) ~= expectType then
    		error('message: type error in message cfg.' .. cfgKey .. ' (' .. expectType .. ' expected, got ' .. type(msg) .. ')', 2)
    	end
    	if not valArray then
    		return msg
    	end
    
    	local function getMessageVal(match)
    		match = tonumber(match)
    		return valArray[match] or error('message: no value found for key $' .. match .. ' in message cfg.' .. cfgKey, 4)
    	end
    
    	local ret = ugsub(msg, '$([1-9][0-9]*)', getMessageVal)
    	return ret
    end
    
    p.message = message
    
    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
    
    p.makeWikilink = makeWikilink
    
    local function makeCategoryLink(cat, sort)
    	local catns = mw.site.namespaces[14].name
    	return makeWikilink(catns .. ':' .. cat, sort)
    end
    
    p.makeCategoryLink = makeCategoryLink
    
    local function makeUrlLink(url, display)
    	return mw.ustring.format('[%s %s]', url, display)
    end
    
    p.makeUrlLink = makeUrlLink
    
    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	
    
    p.makeToolbar = makeToolbar
    
    ----------------------------------------------------------------------------
    -- Argument processing
    ----------------------------------------------------------------------------
    
    local function makeInvokeFunc(funcName)
    	return function (frame)
    		local args = getArgs(frame, {
    			valueFunc = function (key, value)
    				if type(value) == 'string' then
    					value = value:match('^%s*(.-)%s*$') -- Remove whitespace.
    					if key == 'heading' 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)
    	--[[
    	-- This function defines logic flow for the module.
    	-- @args - table of arguments passed by the user
    	-- 
    	-- Messages:
    	-- 'main-div-id' --> 'template-documentation'
    	-- 'main-div-classes' --> 'template-documentation iezoomfix'
    	--]]
    	local env = p.getEnvironment(args)
    	local root = mw.html.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('main-div-id'))
    			:addClass(message('main-div-classes'))
    			: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))
    		:wikitext(p.addTrackingCategories(env))
    	return tostring(root)
    end
    
    ----------------------------------------------------------------------------
    -- Environment settings
    ----------------------------------------------------------------------------
    
    function p.getEnvironment(args)
    	--[[
    	-- Returns a table with information about the environment, including title objects and other namespace- or
    	-- path-related data.
    	-- @args - table of arguments passed by the user
    	--
    	-- Title objects include:
    	-- env.title - the page we are making documentation for (usually the current title)
    	-- env.templateTitle - the template (or module, file, etc.)
    	-- env.docTitle - the /doc subpage.
    	-- env.sandboxTitle - the /sandbox subpage.
    	-- env.testcasesTitle - the /testcases subpage.
    	-- env.printTitle - the print version of the template, located at the /Print subpage.
    	--
    	-- Data includes:
    	-- env.protectionLevels - the protection levels table of the title object.
    	-- env.subjectSpace - the number of the title's subject namespace.
    	-- env.docSpace - the number of the namespace the title puts its documentation in.
    	-- env.docpageBase - the text of the base page of the /doc, /sandbox and /testcases pages, with namespace.
    	-- env.compareUrl - URL of the Special:ComparePages page comparing the sandbox with the template.
    	-- 
    	-- All table lookups are passed through pcall so that errors are caught. If an error occurs, the value
    	-- returned will be nil.
    	--]]
    	
    	local env, envFuncs = {}, {}
    
    	-- Set up the metatable. If triggered we call the corresponding function in the envFuncs table. The value
    	-- returned by that function is memoized in the env table so that we don't call any of the functions
    	-- more than once. (Nils won't be memoized.)
    	setmetatable(env, {
    		__index = function (t, key)
    			local envFunc = envFuncs[key]
    			if envFunc then
    				local success, val = pcall(envFunc)
    				if success then
    					env[key] = val -- Memoise the value.
    					return val
    				end
    			end
    			return nil
    		end
    	})	
    
    	function envFuncs.title()
    		-- The title object for the current page, or a test page passed with args.page.
    		local title
    		local titleArg = args.page
    		if titleArg then
    			title = mw.title.new(titleArg)
    		else
    			title = mw.title.getCurrentTitle()
    		end
    		return title
    	end
    
    	function envFuncs.templateTitle()
    		--[[
    		-- The template (or module, etc.) title object.
    		-- Messages:
    		-- 'sandbox-subpage' --> 'sandbox'
    		-- 'testcases-subpage' --> 'testcases'
    		--]]
    		local subjectSpace = env.subjectSpace
    		local title = env.title
    		local subpage = title.subpageText
    		if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage') then
    			return mw.title.makeTitle(subjectSpace, title.baseText)
    		else
    			return mw.title.makeTitle(subjectSpace, title.text)
    		end
    	end
    
    	function envFuncs.docTitle()
    		--[[
    		-- Title object of the /doc subpage.
    		-- Messages:
    		-- 'doc-subpage' --> 'doc'
    		--]]
    		local title = env.title
    		local docname = args[1] -- User-specified doc page.
    		local docpage
    		if docname then
    			docpage = docname
    		else
    			docpage = env.docpageBase .. '/' .. message('doc-subpage')
    		end
    		return mw.title.new(docpage)
    	end
    	
    	function envFuncs.sandboxTitle()
    		--[[
    		-- Title object for the /sandbox subpage.
    		-- Messages:
    		-- 'sandbox-subpage' --> 'sandbox'
    		--]]
    		return mw.title.new(env.docpageBase .. '/' .. message('sandbox-subpage'))
    	end
    	
    	function envFuncs.testcasesTitle()
    		--[[
    		-- Title object for the /testcases subpage.
    		-- Messages:
    		-- 'testcases-subpage' --> 'testcases'
    		--]]
    		return mw.title.new(env.docpageBase .. '/' .. message('testcases-subpage'))
    	end
    	
    	function envFuncs.printTitle()
    		--[[
    		-- Title object for the /Print subpage.
    		-- Messages:
    		-- 'print-subpage' --> 'Print'
    		--]]
    		return env.templateTitle:subPageTitle(message('print-subpage'))
    	end
    
    	function envFuncs.protectionLevels()
    		-- The protection levels table of the title object.
    		return env.title.protectionLevels
    	end
    
    	function envFuncs.subjectSpace()
    		-- The subject namespace number.
    		return mw.site.namespaces[env.title.namespace].subject.id
    	end
    
    	function envFuncs.docSpace()
    		-- The documentation namespace number. For most namespaces this is the same as the
    		-- subject namespace. However, pages in the Article, File, MediaWiki or Category
    		-- namespaces must have their /doc, /sandbox and /testcases pages in talk space.
    		local subjectSpace = env.subjectSpace
    		if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
    			return subjectSpace + 1
    		else
    			return subjectSpace
    		end
    	end
    
    	function envFuncs.docpageBase()
    		-- The base page of the /doc, /sandbox, and /testcases subpages.
    		-- For some namespaces this is the talk page, rather than the template page.
    		local templateTitle = env.templateTitle
    		local docSpace = env.docSpace
    		local docSpaceText = mw.site.namespaces[docSpace].name
    		-- Assemble the link. docSpace is never the main namespace, so we can hardcode the colon.
    		return docSpaceText .. ':' .. templateTitle.text
    	end
    	
    	function envFuncs.compareUrl()
    		-- Diff link between the sandbox and the main template using [[Special:ComparePages]].
    		local templateTitle = env.templateTitle
    		local sandboxTitle = env.sandboxTitle
    		if templateTitle.exists and sandboxTitle.exists then
    			local compareUrl = mw.uri.fullUrl(
    				'Special:ComparePages',
    				{page1 = templateTitle.prefixedText, page2 = sandboxTitle.prefixedText}
    			)
    			return tostring(compareUrl)
    		else
    			return nil
    		end
    	end		
    
    	return env
    end	
    
    ----------------------------------------------------------------------------
    -- Auxiliary templates
    ----------------------------------------------------------------------------
    
    function p.sandboxNotice(args, env)
    	--[=[
    	-- Generates a sandbox notice for display above sandbox pages.
    	-- @args - a table of arguments passed by the user
    	-- @env - environment table containing title objects, etc., generated with p.getEnvironment
    	-- 
    	-- Messages:
    	-- 'sandbox-notice-image' --> '[[Image:Sandbox.svg|50px|alt=|link=]]'
    	-- 'sandbox-notice-blurb' --> 'This is the $1 for $2.'
    	-- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).'
    	-- 'sandbox-notice-pagetype-template' --> '[[w:Wikipedia:Template sandbox and test cases|template sandbox]] page'
    	-- 'sandbox-notice-pagetype-module' --> '[[w:Wikipedia:Template sandbox and test cases|module sandbox]] page'
    	-- 'sandbox-notice-pagetype-other' --> 'sandbox page'
    	-- 'sandbox-notice-compare-link-display' --> 'diff'
    	-- 'sandbox-notice-testcases-blurb' --> 'See also the companion subpage for $1.'
    	-- 'sandbox-notice-testcases-link-display' --> 'test cases'
    	-- 'sandbox-category' --> 'Template sandboxes'
    	--]=]
    	local title = env.title
    	local sandboxTitle = env.sandboxTitle
    	local templateTitle = env.templateTitle
    	local subjectSpace = env.subjectSpace
    	if not (subjectSpace and title and sandboxTitle and templateTitle and mw.ti