模块:词典
来自卡厄思梦境WIKI
此模块的文档可以在模块:词典/doc创建
local p = {}
function p.main(frame)
local args = frame:getParent().args
local term = args[1] or ""
local category = args[2] or "" -- 可选参数
-- 加载词典数据
local data = require('Module:词典/data')
local dictionary = data.dictionary
-- 颜色映射(仅用于边框)
local colorMap = {
["白"] = "#ffffff",
["蓝"] = "#0099ff",
["红"] = "#ff3333",
["橙"] = "#f2ba02"
}
-- 查找词条
local entries = dictionary[term]
if not entries then
return '<span style="color: red;">未找到词条: ' .. term .. '</span>'
end
-- 查找匹配的条目
local entry = nil
if category == "" then
-- 如果没有指定类型,使用第一个条目
entry = entries[1]
else
-- 如果指定了类型,查找匹配的类型
for _, e in ipairs(entries) do
if e["类型"] == category then
entry = e
break
end
end
end
if not entry then
return '<span style="color: red;">未找到类型: ' .. category .. ' - ' .. term .. '</span>'
end
-- 获取边框颜色
local lineColor = colorMap[entry["颜色"]] or colorMap["白"]
-- 构建工具提示内容
local tooltipContent = ""
-- 添加图标(如果有)
if entry["icon"] and entry["icon"] ~= "" then
tooltipContent = tooltipContent .. '[[File:' .. entry["icon"] .. '|25px|link=]] '
end
tooltipContent = tooltipContent .. term .. '<br>' .. entry["描述"]
-- 生成唯一ID
local tooltipId = 'tooltip-' .. mw.uri.encode(term .. (category or "")):gsub('[%%%.%-%s]', '')
-- 将tooltip内容存储在data属性中
local tooltipHtml = '<div style="width: 200px; height: 2px; background-color: ' .. lineColor .. ';"></div>' ..
'<div style="width: 200px; background-color: #343434; color: white; padding: 5px; box-shadow: 0 2px 8px rgba(0,0,0,0.3);">' ..
tooltipContent .. '</div>'
-- 生成HTML
local html = mw.html.create('span')
:addClass('dictionary-term')
:attr('data-tooltip-content', tooltipHtml)
:attr('data-tooltip-id', tooltipId)
:css({
['text-decoration'] = 'underline',
['cursor'] = 'help',
['display'] = 'inline-block'
})
:wikitext(term)
return tostring(html)
end
return p