跳转到内容

Module:Documentation

鹿野耕云留言 | 贡献2025年11月21日 (五) 09:49的版本

脚本错误:函数“nonexistent”不存在。

-- 此模块实现了{{documentation}}模板。

-- 获取所需的模块。
local getArgs = require('Module:Arguments').getArgs
local messageBox = require('Module:Message box')

-- 获取配置表。
local cfg = mw.loadData('Module:Documentation/config')
local p = {}

-- 常用函数。
local ugsub = mw.ustring.gsub

----------------------------------------------------------------------------
-- 辅助函数
--
-- 这些函数定义为局部函数,但为了测试目的在p表中可用。
----------------------------------------------------------------------------

local function message(cfgKey, valArray, expectType)
	--[[
	-- 从cfg表中获取消息并在适当时格式化。
	-- 如果cfg表中的值不是expectType类型,函数会引发错误。expectType的默认类型是'string'。
	-- 如果存在valArray表,消息中的$1、$2等字符串将被替换为表中键[1]、[2]等的值。
	-- 例如,如果消息"foo-message"的值为'Foo $2 bar $1.',
	-- message('foo-message', {'baz', 'qux'})将返回"Foo qux bar baz."
	--]]
	local msg = cfg[cfgKey]
	expectType = expectType or 'string'
	if type(msg) ~= expectType then
		error(string.format('Configuration error: key "%s" should be of type "%s" but is of type "%s"', cfgKey, expectType, type(msg)), 2)
	end
	if not valArray then
		return msg
	end

	local function getMessageVal(match)
		match = tonumber(match)
		return valArray[match] or error(string.format('Configuration error: placeholder $%d is empty in key "%s"', match, 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

----------------------------------------------------------------------------
-- 参数处理
----------------------------------------------------------------------------

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

----------------------------------------------------------------------------
-- 加载TemplateStyles
----------------------------------------------------------------------------

p.main = function(frame)
	local parent = frame.getParent(frame)
	local output = p._main(parent.args)
	return frame:extensionTag{ name='templatestyles', args = { src= message('templatestyles-scr') } } .. output
end

----------------------------------------------------------------------------
-- 主函数
----------------------------------------------------------------------------

function p._main(args)
	--[[
	-- 此函数定义模块的逻辑流程。
	-- @args - 用户传递的参数表
	-- 
	-- 消息:
	-- 'main-div-id' --> 'template-documentation'
	-- 'main-div-classes' --> 'template-documentation iezoomfix'
	--]]
	local env = p.getEnvironment(args)
	local root = mw.html.create()
	root
		:wikitext(p._getModuleWikitext(args, env))
		:wikitext(p.protectionTemplate(env))
		:wikitext(p.sandboxNotice(args, env))
		 -- 这个div标签来自{{documentation/start box}},但移动到这里
			 -- 这样我们就不用担心未闭合的标签。
		:tag('div')
			:attr('id', message('main-div-id'))
			:addClass(message('main-div-class'))
			:wikitext(p._startBox(args, env))
			:wikitext(p._content(args, env))
			:done()
		:wikitext(p._endBox(args, env))
		:wikitext(p.addTrackingCategories(env))
	return tostring(root)
end

----------------------------------------------------------------------------
-- 环境设置
----------------------------------------------------------------------------

function p.getEnvironment(args)
	--[[
	-- 返回一个包含环境信息的表,包括标题对象和其他命名空间或路径相关数据。
	-- @args - 用户传递的参数表
	--
	-- 标题对象包括:
	-- env.title - 我们正在为其制作文档的页面(通常是当前标题)
	-- env.templateTitle - 模板(或模块、文件等)
	-- env.docTitle - /doc子页面
	-- env.sandboxTitle - /sandbox子页面
	-- env.testcasesTitle - /testcases子页面
	-- env.printTitle - 模板的打印版本,位于/Print子页面
	--
	-- 数据包括:
	-- env.protectionLevels - 标题对象的保护级别表
	-- env.subjectSpace - 标题主题命名空间的编号
	-- env.docSpace - 标题放置其文档的命名空间的编号
	-- env.docpageBase - /doc、/sandbox和/testcases页面的基础页面文本,带命名空间
	-- env.compareUrl - 比较沙盒与模板的Special:ComparePages页面的URL
	-- 
	-- 所有表查找都通过pcall传递,以便捕获错误。如果发生错误,返回值将为nil。
	--]]
	
	local env, envFuncs = {}, {}

	-- 设置元表。如果触发,我们调用envFuncs表中的相应函数。
	-- 该函数返回的值被缓存在env表中,这样我们不会多次调用任何函数。
	-- (nil不会被缓存。)
	setmetatable(env, {
		__index = function (t, key)
			local envFunc = envFuncs[key]
			if envFunc then
				local success, val = pcall(envFunc)
				if success then
					env[key] = val -- 缓存该值。
					return val
				end
			end
			return nil
		end
	})	

	function envFuncs.title()
		-- 当前页面的标题对象,或通过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()
		--[[
		-- 模板(或模块等)的标题对象。
		-- 消息:
		-- '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()
		--[[
		-- /doc子页面的标题对象。
		-- 消息:
		-- 'doc-subpage' --> 'doc'
		--]]
		local title = env.title
		local docname = args[1] -- 用户指定的文档页面。
		local docpage
		if docname then
			docpage = docname
		else
			docpage = env.docpageBase .. '/' .. message('doc-subpage')
		end
		return mw.title.new(docpage)
	end
	
	function envFuncs.sandboxTitle()
		--[[
		-- /sandbox子页面的标题对象。
		-- 消息:
		-- 'sandbox-subpage' --> 'sandbox'
		--]]
		return mw.title.new(env.docpageBase .. '/' .. message('sandbox-subpage'))
	end
	
	function envFuncs.testcasesTitle()
		--[[
		-- /testcases子页面的标题对象。
		-- 消息:
		-- 'testcases-subpage' --> 'testcases'
		--]]
		return mw.title.new(env.docpageBase .. '/' .. message('testcases-subpage'))
	end
	
	function envFuncs.printTitle()
		--[[
		-- /Print子页面的标题对象。
		-- 消息:
		-- 'print-subpage' --> '打印'
		--]]
		return env.templateTitle:subPageTitle(message('print-subpage'))
	end

	function envFuncs.protectionLevels()
		-- 标题对象的保护级别表。
		return env.title.protectionLevels
	end

	function envFuncs.subjectSpace()
		-- 主题命名空间编号。
		return mw.site.namespaces[env.title.namespace].subject.id
	end

	function envFuncs.docSpace()
		-- 文档命名空间编号。对于大多数命名空间,这与主题命名空间相同。
		-- 但是,文章、文件、MediaWiki或分类命名空间中的页面必须将其
		-- /doc、/sandbox和/testcases页面放在讨论空间中。
		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()
		-- /doc、/sandbox和/testcases子页面的基础页面。
		-- 对于某些命名空间,这是讨论页面,而不是模板页面。
		local templateTitle = env.templateTitle
		local docSpace = env.docSpace
		local docSpaceText = mw.site.namespaces[docSpace].name
		-- 组装链接。docSpace永远不是主命名空间,因此我们可以硬编码冒号。
		return docSpaceText .. ':' .. templateTitle.text
	end
	
	function envFuncs.compareUrl()
		-- 使用[[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	

----------------------------------------------------------------------------
-- 辅助模板
----------------------------------------------------------------------------

p.getModuleWikitext = makeInvokeFunc('_getModuleWikitext')

function p._getModuleWikitext(args, env)
	local currentTitle = mw.title.getCurrentTitle()
	if currentTitle.contentModel ~= 'Scribunto' then return end
	pcall(require, currentTitle.prefixedText) -- if it fails, we don't care
	local moduleWikitext =  package.loaded["Module:Module wikitext"]
	if moduleWikitext then
		return moduleWikitext.main()
	end
end

function p.sandboxNotice(args, env)
	--[=[
	-- 为沙盒页面生成显示在顶部的沙盒通知。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 
	-- 消息:
	-- 'sandbox-notice-image' --> '[[Image:Sandbox.svg|50px|alt=|link=]]'
	-- 'sandbox-notice-blurb' --> '这是$2的$1。'
	-- 'sandbox-notice-diff-blurb' --> '这是$2的$1($3)。'
	-- 'sandbox-notice-pagetype-template' --> '[[w:Wikipedia:Template test cases|模板沙盒]]页面'
	-- 'sandbox-notice-pagetype-module' --> '[[w:Wikipedia:Template test cases|模块沙盒]]页面'
	-- 'sandbox-notice-pagetype-other' --> '沙盒页面'
	-- 'sandbox-notice-compare-link-display' --> '差异'
	-- 'sandbox-notice-testcases-blurb' --> '另请参阅用于$1的配套子页面。'
	-- 'sandbox-notice-testcases-link-display' --> '测试用例'
	-- 'sandbox-category' --> '模板沙盒'
	--]=]
	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.title.equals(title, sandboxTitle)) then
		return nil
	end
	-- 构建传递给{{ombox}}的参数表。我们只需要两个字段:"image"和"text"。
	local omargs = {}
	omargs.image = message('sandbox-notice-image')
	-- 获取文本。我们从开头的提示文本开始,类似于
	-- "这是[[Template:Foo]]的模板沙盒(差异)。"
	local text = ''
	local frame = mw.getCurrentFrame()
	local isPreviewing = frame:preprocess('{{REVISIONID}}') == '' -- True if the page is being previewed.
	local pagetype
	if subjectSpace == 10 then
		pagetype = message('sandbox-notice-pagetype-template')
	elseif subjectSpace == 828 then
		pagetype = message('sandbox-notice-pagetype-module')
	else
		pagetype = message('sandbox-notice-pagetype-other')
	end
	local templateLink = makeWikilink(templateTitle.prefixedText)
	local compareUrl = env.compareUrl
	if isPreviewing or not compareUrl then
		text = text .. message('sandbox-notice-blurb', {pagetype, templateLink})
	else
		local compareDisplay = message('sandbox-notice-compare-link-display')
		local compareLink = makeUrlLink(compareUrl, compareDisplay)
		text = text .. message('sandbox-notice-diff-blurb', {pagetype, templateLink, compareLink})
	end
	-- 如果测试用例页面存在,获取其提示文本。这类似于
	-- "另请参阅用于[[Template:Foo/testcases|测试用例]]的配套子页面。"
	local testcasesTitle = env.testcasesTitle
	if testcasesTitle and testcasesTitle.exists then
		if testcasesTitle.contentModel == "Scribunto" then
			local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display')
			local testcasesRunLinkDisplay = message('sandbox-notice-testcases-run-link-display')
			local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay)
			local testcasesRunLink = makeWikilink(testcasesTitle.talkPageTitle.prefixedText, testcasesRunLinkDisplay)
			text = text .. '<br />' .. message('sandbox-notice-testcases-run-blurb', {testcasesLink, testcasesRunLink})
		else
			local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display')
			local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay)
			text = text .. '<br />' .. message('sandbox-notice-testcases-blurb', {testcasesLink})
		end
	end
	-- 将沙盒添加到沙盒分类中。
	text = text .. makeCategoryLink(message('sandbox-category'))
	omargs.text = text
	omargs.class = message('sandbox-class')
	local ret = '<div style="clear: both;"></div>'
	ret = ret .. messageBox.main('ombox', omargs)
	return ret
end

function p.protectionTemplate(env)
	-- 在右上角生成挂锁图标。
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 消息:
	-- 'protection-template' --> 'pp-template'
	-- 'protection-template-args' --> {docusage = 'yes'}
	local title = env.title
	local protectionLevels
	local protectionTemplate = message('protection-template')
	local namespace = title.namespace
	if not (protectionTemplate and (namespace == 10 or namespace == 828)) then
		-- 如果不在模板或模块命名空间中,则不显示保护模板。
		return nil
	end
	protectionLevels = env.protectionLevels
	if not protectionLevels then
		return nil
	end
	local editLevels = protectionLevels.edit
	local moveLevels = protectionLevels.move
	if moveLevels and moveLevels[1] == 'sysop' or editLevels and editLevels[1] then
		-- 页面受到完全移动保护,或者完全、模板或半保护。
		local frame = mw.getCurrentFrame()
		return frame:expandTemplate{title = protectionTemplate, args = message('protection-template-args', nil, 'table')}
	else
		return nil
	end
end

----------------------------------------------------------------------------
-- 起始框
----------------------------------------------------------------------------

p.startBox = makeInvokeFunc('_startBox')

function p._startBox(args, env)
	--[[
	-- 此函数生成起始框。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 
	-- 实际工作由p.makeStartBoxLinksData和p.renderStartBoxLinks完成,它们创建
	-- [view] [edit] [history] [purge]链接,以及由p.makeStartBoxData和p.renderStartBox
	-- 生成框的HTML。
	--]]
	env = env or p.getEnvironment(args)
	local links
	local content = args.content
	if not content then
		-- 如果文档在模板页面本身,则无需包含链接。
		local linksData = p.makeStartBoxLinksData(args, env)
		if linksData then
			links = p.renderStartBoxLinks(linksData)
		end
	end
	-- 生成起始框的HTML。
	local data = p.makeStartBoxData(args, env, links)
	if data then
		return p.renderStartBox(data)
	else
		-- 用户指定无标题。
		return nil
	end
end

function p.makeStartBoxLinksData(args, env)
	--[[
	-- 对数据进行初始处理以创建[view] [edit] [history] [purge]链接。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 
	-- 消息:
	-- 'view-link-display' --> '查看'
	-- 'edit-link-display' --> '编辑'
	-- 'history-link-display' --> '历史'
	-- 'purge-link-display' --> '清除缓存'
	-- 'file-docpage-preload' --> 'Template:Documentation/preload-filespace'
	-- 'module-preload' --> 'Template:Documentation/preload-module-doc'
	-- 'docpage-preload' --> 'Template:Documentation/preload'
	-- 'create-link-display' --> '创建'
	--]]
	local subjectSpace = env.subjectSpace
	local title = env.title
	local docTitle = env.docTitle
	if not title or not docTitle then
		return nil
	end
	if docTitle.isRedirect then 
		docTitle = docTitle.redirectTarget
	end

	local data = {}
	data.title = title
	data.docTitle = docTitle
	-- 如果 /doc 存在,则显示查看、显示、编辑和清除缓存链接。
	data.viewLinkDisplay = message('view-link-display')
	data.editLinkDisplay = message('edit-link-display')
	data.historyLinkDisplay = message('history-link-display')
	data.purgeLinkDisplay = message('purge-link-display')
	-- 如果 /doc 不存在,则创建链接。
	local preload = args.preload
	if not preload then
		if subjectSpace == 6 then -- 文件命名空间
			preload = message('file-docpage-preload')
		elseif subjectSpace == 828 then -- 模块命名空间
			preload = message('module-preload')
		else
			preload = message('docpage-preload')
		end
	end
	data.preload = preload
	data.createLinkDisplay = message('create-link-display')
	return data
end

function p.renderStartBoxLinks(data)
	--[[
	-- 从数据表生成[view][edit][history][purge]或[create]链接。
	-- @data - 由p.makeStartBoxLinksData生成的数据表
	--]]
	
	local function escapeBrackets(s)
		-- 使用HTML实体转义方括号。
		s = s:gsub('%[', '&#91;') -- 将方括号替换为HTML实体。
		s = s:gsub('%]', '&#93;') -- 将方括号替换为HTML实体。
		return s
	end

	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 = escapeBrackets(ret)
		ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink)
	else
		local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay)
		ret = '[%s]'
		ret = escapeBrackets(ret)
		ret = mw.ustring.format(ret, createLink)
	end
	return ret
end

function p.makeStartBoxData(args, env, links)
	--[=[
	-- 对数据进行初始处理以传递给起始框渲染函数p.renderStartBox。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- @links - 包含[view][edit][history][purge]链接的字符串 - 如果出现错误可能为nil
	--
	-- 消息:
	-- 'documentation-icon-wikitext' --> '[[File:Test Template Info-Icon - Version (2).svg|50px|link=|alt=Documentation icon]]'
	-- 'template-namespace-heading' --> '模板文档'
	-- 'module-namespace-heading' --> '模块文档'
	-- 'file-namespace-heading' --> '摘要'
	-- 'other-namespaces-heading' --> '文档'
	-- 'start-box-linkclasses' --> 'mw-editsection-like plainlinks'
	-- 'start-box-link-id' --> 'doc_editlinks'
	-- 'testcases-create-link-display' --> '创建'
	--]=]
	local subjectSpace = env.subjectSpace
	if not subjectSpace then
		-- 默认使用"其他命名空间"的命名空间,这样即使出现错误也能获得一些输出。
		subjectSpace = 2
	end
	local data = {}
	
	-- Heading
	local heading = args.heading -- 空白值不会被移除。
	if heading == '' then
		-- 如果标题参数已定义但为空,则不显示起始框。
		return nil
	end
	if heading then
		data.heading = heading
	elseif subjectSpace == 10 then -- 模板命名空间
		data.heading = message('template-namespace-heading')
	elseif subjectSpace == 828 then -- Module namespace
		data.heading = message('module-namespace-heading')
	elseif subjectSpace == 6 then -- 文件命名空间
		data.heading = message('file-namespace-heading')
	else
		data.heading = message('other-namespaces-heading')
	end
	
	-- [view][edit][history][purge]或[create]链接的数据。
	if links then
		data.linksClass = message('start-box-linkclasses')
		data.linksId = message('start-box-link-id')
		data.links = links
	end
	
	return data
end

function p.renderStartBox(data)
	-- 渲染起始框的HTML。
	-- @data - 由p.makeStartBoxData生成的数据表。
	local sbox = mw.html.create('div')
	sbox
		:addClass(message('header-div-class'))
		:tag('div')
			:addClass(message('heading-div-class'))
			:wikitext(data.heading)
	local links = data.links
	if links then
		sbox
			:tag('div')
				:addClass(data.linksClass)
				:attr('id', data.linksId)
				:wikitext(links)
	end
	return tostring(sbox)
end

----------------------------------------------------------------------------
-- 文档内容
----------------------------------------------------------------------------

p.content = makeInvokeFunc('_content')

function p._content(args, env)
	-- 显示文档内容
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	env = env or p.getEnvironment(args)
	local docTitle = env.docTitle
	local content = args.content
	if not content and docTitle and docTitle.exists then
		content = args._content or mw.getCurrentFrame():expandTemplate{title = docTitle}
	end
	-- 下面的换行符是必要的,以便文档开头和结尾的"=== 标题 ==="能够正确解析。
	local cbox = mw.html.create('div')
	cbox
		:addClass(message('content-div-class'))
		:wikitext('\n' .. (content or '') .. '\n')
	return tostring(cbox)
end

p.contentTitle = makeInvokeFunc('_contentTitle')

function p._contentTitle(args, env)
	env = env or p.getEnvironment(args)
	local docTitle = env.docTitle
	if not args.content and docTitle and docTitle.exists then
		return docTitle.prefixedText
	else
		return ''
	end
end

----------------------------------------------------------------------------
-- 结束框
----------------------------------------------------------------------------

p.endBox = makeInvokeFunc('_endBox')

function p._endBox(args, env)
	--[=[
	-- 此函数生成结束框(也称为链接框)。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	--]=]
	
	-- 获取环境数据。
	env = env or p.getEnvironment(args)
	local subjectSpace = env.subjectSpace
	local docTitle = env.docTitle
	if not subjectSpace or not docTitle then
		return nil
	end
		
	-- 检查是否应该输出结束框。如果文档存在或者我们在用户、模块或模板命名空间中,默认添加结束框。
	local linkBox = args['link box']
	if linkBox == 'off'
		or not (
			docTitle.exists
			or subjectSpace == 2
			or subjectSpace == 828
			or subjectSpace == 10
		)
	then
		return nil
	end

	-- 组装页脚文本字段。
	local text = ''
	if linkBox then
		text = text .. linkBox
	else
		text = text .. (p.makeDocPageBlurb(args, env) or '') -- "此文档是从[[Foo]]嵌入的。" 
		if subjectSpace == 2 or subjectSpace == 10 or subjectSpace == 828 then
			-- 我们在用户、模板或模块命名空间中。
			-- 添加沙盒和测试用例链接。
			-- "编辑者可以在此模板的沙盒和测试用例页面中进行实验。"
			text = text .. (p.makeExperimentBlurb(args, env) or '')
			text = text .. '<br />'
			if not args.content and not args[1] then
				-- "请将分类添加到/doc子页面。"
				-- 对于内联文档或明确指定的文档页面不显示此消息,
				-- 因为不清楚在哪里添加分类。
				text = text .. (p.makeCategoriesBlurb(args, env) or '')
			end
			text = text .. ' ' .. (p.makeSubpagesBlurb(args, env) or '') --"Subpages of this template"
			local printBlurb = p.makePrintBlurb(args, env) -- Two-line blurb about print versions of templates.
			if printBlurb then
				text = text .. '<br />' .. printBlurb
			end
		end
	end

	local ebox = mw.html.create('div')
	ebox
		:addClass(message('footer-div-class'))
		:wikitext(text)
	return tostring(ebox)
end

function p.makeDocPageBlurb(args, env)
	--[=[
	-- 生成提示文本"此文档是从[[Template:Foo]]嵌入的(编辑,历史)"。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 
	-- 消息:
	-- 'edit-link-display' --> '编辑'
	-- 'history-link-display' --> '历史'
	-- 'transcluded-from-blurb' --> 
	-- '上面的[[w:Wikipedia:Template documentation|文档]] 
	-- 是从$1[[w:Wikipedia:Transclusion|嵌入]]的。'
	-- 'module-preload' --> 'Template:Documentation/preload-module-doc'
	-- 'create-link-display' --> '创建'
	-- 'create-module-doc-blurb' -->
	-- '您可能想要为此[[w:Wikipedia:Lua|Scribunto模块]]$1一个文档页面。'
	--]=]
	local docTitle = env.docTitle
	if not docTitle or args.content then
		return nil
	end
	local ret
	if docTitle.exists then
		-- /doc存在;链接到它。
		local docLink = makeWikilink(docTitle.prefixedText)
		local editUrl = docTitle:fullUrl{action = 'edit'}
		local editDisplay = message('edit-link-display')
		local editLink = makeUrlLink(editUrl, editDisplay)
		local historyUrl = docTitle:fullUrl{action = 'history'}
		local historyDisplay = message('history-link-display')
		local historyLink = makeUrlLink(historyUrl, historyDisplay)
		ret = message('transcluded-from-blurb', {docLink})
			.. ' '
			.. makeToolbar(editLink, historyLink)
			.. '<br />'
	elseif env.subjectSpace == 828 then
		-- /doc不存在;请求创建它。
		local createUrl = docTitle:fullUrl{action = 'edit', preload = message('module-preload')}
		local createDisplay = message('create-link-display')
		local createLink = makeUrlLink(createUrl, createDisplay)
		ret = message('create-module-doc-blurb', {createLink})
			.. '<br />'
	end
	return ret
end

function p.makeExperimentBlurb(args, env)
	--[[
	-- 渲染文本"编辑者可以在此模板的沙盒(编辑 | 差异)和测试用例(编辑)页面中进行实验。"
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 
	-- 消息:
	-- 'sandbox-link-display' --> '沙盒'
	-- 'sandbox-edit-link-display' --> '编辑'
	-- 'compare-link-display' --> '差异'
	-- 'module-sandbox-preload' --> 'Template:Documentation/preload-module-sandbox'
	-- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox'
	-- 'sandbox-create-link-display' --> '创建'
	-- 'mirror-edit-summary' --> '创建$1的沙盒版本'
	-- 'mirror-link-display' --> '镜像'
	-- 'mirror-link-preload' --> 'Template:Documentation/mirror'
	-- 'sandbox-link-display' --> '沙盒'
	-- 'testcases-link-display' --> '测试用例'
	-- 'testcases-edit-link-display'--> '编辑'
	-- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox'
	-- 'testcases-create-link-display' --> '创建'
	-- 'testcases-link-display' --> '测试用例'
	-- 'testcases-edit-link-display' --> '编辑'
	-- 'module-testcases-preload' --> 'Template:Documentation/preload-module-testcases'
	-- 'template-testcases-preload' --> 'Template:Documentation/preload-testcases'
	-- 'experiment-blurb-module' --> '编辑者可以在此模块的$1和$2页面中进行实验。'
	-- 'experiment-blurb-template' --> '编辑者可以在此模板的$1和$2页面中进行实验。'
	--]]
	local subjectSpace = env.subjectSpace
	local templateTitle = env.templateTitle
	local sandboxTitle = env.sandboxTitle
	local testcasesTitle = env.testcasesTitle
	local templatePage = templateTitle.prefixedText
	if not subjectSpace or not templateTitle or not sandboxTitle or not testcasesTitle then
		return nil
	end
	-- 创建链接。
	local sandboxLinks, testcasesLinks
	if sandboxTitle.exists then
		local sandboxPage = sandboxTitle.prefixedText
		local sandboxDisplay = message('sandbox-link-display')
		local sandboxLink = makeWikilink(sandboxPage, sandboxDisplay)
		local sandboxEditUrl = sandboxTitle:fullUrl{action = 'edit'}
		local sandboxEditDisplay = message('sandbox-edit-link-display')
		local sandboxEditLink = makeUrlLink(sandboxEditUrl, sandboxEditDisplay)
		local compareUrl = env.compareUrl
		local compareLink
		if compareUrl then
			local compareDisplay = message('compare-link-display')
			compareLink = makeUrlLink(compareUrl, compareDisplay)
		end
		sandboxLinks = sandboxLink .. ' ' .. makeToolbar(sandboxEditLink, compareLink)
	else
		local sandboxPreload
		if subjectSpace == 828 then
			sandboxPreload = message('module-sandbox-preload')
		else
			sandboxPreload = message('template-sandbox-preload')
		end
		local sandboxCreateUrl = sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload}
		local sandboxCreateDisplay = message('sandbox-create-link-display')
		local sandboxCreateLink = makeUrlLink(sandboxCreateUrl, sandboxCreateDisplay)
		local mirrorSummary = message('mirror-edit-summary', {makeWikilink(templatePage)})
		local mirrorPreload = message('mirror-link-preload')
		local mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = mirrorPreload, summary = mirrorSummary}
		local mirrorDisplay = message('mirror-link-display')
		local mirrorLink = makeUrlLink(mirrorUrl, mirrorDisplay)
		sandboxLinks = message('sandbox-link-display') .. ' ' .. makeToolbar(sandboxCreateLink, mirrorLink)
	end
	if testcasesTitle.exists then
		local testcasesPage = testcasesTitle.prefixedText
		local testcasesDisplay = message('testcases-link-display')
		local testcasesLink = makeWikilink(testcasesPage, testcasesDisplay)
		local testcasesEditUrl = testcasesTitle:fullUrl{action = 'edit'}
		local testcasesEditDisplay = message('testcases-edit-link-display')
		local testcasesEditLink = makeUrlLink(testcasesEditUrl, testcasesEditDisplay)
		testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink)
	else
		local testcasesPreload
		if subjectSpace == 828 then
			testcasesPreload = message('module-testcases-preload')
		else
			testcasesPreload = message('template-testcases-preload')
		end
		local testcasesCreateUrl = testcasesTitle:fullUrl{action = 'edit', preload = testcasesPreload}
		local testcasesCreateDisplay = message('testcases-create-link-display')
		local testcasesCreateLink = makeUrlLink(testcasesCreateUrl, testcasesCreateDisplay)
		testcasesLinks = message('testcases-link-display') .. ' ' .. makeToolbar(testcasesCreateLink)
	end
	local messageName
	if subjectSpace == 828 then
		messageName = 'experiment-blurb-module'
	else
		messageName = 'experiment-blurb-template'
	end
	return message(messageName, {sandboxLinks, testcasesLinks})
