更多操作
此模块的文档可以在模块:Emoji/doc创建
local p = {}
p.data = require('Module:emoji/data')
function p.separateNameAndVar(nameWithVar)
-- 接受两种格式来指定变种:
-- {{e|名称/变种名}}
-- {{e|名称|变种名}}
nameWithVar = string.lower(nameWithVar)
local varSepPosi = string.find(nameWithVar, '/')
if varSepPosi ~= nil then
var = string.sub(nameWithVar, varSepPosi + 1)
name = string.sub(nameWithVar, 1, varSepPosi - 1)
return name, var
end
return nameWithVar, nil
end
function p.getEmojiMeta(emojis, nameWithVar)
-- 获取该名称 emoji 的元数据, 处理别名
local name, var = p.separateNameAndVar(nameWithVar)
local level = 0
while true do
if level == 10 then
return 'emoji别名的嵌套级别超过10级, 放弃', false
end
level = level + 1
emojiMetaOrAlias = emojis[name]
if type(emojiMetaOrAlias) == 'table' then
local emojiMeta = emojiMetaOrAlias
if var ~= nil then
return p.resolveVariety(emojiMeta, var)
end
return emojiMeta, true
elseif emojiMetaOrAlias == nil then
return 'emoji遗失, 名称: ' .. name, false
elseif type(emojiMetaOrAlias) ~= 'string' then
return 'emoji配置不正确, 类型: ' .. type(emojiMetaOrAlias), false
end
local alias = emojiMetaOrAlias
newName, newVar = p.separateNameAndVar(alias)
if newVar ~= nil and var ~= nil then
return '在已解析到emoji变种名的情况下, 解析emoji别名时解析到了新的变种名, 旧: ' .. var .. '; 新: ' .. newVar, false
elseif newVar ~= nil then
var = newVar
end
name = newName
end
end
function p.resolveVariety(meta, varietyName)
-- 处理变种 (如果指定了)
-- 变种暂时不支持别名 (懒)
meta = (meta.varieties 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 = mw.text.trim(frame.args[1])
local var = frame.args[2]
if var ~= nil then
var = mw.text.trim(var)
end
if var ~= nil then
name = name .. '/' .. var
end
local metaOrError, succ = p.getEmojiMeta(p.data.emojis, name)
if not succ then
return frame:expandTemplate {
title = '?',
args = {
'[[help:emoji|❓]]',
metaOrError,
},
}
end
local meta = metaOrError
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 imageName = 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:' .. imageName .. '|link=' .. (frame.args['link'] or 'help:emoji') .. ']]',
},
}
end
return p