无编辑摘要 |
标签:撤销 |
||
第36行: | 第36行: | ||
-- core function |
-- core function |
||
function p.core( str , mode ) |
function p.core( str , mode ) |
||
− | local arg = string.lower(string.gsub(str , "-" , " |
+ | local arg = string.lower(string.gsub(str , "-" , " ")) |
local be |
local be |
||
local lce |
local lce |
2018年12月5日 (三) 14:14的版本
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 other = mw.loadData( 'Module:Autolink/Other' )
local exclusive = mw.loadData( 'Module:Autolink/Exclusive' )
local colors = { 'white ', 'orange ', 'magenta ', 'light blue ', 'yellow ', 'lime ', 'pink ', 'gray ', 'light gray ', 'cyan ', 'purple ', 'blue ', 'brown ', 'green ', 'red ', 'black ', 'silver ' }
local colorc = { '白色', '橙色', '品红色', '淡蓝色', '黄色', '黄绿色', '粉红色', '灰色', '淡灰色', '青色', '紫色', '蓝色', '棕色', '绿色', '红色', '黑色', '淡灰色'}
local function Set (list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
local coloredItems = Set { 'firework star', 'hardened clay', 'stained clay', 'banner', 'carpet', 'concrete', 'concrete powder', 'glazed terracotta', 'terracotta', 'shield', 'shulker box', 'stained glass', 'stained glass pane', 'wool', 'bed', 'hardened glass', 'hardened stained glass', 'balloon', 'glow stick', 'hardened glass pane', 'hardened glass', 'sparkler' }
-- 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.core( args[1] , args[2] )
end
-- used by other modules, called via require()
function p.xlink( str , mode )
if str == nil then
return nil
end
local arg = string.lower(str)
return p.core( str, mode )
end
-- core function
function p.core( str , mode )
local arg = string.lower(string.gsub(str , "-" , " "))
local be
local lce
if string.sub(arg, -3) == ' pe' or string.sub(arg, -3) == ' be' then
be = 1
arg = string.sub(arg, 0, -4)
end
if string.sub(arg, -4) == ' lce' then
lce = 1
arg = string.sub(arg, 0, -5)
end
local color
for i, c in ipairs( colors ) do
if string.find(arg, c) == 1 then
local item = string.sub(arg, string.len(c)+1)
if coloredItems[item] then
color = colorc[i]
arg = item
end
end
end
local result = block[arg] or item[arg]
if result == nil and string.sub(arg, -1) == 's' and string.len(arg) > 1 then
local singular = string.sub(arg, 0, -2)
result = block[singular] or item[singular]
end
if result == nil then
--check for spawn egg
if string.len(arg) < 10 or string.find(arg,' spawn egg',-10,true) == nil then
result = other[arg]
else
local mob = other[string.sub(arg,0,-11)]
if mob == nil then
error('missing mob entry: ' .. string.sub(arg,0,-11))
end
index = string.find(mob,'|',0,true)
if index ~= nil then
mob = string.sub(mob, index+1)
end
result = '刷怪蛋|' .. mob .. '刷怪蛋'
end
end
if result == nil then
result = exclusive[arg]
end
local index
if result == nil then
result = str
index = string.find(result,'|',0,true)
else
if color then
result = result .. '|' .. color .. result
end
index = string.find(result,'|',0,true)
if be then
if index then
result = result .. '(基岩版)'
else
result = result .. '|' .. result .. '(基岩版)'
index = string.find(result,'|',0,true)
end
end
if lce then
if index then
result = result .. '(原主机版)'
else
result = result .. '|' .. result .. '(原主机版)'
index = string.find(result,'|',0,true)
end
end
end
if mode and index then
-- return the fully translated part
if mode == 'nolink' then
result = string.sub(result, index+1)
end
-- return the page link part
if mode == 'linkonly' then
result = string.sub(result, 1 , index-1)
end
end
return result
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 = string.lower(type)
local list = nil
if type == 'block' then
list = block
end
if type == 'item' then
list = item
end
if type == 'other' then
list = other
end
if list == nil then
return ''
end
local result = ''
local t = {}
for k,_ in pairs(list) do
table.insert(t, k)
end
table.sort(t)
local limit = 50
local count = 0
local table = nil
local header = mw.html.create('tr')
header:tag('th'):wikitext('英文名称')
header:tag('th'):wikitext('中文名称')
for _,v in ipairs(t) do
if count == 0 then
if table ~= nil then
result = result .. tostring(table)
end
table = mw.html.create('table'):attr('align', 'left'):css('float', 'left'):css('margin', '2px'):css('border', '1px solid silver'):css('text-align', 'center'):css('background-color', '#fff'):node(header)
end
local row = mw.html.create('tr')
row:tag('td'):wikitext((v:gsub('(%l)(%w*)', function(a,b) return a:upper()..b end)))
row:tag('td'):wikitext(p.xlink(v, 'nolink'))
table:node(row)
count = count + 1
if count == limit then
count = 0
end
end
result = result .. tostring(table)
return result
end
return p