end

function p.makeCategoriesBlurb(args, env)
	--[[
	-- 生成文本"请将分类添加到/doc子页面。"
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	-- 消息:
	-- 'doc-link-display' --> '/doc'
	-- 'add-categories-blurb' --> '请将分类添加到$1子页面。'
	--]]
	local docTitle = env.docTitle
	if not docTitle then
		return nil
	end
	local docPathLink = makeWikilink(docTitle.prefixedText, message('doc-link-display'))
	return message('add-categories-blurb', {docPathLink})
end

function p.makeSubpagesBlurb(args, env)
	--[[
	-- 生成"此模板的子页面"链接。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	
	-- 消息:
	-- 'template-pagetype' --> '模板'
	-- 'module-pagetype' --> '模块'
	-- 'default-pagetype' --> '页面'
	-- 'subpages-link-display' --> '此$1的子页面'
	--]]
	local subjectSpace = env.subjectSpace
	local templateTitle = env.templateTitle
	if not subjectSpace or not templateTitle then
		return nil
	end
	local pagetype
	if subjectSpace == 10 then
		pagetype = message('template-pagetype')
	elseif subjectSpace == 828 then
		pagetype = message('module-pagetype')
	else
		pagetype = message('default-pagetype')
	end
	local subpagesLink = makeWikilink(
		'Special:PrefixIndex/' .. templateTitle.prefixedText .. '/',
		message('subpages-link-display', {pagetype})
	)
	return message('subpages-blurb', {subpagesLink})
