模組:Labelled list hatnote
![]() | 此模組已被保護。此為高度可見模組,其已用於大量條目或被頻繁替換引用。由於破壞或失誤會影響諸多頁面,即便細小的改動也可能導致大量伺服器負載,因此已被保護,不可編輯。 |
![]() | 此模組被引用於約27,000個頁面。 為了避免造成大規模的影響,所有對此模組的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
此模塊提供了幾個函數來實現帶有標籤的頁面列表提示,例如 {{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