更多操作
此模块的文档可以在模块:Emoji/doc创建
local p = {}
p.data = require('Module:emoji/data')
function p.getEmojiMeta(data, name)
-- 获取该名称 emoji 的元数据, 处理别名
local meta = name
while true do
mw.log(meta)
meta = data.emojis[meta]
if type(meta) == 'table' then
return meta, true
elseif meta == nil then
return 'emoji遗失, 名称: ' .. name, false
elseif type(meta) ~= 'string' then
return 'emoji配置不正确, 类型: ' .. type(meta), false
end
end
end
function p.resolveVariety(meta, varietyName)
-- 处理变种 (如果指定了)
-- 变种暂时不支持别名 (懒)
meta = (meta.aliases or {})[varietyName]
if meta == nil then
return 'emoji变种遗失, 变种名称: ' .. varietyName, false
elseif type(meta) == 'string' then
return 'emoji变种暂未支持别名', false
elseif type(meta) ~= 'table' then
return 'emoji变种配置不正确, 类型:' .. type(meta), false
end
return meta, true
end
function p.emoji()
local frame = mw.getCurrentFrame():getParent()
local name = frame.args[1]
local var = nil
-- 接受两种格式来指定变种:
-- {{e|名称/变种名}}
-- {{e|名称|变种名}}
local varSepPosi = string.find(name, '/')
if varSepPosi ~= nil then
var = string.sub(name, varSepPosi + 1)
name = string.sub(name, 1, varSepPosi - 1)
else
var = frame.args[2]
end
local metaOrError, succ = p.getEmojiMeta(p.data, name)
if not succ then
return frame:expandTemplate {
title = '?',
args = {
'[[help:emoji|❓]]',
metaOrError,
},
}
end
local meta = metaOrError
if var ~= nil then
local metaOrError, succ = p.resolveVariety(meta, var)
if not succ then
return frame:expandTemplate {
title = '?',
args = {
'[[help:emoji|❓]]',
metaOrError,
},
}
end
meta = metaOrError
end
if type(meta.position) ~= 'table'
or type(meta.position[1]) ~= 'number'
or type(meta.position[2]) ~= 'number'
then
return frame:expandTemplate {
title = '?',
args = {
'[[help:emoji|❓]]',
'emoji位置配置不正确',
},
}
end
local image = p.data.texture.image
local row = meta.position[1]
local col = meta.position[2]
local width = p.data.texture.unitSize[1]
local height = p.data.texture.unitSize[2]
local y = row * height
local x = col * width
return frame:expandTemplate {
title = 'image crop',
args = {
width = tostring(width) .. 'px',
height = tostring(height) .. 'px',
y = tostring(y) .. 'px',
x = tostring(x) .. 'px',
image = '[[File:' .. image .. '|link=help:emoji]]',
},
}
end
return p