Module:頂註
跳转到导航
跳转到搜索
本模組用於頂註模板,用法見各模板文檔。
模組內容
hatnote
本模組的返回值。
hatnote.main(frame)
- 對應{{頂註}}。
hatnote.about(frame)
hatnote._about(args)
- 對應{{关于}}。
hatnote.forabout(frame)
hatnote._for(args)
- 對應{{见}}。
hatnote.notabout(frame)
hatnote._not(args)
- 對應{{不是}}。
hatnote.redirect(frame)
hatnote._redirect(args)
- 對應{{重定向}}。
hatnote.catmore(frame)
hatnote._catmore(args)
- 對應{{分类信息}}。
hatnote.generator(content, prefix, main, image)
- 產生「關於……見……」部分。
hatnote.wrapper(content, image)
- 接收內容表
content
和圖片資料表image
,返回頂註結果。 hatnote.image(light, dark, size)
- 處理圖片。
hatnote.getArgs(frame)
- 從框架對象
frame
取得參數並直接加入參數表中,而不使用元表。返回參數表。 hatnote.formatLinks(links)
- 將連結序列表
links
處理爲維基連結,即將表中不包括[[]]
結構的項以[[]]
括上,之後返回序列表。
參見
local hatnote = {}
local ts = require('Module:参数').loadData('Module:頂註/參數')
function hatnote.getArgs(frame, wrappers)
frame = frame or {}
local metaArgs
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then
local parent = frame:getParent()
if not parent then
metaArgs = frame.args
else
local title = parent:getTitle()
local found
if type(wrappers) == 'table' then
for _, v in pairs(wrappers) do
if v == title then
found = true
break
end
end
elseif wrappers == title then
found = true
end
if found then
metaArgs = parent.args
else
metaArgs = frame.args
end
end
end
local args = {}
args.title = mw.title.getCurrentTitle().text
for k, v in pairs(metaArgs or {}) do
args[k] = v
end
return args
end
local function invoke(functio, ...)
local wrappers = { ... }
return function(frame)
local args = hatnote.getArgs(frame, wrappers)
return hatnote[functio](args)
end
end
function hatnote.formatLinks(links)
for i = 1, table.maxn(links) do
local link = links[i]
if link and not mw.ustring.match(link, '[%[%]]') then
links[i] = string.format('[[%s]]', link)
end
end
return links
end
function hatnote.image(light, dark, size)
dark = string.gsub(dark or light or '', '^File:', '')
light = string.gsub(light or dark or '', '^File:', '')
size = size or '25px'
local same = light == dark
if dark ~= '' then
dark = string.format('[[File:%s|%s|link=]]', dark, size)
end
if same then
return dark
end
if light ~= '' then
light = string.format('[[File:%s|%s|link=]]', light, size)
end
return string.format('%s%s',
mw.text.tag('span', { class = ts:msg{ key = '類', sub = '深色圖片' } }, dark),
mw.text.tag('span', { class = ts:msg{ key = '類', sub = '淺色圖片' } }, light)
)
end
hatnote.main = invoke('wrapper', 'Template:頂註', 'Template:顶注')
function hatnote.wrapper(content, image) -- 輸出結果
local imageWikitext, contentWikitext = '', ''
if type(image) == "table" then
imageWikitext = hatnote.image(image.light, image.dark, image.size)
end
if type(content) == "table" then
contentWikitext = mw.text.tag('span', nil, table.concat(content))
else
contentWikitext = mw.text.tag('span', nil, tostring(content or ''))
end
return mw.text.tag('div', { class = 'hatnote hatnote-page' }, string.format('%s%s', imageWikitext, contentWikitext))
end
function hatnote.generator(args, prefix, main, image)
local content = {}
if prefix then
table.insert(content, prefix)
end
local i = 1
repeat
local forabout = args[i]
forabout = forabout ~= '' and forabout or main and string.format(ts:msg('的其他'), main) or ts:msg('其他')
local forsee = args[i + 1]
forsee = forsee ~= '' and forsee or string.format(ts:msg('消歧義'), main or args.title or '')
forsee = hatnote.formatLinks(mw.text.split(forsee, '[>]'))
if #forsee == 2 then
forsee = table.concat(forsee, ts:msg('和'))
else
forsee = table.concat(forsee, ts:msg('、'))
end
table.insert(content, string.format(ts:msg('關於見'), forabout, forsee))
i = i + 2
until i > table.maxn(args)
return hatnote.wrapper(content, image or {
dark = ts:msg{ key = '關於圖片', sub = '深色' },
light = ts:msg{ key = '關於圖片', sub = '淺色' },
size = ts:msg{ key = '關於圖片', sub = '大小' }
})
end
hatnote.about = invoke('_about', 'Template:关于', 'Template:消歧义')
function hatnote._about(args)
local about = table.remove(args, 1) or ''
local prefix = about and string.format(ts:msg('關於'), about)
return hatnote.generator(args, prefix)
end
hatnote.forabout = invoke('generator', 'Template:见')
hatnote._for = hatnote.generator
hatnote.notabout = invoke('_not', 'Template:不是')
function hatnote._not(args)
local content = ''
args = hatnote.formatLinks(args)
local notabout
if args[3] then
notabout = mw.text.listToText(args, ts:msg('、'), ts:msg('、') .. ts:msg('或'))
else
notabout = table.concat(args, ts:msg('或'))
end
notabout = notabout ~= '' and notabout or '[[]]'
content = string.format(ts:msg('不是'), notabout)
return hatnote.wrapper(content, {
dark = ts:msg{ key = '不是圖片', sub = '深色' },
light = ts:msg{ key = '不是圖片', sub = '淺色' },
size = ts:msg{ key = '不是圖片', sub = '大小' }
})
end
hatnote.redirect = invoke('_redirect', 'Template:重定向', 'Template:重新導向')
function hatnote._redirect(args)
local redirect = table.remove(args, 1) or ''
local prefix = redirect and string.format(ts:msg('重定向'), redirect)
return hatnote.generator(args, prefix, redirect)
end
-- 分類更多資訊
hatnote.catmore = invoke('_catmore', 'Template:分类信息', 'Template:分類資訊')
function hatnote._catmore(args)
local content = ''
args = hatnote.formatLinks(args)
local catmore
-- 數量判斷連接詞
if args[3] then
catmore = table.concat(args, ts:msg('、'))
else
catmore = table.concat(args, ts:msg('和'))
end
catmore = catmore ~= '' and catmore or '[[]]' -- 防止爲空
content = string.format(ts:msg('分類資訊'), catmore)
return hatnote.wrapper(content)
end
return hatnote