本模块为《原神》《崩坏:星穹铁道》《绝区零》等专题通用的模块,可封装成各专题的角色链接模板,用于为游戏中的可控角色创建带颜色或不带颜色的内部链接,以免在编写条目时处理繁琐的消歧义。
database:数据库名称,对应Module:角色链接/data/xxx中的xxx。mode:输出模式,可选值如下:
plain:无颜色模式,生成不带颜色的普通内链。image:图片模式,生成含内链的图片。sep:多个角色链接之间的分隔符,默认为空格 。color:颜色模板名称,用于常规模式,默认为color(即{{color}})。imageFormat:图像文件名格式,用于图片模式,默认为%s。以上参数均为parentFirst(可在调用模块编写模板时传递参数,也可在调用模板时传递参数,后者优先)。
本模板通过外部数据模块(Module:角色链接/data/xxx)管理角色数据,该模块应返回一个Lua表,其键为角色名,值为包含角色信息的子表。每个角色信息子表可包含以下字段:
[1]或color:用于着色的颜色值,用于常规模式。[2]或link:角色条目名,用于所有模式。display:链接的默认显示文本,用于常规和plain模式。若未提供,则使用角色名作为默认显示文本。image:图像文件名,用于image模式。如要为某部游戏创建角色链接模板,可参考以下步骤操作:
| 步骤 |
|---|
|
调用角色链接模板时,将要创建链接的角色名作为参数传入,即:{{角色链接模板|角色名1|角色名2|角色名3}}。
模板基础功能如下:
[[条目名|{{颜色模板|颜色|角色名}}]]。[[条目名|{{颜色模板|颜色|显示内容}}]]。sep参数指定的分隔符。调用角色链接模板时,可用,(全角逗号)或,(半角逗号)重新指定显示内容,即:{{角色链接模板|角色名,显示内容}}。
,,指定的显示内容优先级最高,将覆盖数据库提供的显示内容。在此基础上,还可用@重新指定颜色,即:{{角色链接模板|条目名,显示内容@颜色}}。
@指定颜色时,将跳过数据库查询,直接用指定的条目名、显示内容和颜色构建链接。@,即:{{角色链接模板|文字@颜色}}。此时将只会生成带颜色的文字,不会生成链接。[[条目名|角色名]]。image,或使用,,提供文件名,即:{{角色链接模板|角色名,文件名}}。输出格式将为:[[File:文件名|link=条目名]]。imageFormat参数用于将数据库中记录的文件名补全为实际的文件名,例如可将imageFormat设置为Icon_%s.png,模块会在输出图片时将%s替换为数据库中对应角色的image字段。,,手动指定文件名时,imageFormat参数不会生效。{{!}}直接写在imageFormat参数或手动指定的文件名后,如:{{角色链接模板|角色名,文件名{{!}}50px}}。plain和image模式下,由@指定的颜色将被忽略,@仅作为跳过查表、手动指定条目名的标志。此时参数可略作:{{角色链接模板|条目名,显示内容@}}。
| 参数格式 | 查表情况 | 各模式下输出格式 | ||
|---|---|---|---|---|
| (常规) | plain | image | ||
name
|
dbColor,link,dbDisplay
|
[[link|{{ColorTemplate|dbColor|dbDisplay}}]]
|
[[link|dbDisplay]]
|
[[File:image|link=link]](若查到 image数据)
|
dbColor,link
|
[[link|{{ColorTemplate|dbColor|name}}]]
|
[[link|name]]
| ||
| 无对应角色 | name
|
name
|
无输出 | |
name,display
|
dbColor,link,dbDisplay
|
[[link|{{ColorTemplate|dbColor|display}}]]
|
[[link|display]]
|
[[File:display|link=link]]
|
dbColor,link
| ||||
| 无对应角色 | display
|
display
|
[[File:display|link=]]
| |
name@color
|
不查表 | {{ColorTemplate|color|name}}
|
name
|
无输出 |
name,display@color
|
不查表 | [[name|{{ColorTemplate|color|display}}]]
|
[[name|display]]
|
[[File:display|link=name]]
|
以{{原神角色链接}}为例:
{{原神角色链接|sep=、|安柏|达达利亚,公子|特瓦林,东风之龙@风|西风骑士团,西风骑士团@风|原石@五星}}
(可将鼠标放在链接上,查看左下角显示的链接地址)
相当于代码:
[[安柏(原神)|{{genshincolor|火|安柏}}]]、[[达达利亚|{{genshincolor|水|公子}}]]、[[特瓦林|{{genshincolor|风|东风之龙}}]]、[[西风骑士团|{{genshincolor|风|西风骑士团}}]]、{{genshincolor|五星|原石}}
由,,指定的显示内容并不限于文字,也可包含模板或HTML标签等。“颜色模板”也未必只能是颜色模板,可按实际需要在模板中添加各种格式。
在显示内容中添加HTML标签时,可能会因HTML标签被提前解析而出现模板无法正常工作的情况,此时可将HTML标签包裹在<nowiki>标签中(本模块已添加了相关处理),例如:
{{原神角色链接|派蒙,<nowiki><span style="font-size:1.2em;">最好的伙伴!</span></nowiki>}}
效果:最好的伙伴!
编写条目时,添加如下代码:
{{subst:#invoke:角色链接|main|database=数据库名|mode=plain|角色名}}
则当保存页面时,这段代码将被自动替换为已完成消歧义且未上色的角色链接。因为过于复杂,不建议人类使用。
| 模板 | 数据库 |
|---|---|
| {{原神角色链接}} | Module:角色链接/data/原神 |
| {{崩铁角色链接}} | Module:角色链接/data/崩铁 |
| {{绝区零角色链接}} | Module:角色链接/data/绝区零 |
| {{鸣潮角色链接}} | Module:角色链接/data/鸣潮 |
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p.main(frame)
local args = getArgs(frame, {parentFirst = true})
local data
if args.database then
data = mw.loadData('Module:角色链接/data/' .. args.database)
end
local sep = args.sep or ' '
local mode = mw.ustring.lower(args.mode or '')
local colorTemplate = args.color or 'color'
local imageFormat = args.imageFormat or '%s'
local result = {}
-- 调用颜色模板
local function callColorTemplate(color, text)
return frame:expandTemplate{title=colorTemplate, args={color, text}}
end
-- 分割输入
local function splitInput(value)
local name, display, color, text
-- 按第一个@分割
local atPos = mw.ustring.find(value, '@')
if atPos then
text = mw.ustring.sub(value, 1, atPos - 1)
color = mw.ustring.sub(value, atPos + 1)
else
text = value
color = nil
end
-- 按第一个逗号分割
local commaPos = mw.ustring.find(text, '[,,]')
if commaPos then
name = mw.ustring.sub(text, 1, commaPos - 1)
display = mw.ustring.sub(text, commaPos + 1)
else
name = text
display = ''
end
return name, display, color
end
-- 处理单个参数
local function processParam(value)
if mw.ustring.find(value, '%[%[') or mw.ustring.find(value, '%]%]') then
return frame:expandTemplate{title='error', args={'禁止使用内链。'}}
end
local name, display, color = splitInput(value)
-- name为空 → 返回空
if name == '' then
return ''
end
-- image模式
if mode == 'image' then
-- 含@ → 不查表
if color then
return string.format('[[File:%s|link=%s]]', display, name)
end
-- 不含@ → 查表
local entry = data and data[name]
if not entry then
return ''
end
local link = entry[2] or entry.link
local image
if display ~= '' then
image = display
elseif entry.image then
image = string.format(imageFormat, entry.image)
end
if image and image ~= '' then
return string.format('[[File:%s|link=%s]]', image, link)
else
return ''
end
end
-- 含@ → 不查表
if color then
if mode == '' then
if display == '' then
return callColorTemplate(color, name)
else
return string.format('[[%s|%s]]', name, callColorTemplate(color, display))
end
elseif mode == 'plain' then
if display == '' then
return name
elseif name == display then
return string.format('[[%s]]', name)
else
return string.format('[[%s|%s]]', name, display)
end
else
return ''
end
end
-- 不含@ → 查表
local entry = data and data[name]
if not entry then
return display ~= '' and display or name
end
local dbColor = entry[1] or entry.color
local link = entry[2] or entry.link
local dbDisplay = entry.display
local displayText
if display ~= '' then
displayText = display
elseif dbDisplay and dbDisplay ~= '' then
displayText = dbDisplay
else
displayText = name
end
if mode == '' then
return string.format('[[%s|%s]]', link, callColorTemplate(dbColor, displayText))
elseif mode == 'plain' then
if link == displayText then
return string.format('[[%s]]', link)
else
return string.format('[[%s|%s]]', link, displayText)
end
else
return ''
end
end
-- 处理所有参数
for i, value in ipairs(args) do
value = mw.text.unstripNoWiki(value)
value = mw.text.decode(value)
if value ~= nil and value ~= '' then
local processed = processParam(value)
if processed ~= '' then
table.insert(result, processed)
end
end
end
return table.concat(result, sep)
end
return p