Autolink,即自动链接,用于给模板的参数中的英文转换为对应的中文并显示出来,正如Module:Reverselink能将中文参数转换为英文并提供给模板以显示图片。
本模块主要用于:
数据模块
本模块采用以下子页面中的数据:
- Module:Autolink/Block
- Module:Autolink/Item
- Module:Autolink/Entity
- Module:Autolink/Biome
- Module:Autolink/Effect
- Module:Autolink/Enchantment
- Module:Autolink/Environment
- Module:Autolink/Dungeons
- Module:Autolink/Legends
- Module:Autolink/Exclusive
- Module:Autolink/Earth
- Module:Autolink/Other
这些页面中的每条数据可以按下面2种形式中的任意一种来组织:
['注册名'] = '输出内容',- 只包含输出内容,不包含任何flag,所有flag均采用其默认值。
['注册名'] = { '输出内容', flag1, flag2, ... },- 指定用于控制特殊行为的flag。各flag的顺序及其含义见
grabFlags函数中的定义,目前定义如下:flag1,引用名hideinlist:布尔值,用于控制对应项目是否不在标准译名列表中出现。false为出现,true为不出现,默认值false。flag2,引用名groups:字符串(逗号分隔),用于按组显示特定内容。默认为main(若需要指定其他组,main则默认不包含,需手动填写)。
- 指定用于控制特殊行为的flag。各flag的顺序及其含义见
依赖项
local p = {}
local block = mw.loadData( 'Module:Autolink/Block' )
local item = mw.loadData( 'Module:Autolink/Item' )
local entity = mw.loadData( 'Module:Autolink/Entity' )
local biome = mw.loadData( 'Module:Autolink/Biome' )
local effect = mw.loadData( 'Module:Autolink/Effect' )
local other = mw.loadData( 'Module:Autolink/Other' )
local exclusive = mw.loadData( 'Module:Autolink/Exclusive' )
local earth = mw.loadData( 'Module:Autolink/Earth' )
local dungeons = mw.loadData( 'Module:Autolink/Dungeons' )
local function grabName( tbl, key )
local entry = tbl[key]
if entry == nil then
return nil
end
if type(entry) == 'string' then
return entry
elseif type(entry) == 'table' then
return entry[1]
else
return nil
end
end
local function grabFlags( tbl, key )
local entry = tbl[key]
if entry == nil then
return nil
end
local result = {
hideinlist = false,
groups = 'main',
}
if type(entry) == 'table' then
result.hideinlist = tbl[key][2]
result.groups = tbl[key][3]
end
return result
end
local function resolveGroups( src )
local result = {}
if string.find(src, ',') then
for v in mw.text.gsplit(src, ',') do
local keyname = mw.text.trim(v)
result[keyname] = 1
end
else
result[src] = 1
end
return result
end
-- used by templates, called via #invoke
function p.link( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
end
return p.invlink( args[1] , args[2] , args[3] )
end
function p.invlink( str, mode, type )
local arg = str:gsub('-', ' '):lower()
local be
local lce
-- check for version suffix
if arg:find(' pe$') or arg:find(' be$') then
be = 1
arg = arg:sub(0, -4)
end
if arg:find(' lce$') then
lce = 1
arg = arg:sub(0, -5)
end
local result
local dataSources = {
BiomeSprite = { biome },
BlockSprite = { block, earth.BlockSprite },
DungeonsEffectSprite = { dungeons.DungeonsItemSprite },
DungeonsEmoteSprite = { dungeons.DungeonsEmoteSprite },
DungeonsEnchantmentSprite = { dungeons.DungeonsEnchantmentSprite },
DungeonsEntitySprite = { dungeons.DungeonsEntitySprite },
DungeonsFlairSprite = { dungeons.DungeonsFlairSprite },
DungeonsItemSprite = { dungeons.DungeonsItemSprite },
DungeonsLevelSprite = { dungeons.DungeonsLevelSprite },
DungeonsMiscellaneousSprite = { dungeons.DungeonsMiscellaneousSprite },
EarthEntitySprite = { earth.EarthEntitySprite },
EffectSprite = { effect },
EntitySprite = { entity },
EnvSprite = { other.EnvSprite },
ItemSprite = { item, earth.ItemSprite },
InvSprite = { other.Invsprite, earth.InvSprite },
}
if type and other[type] then
result = grabName(other[type], arg)
end
if result == nil and type and earth[type] then
result = grabName(earth[type], arg)
end
if result == nil and type and dungeons[type] then
result = grabName(dungeons[type], arg)
end
if result == nil and type and type == 'EffectSprite' then
result = grabName(effect, arg)
end
if result == nil and type and type == 'BiomeSprite' then
result = grabName(biome, arg)
end
if result == nil and type and type == 'EntitySprite' then
result = grabName(entity, arg)
end
if result == nil and type and type == 'ItemSprite' then
result = grabName(item, arg)
end
if result == nil then
result = grabName(block, arg) or grabName(item, arg) or grabName(entity, arg) or grabName(biome, arg) or grabName(effect, arg)
end
if result == nil and arg:sub(-1) == 's' then
local singular = arg:sub(0, -2)
result = grabName(block, singular) or grabName(item, singular) or grabName(entity, singular)
end
if result == nil then
for _, list in pairs( other ) do
result = grabName(list, arg)
if result then
break
end
end
end
if result == nil then
for _, list in pairs( dungeons ) do
result = grabName(list, arg)
if result then
break
end
end
end
if result == nil then
result = grabName(exclusive, arg) or str
end
if be then
return p.mode(result, mode, '(基岩版)')
end
if lce then
return p.mode(result, mode, '(原主机版)')
end
return p.mode(result, mode)
end
function p.mode( str, mode, suffix )
if suffix then
if str:find('|') then
str = str .. suffix
else
str = str .. '|' .. str .. suffix
end
end
local index = str:find('|')
-- return the translated part
if index then
if mode == 'nolink' then
return str:sub(index + 1)
end
-- return the page link part
if mode == 'linkonly' then
return str:sub(1, index - 1)
end
end
return str
end
-- list out all entries with the type
function p.list( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
end
local type = args[1]
type = type:lower()
local groups = args.groups or 'main'
groups = groups:lower()
groups = mw.text.trim(groups)
local sprite = nil
local list = nil
local dataRegistry = {
block = { block, 'BlockSprite' },
item = { item, 'ItemSprite' },
entity = { entity, 'EntitySprite' },
biome = { biome, 'BiomeSprite' },
effect = { effect, 'EffectSprite' },
dungeonsitem = { dungeons.DungeonsItemSprite, 'DungeonsItemSprite' },
dungeonsentity = { dungeons.DungeonsEntitySprite, 'DungeonsEntitySprite' },
dungeonsenchantment = { dungeons.DungeonsEnchantmentSprite, 'DungeonsEnchantmentSprite' },
dungeonslevel = { dungeons.DungeonsLevelSprite, 'DungeonsLevelSprite' },
}
if dataRegistry[type] == nil then
return ''
end
list = dataRegistry[type][1]
sprite = dataRegistry[type][2]
if list == nil or sprite == nil then
return ''
end
local spriteids = mw.loadData( 'Module:' .. sprite ).ids
local result = ''
local t = {}
for k, _ in pairs(list) do
local flags = grabFlags(list, k)
if flags.hideinlist == false then
local query_groups = resolveGroups(groups)
local store_groups = resolveGroups(flags.groups)
for current_query, _ in pairs(query_groups) do
if store_groups[current_query] then
table.insert(t, k)
end
end
end
end
table.sort(t)
local limit = 50
local count = 0
local frame = mw.getCurrentFrame()
local itemlist = nil
local header = mw.html.create('tr')
header:tag('th'):wikitext('图标')
header:tag('th'):wikitext('英文名称')
header:tag('th'):wikitext('中文名称')
for _, v in ipairs(t) do
if count == 0 then
if itemlist ~= nil then
result = result .. tostring(itemlist)
end
itemlist = mw.html.create('table')
:addClass('data-table')
:node(header)
end
local row = mw.html.create('tr')
if spriteids[v] or spriteids[mw.ustring.lower( v ):gsub( '[%s%+]', '-' )] then
row:tag('td'):wikitext(frame:expandTemplate{ title = sprite, args = { v } })
else
row:tag('td')
end
local words = {}
v:gsub('[^%s]+', function(w) table.insert(words, w) end)
for k, w in ipairs(words) do
if w ~= 'of' and w ~= 'or' and w ~= 'o\'' then
words[k] = w:gsub('(%l)(.+)', function(a, b) return a:upper() .. b end)
end
end
row:tag('td'):wikitext(tostring(table.concat(words, ' ')))
row:tag('td'):wikitext(p.mode(grabName(list, v), 'nolink'))
itemlist:node(row)
count = count + 1
if count == limit then
count = 0
end
end
result = result .. tostring(itemlist)
return result
end
return p