跳转到内容

模組:Labelled list hatnote

被永久保护的模块
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

此模块提供了几个函数来实现带有标签的页面列表提示,例如 {{hatnote|LABEL: [[A]], [[B]], and [[C]]}}。 此模块实现了一个功能,用于创建带有标签的页面���表提示,类似于 "{{see also}}" 等模板的效果。它包括两个主要函数:preprocessDisplays 函数用于将显示参数预先组合成页面参数,并在处理过程中压缩稀疏数组;labelledList 函数处理模板调用,接受单数和可选的复数标签参数,并生成带标签的页面列表提示。

使用方法

labelledList

调用 labelledList() 函数足以实现大多数类似的模板: {{#invoke list hatnote|labelledList|通用标签}} 或者 {{#invoke list hatnote|labelledList|单数标签|复数标签}} 例如,用 "See also" 替代 "通用标签" 可以复制 {{see also}} 的功能,用 "Main article" 和 "Main articles" 替代 "单数标签" 和 "复数标签" 可以复制 {{main}} 的功能(在文章命名空间)。

preprocessDisplays

preprocessDisplays() 函数接受一个原始参数列表,并组合任何显示参数。例如,{{see also|1|l1=One}} 最初的参数表为 {'1', ['l1'] = 'One'};此函数会将其组合成 {'1|One'}。它会覆盖手动的管道符(例如 {{see also|1{{!}}2|l1=One}}{'1|One'}),并压缩稀疏数组,如果参数被跳过或留空。

示例:
 local mLabelledList = require('Module
list hatnote') local pages = mLabelledList.preprocessDisplays(args)

_labelledList

对于需要稍微修改功能但仍使用它的模块,_labelledList() 提供了一些灵活性。它接受三个参数: 页面列表,最好由 preprocessDisplays 预处理和压缩 标签表,第一项为单数或通用标签,第二项为复数标签或第一项的副本 选项表,最好包含:

    • template 字符串,包含模板的完整标题。默认为本模块的标题。 #* category 字符串(或 nil),如由 [[Module

]] 的 makeWikitextError 接受,可选择禁用错误分类 #* selfref 字符串(或 nil),如由 _hatnote 接受,可启用自引用选项

示例:
 local mLabelledList = require('Module
list hatnote') return mLabelledList._labelledList(pages, labels, options)

== 错误 == 如果模板基于此模块,并且没有提供页面名称作为模板参数,则会产生错误消息。通常情况下,这些应指回该模板的 "错误" 部分的文档。然而,如果这些模板使用了一个具有 _labelledList() 并且没有在选项表中提供 template 项的模块,那么该错误将默认指回这里。可以通过为相应模板提供至少一个有效的页面名称参数来解决错误;可以通过为 _labelledList()options 表的 template 项提供一些值来修复模板中的问题。

--------------------------------------------------------------------------------
--                               Labelled list                                --
--                                                                            --
-- This module does the core work of creating a hatnote composed of a list    --
-- prefixed by a colon-terminated label, i.e. "LABEL: [andList of pages]",    --
-- for {{see also}} and similar templates.                                    --
--------------------------------------------------------------------------------

local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
local mArguments --initialize lazily
local p = {}

-- Defaults global to this module
-- 本地化注意
local defaults = {
	label = '参见', --Final fallback for label argument
	labelForm = '%s:%s',
	prefixes = {'label', 'label ', 'l'},
	template = 'Module:Labelled list hatnote'
}

-- Helper function that pre-combines display parameters into page arguments.
-- Also compresses sparse arrays, as a desirable side-effect.
function p.preprocessDisplays (args, prefixes)
	-- Prefixes specify which parameters, in order, to check for display options
	-- They each have numbers auto-appended, e.g. 'label1', 'label 1', & 'l1'
	prefixes = prefixes or defaults.prefixes
	local pages = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			local display
			for i = 1, #prefixes do
				display = args[prefixes[i] .. k]
				if display then break end
			end
			local page = display and
				string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
			pages[#pages + 1] = page
		end
	end
	return pages
end

-- Produces a labelled pages-list hatnote.
-- The main frame (template definition) takes 1 or 2 arguments, for a singular
-- and (optionally) plural label respectively:
-- * {{#invoke:Labelled list hatnote|labelledList|Singular label|Plural label}}
-- The resulting template takes pagename & label parameters normally.
function p.labelledList (frame)
	mArguments = require('Module:Arguments')
	local labels = {frame.args[1] or defaults.label}
	labels[2] = frame.args[2] or labels[1]
	local template = frame:getParent():getTitle()
	local args = mArguments.getArgs(frame, {parentOnly = true})
	local pages = p.preprocessDisplays(args)
	local options = {
		extraclasses = frame.args.extraclasses,
		category = args.category,
		selfref = frame.args.selfref or args.selfref,
		template = template
	}
	return p._labelledList(pages, labels, options)
end

function p._labelledList (pages, labels, options)
	labels = labels or {}
	if #pages == 0 then
		-- 本地化注意
		return mHatnote.makeWikitextError(
			'未指定页面名称',
			(options.template or defaults.template) .. '#错误',
			options.category
		)
	end
	label = (#pages == 1 and labels[1] or labels[2]) or defaults.label
	local text = string.format(
		options.labelForm or defaults.labelForm,
		label,
		mHatlist.andList(pages, true)
	)
	local hnOptions = {
		extraclasses = options.extraclasses,
		selfref = options.selfref
	}
	return mHatnote._hatnote(text, hnOptions)
end

return p