更多操作
小 (拆分) |
小 (导入21个版本) |
||
(未显示同一用户的12个中间版本) | |||
第1行: | 第1行: | ||
local p = {} |
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 的元数据, 处理别名 |
-- 获取该名称 emoji 的元数据, 处理别名 |
||
local |
local name, var = p.separateNameAndVar(nameWithVar) |
||
local level = 0 |
|||
while true do |
while true do |
||
if level == 10 then |
|||
return 'emoji别名的嵌套级别超过10级, 放弃', false |
|||
if type(meta) == 'table' then |
|||
return meta, true |
|||
elseif meta == nil then |
|||
return frame:expandTemplate { |
|||
title = '?', |
|||
args = { |
|||
'[[help:emoji|❓]]', |
|||
'emoji遗失, 名称: ' .. name, |
|||
}, |
|||
}, false |
|||
elseif type(meta) ~= 'string' then |
|||
return frame:expandTemplate { |
|||
title = '?', |
|||
args = { |
|||
'[[help:emoji|❓]]', |
|||
'emoji配置不正确, 类型: ' .. type(meta), |
|||
}, |
|||
}, false |
|||
end |
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 |
||
end |
end |
||
function p.resolveVariety(meta, varietyName) |
|||
-- 处理变种 (如果指定了) |
-- 处理变种 (如果指定了) |
||
-- 变种暂时不支持别名 (懒) |
-- 变种暂时不支持别名 (懒) |
||
meta = (meta. |
meta = (meta.varieties or {})[varietyName] |
||
if meta == nil then |
if meta == nil then |
||
return 'emoji变种遗失, 变种名称: ' .. varietyName, false |
|||
return frame:expandTemplate { |
|||
title = '?', |
|||
args = { |
|||
'[[help:emoji|❓]]', |
|||
'emoji变种遗失, 变种名称: ' .. varietyName, |
|||
}, |
|||
}, false |
|||
elseif type(meta) == 'string' then |
elseif type(meta) == 'string' then |
||
return |
return 'emoji变种暂未支持别名', false |
||
title = '?', |
|||
args = { |
|||
'[[help:emoji|❓]]', |
|||
'emoji变种暂未支持别名', |
|||
}, |
|||
}, false |
|||
elseif type(meta) ~= 'table' then |
elseif type(meta) ~= 'table' then |
||
return |
return 'emoji变种配置不正确, 类型:' .. type(meta), false |
||
title = '?', |
|||
args = { |
|||
'[[help:emoji|❓]]', |
|||
'emoji变种配置不正确, 类型:' .. type(meta), |
|||
}, |
|||
}, false |
|||
end |
end |
||
return meta, true |
return meta, true |
||
第64行: | 第68行: | ||
function p.emoji() |
function p.emoji() |
||
local frame = mw.getCurrentFrame():getParent() |
local frame = mw.getCurrentFrame():getParent() |
||
local name = frame.args[1] |
local name = mw.text.trim(frame.args[1]) |
||
local var = |
local var = frame.args[2] |
||
if var ~= nil then |
|||
var = mw.text.trim(var) |
|||
end |
|||
if var ~= nil then |
|||
-- 接受两种格式来指定变种: |
|||
name = name .. '/' .. var |
|||
-- {{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 |
end |
||
local metaOrError, succ = getEmojiMeta(data, name) |
local metaOrError, succ = p.getEmojiMeta(p.data.emojis, name) |
||
if not succ then |
if not succ then |
||
return |
return frame:expandTemplate { |
||
title = '?', |
|||
args = { |
|||
'[[help:emoji|❓]]', |
|||
metaOrError, |
|||
}, |
|||
} |
|||
end |
end |
||
local meta = metaOrError |
local meta = metaOrError |
||
if var ~= nil then |
|||
local metaOrError, succ = resolveVariety(meta, var) |
|||
if not succ then |
|||
return metaOrError |
|||
end |
|||
meta = metaOrError |
|||
end |
|||
if type(meta.position) ~= 'table' |
if type(meta.position) ~= 'table' |
||
第105行: | 第103行: | ||
end |
end |
||
local |
local imageName = p.data.texture.image |
||
local row = meta.position[1] |
local row = meta.position[1] |
||
local col = meta.position[2] |
local col = meta.position[2] |
||
local width = data.texture.unitSize[1] |
local width = p.data.texture.unitSize[1] |
||
local height = data.texture.unitSize[2] |
local height = p.data.texture.unitSize[2] |
||
local y = row * height |
local y = row * height |
||
local x = col * width |
local x = col * width |
||
第115行: | 第113行: | ||
return frame:expandTemplate { |
return frame:expandTemplate { |
||
title = 'image crop', |
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') .. ']]', |
|||
}, |
|||
} |
} |
||
2022年9月21日 (三) 23:44的最新版本
此模块的文档可以在模块: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