end

function p.makePrintBlurb(args, env)
	--[=[
	-- 当模板有打印版本可用时生成显示的提示文本。
	-- @args - 用户传递的参数表
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	--
	-- 消息:
	-- 'print-link-display' --> '/打印'
	-- 'print-blurb' --> '此模板的[[Help:Books/for experts#Improving the book layout|打印版本]]'
	--		.. '存在于$1。'
	--		.. ' 如果您对此模板进行了更改,请同时更新打印版本。'
	-- 'display-print-category' --> true
	-- 'print-category' --> '有打印版本的模板'
	--]=]
	local printTitle = env.printTitle
	if not printTitle then
		return nil
	end
	local ret
	if printTitle.exists then
		local printLink = makeWikilink(printTitle.prefixedText, message('print-link-display'))
		ret = message('print-blurb', {printLink})
		local displayPrintCategory = message('display-print-category', nil, 'boolean')
		if displayPrintCategory then
			ret = ret .. makeCategoryLink(message('print-category'))
		end
	end
	return ret
end

----------------------------------------------------------------------------
-- 跟踪分类
----------------------------------------------------------------------------

function p.addTrackingCategories(env)
	--[[
	-- 检查{{documentation}}是否在/doc或/testcases页面上嵌入。
	-- @env - 包含标题对象等的环境表,由p.getEnvironment生成
	--
	-- 消息:
	-- 'display-strange-usage-category' --> true
	-- 'doc-subpage' --> 'doc'
	-- 'testcases-subpage' --> 'testcases'
	-- 'strange-usage-category' --> '使用奇怪{{documentation}}的维基百科页面'
	-- 
	-- 模块命名空间中的/testcases页面不会被分类,因为它们可能自动嵌入了{{documentation}}。
	--]]
	local title = env.title
	local subjectSpace = env.subjectSpace
	if not title or not subjectSpace then
		return nil
	end
	local subpage = title.subpageText
	local ret = ''
	if message('display-strange-usage-category', nil, 'boolean')
		and (
			subpage == message('doc-subpage')
			or subjectSpace ~= 828 and subpage == message('testcases-subpage')
		)
	then
		ret = ret .. makeCategoryLink(message('strange-usage-category'))
	end
	return ret
end

return p