Module:SpellHover: Difference between revisions
Created page with "local p = {} local classAbbr = { ["Bard"] = "BRD", ["Beastlord"] = "BST", ["Berserker"] = "BER", ["Cleric"] = "CLR", ["Druid"] = "DRU", ["Enchanter"] = "ENC", ["Magician"] = "MAG", ["Monk"] = "MNK", ["Necromancer"] = "NEC", ["Paladin"] = "PAL", ["Ranger"] = "RNG", ["Rogue"] = "ROG", ["Shadow Knight"] = "SHD", ["Shaman"] = "SHM", ["Warrior"] = "WAR", ["Wizard"] = "WIZ" } local function trim(value) if value == nil then return "" end return mw.text.t..." |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 50: | Line 50: | ||
-- Remove simple bold/italic markup. | -- Remove simple bold/italic markup. | ||
s = mw.ustring.gsub(s, "''+", "") | s = mw.ustring.gsub(s, "''+", "") | ||
-- Common parser-function leftovers in simple spell-slot text. | |||
s = mw.ustring.gsub(s, "{{#ifeq:[^{}]+}}", "") | |||
s = mw.ustring.gsub(s, "{{#if:[^{}]+}}", "") | |||
-- Very simple template cleanup fallback. | -- Very simple template cleanup fallback. | ||
| Line 274: | Line 278: | ||
local effects = {} | local effects = {} | ||
local function isSlotTemplateStart(pos) | |||
local parts = splitTopLevelPipes( | local chunk = string.sub(slotsText, pos, pos + 80) | ||
local slotNum = stripMarkup(parts[ | local lower = mw.ustring.lower(chunk) | ||
local effectText = stripMarkup(parts[ | |||
return mw.ustring.match(lower, "^{{%s*spellslotrow%s*[|}]") or | |||
mw.ustring.match(lower, "^{{%s*spellslotrowsmart%s*[|}]") | |||
end | |||
local function findNextSlotTemplate(startAt) | |||
local searchAt = startAt | |||
while true do | |||
local pos = string.find(slotsText, "{{", searchAt, true) | |||
if not pos then | |||
return nil | |||
end | |||
if isSlotTemplateStart(pos) then | |||
return pos | |||
end | |||
searchAt = pos + 2 | |||
end | |||
end | |||
local function readBalancedTemplate(startPos) | |||
local depth = 0 | |||
local i = startPos | |||
local len = #slotsText | |||
while i <= len - 1 do | |||
local pair = string.sub(slotsText, i, i + 1) | |||
if pair == "{{" then | |||
depth = depth + 1 | |||
i = i + 2 | |||
elseif pair == "}}" then | |||
depth = depth - 1 | |||
i = i + 2 | |||
if depth == 0 then | |||
return string.sub(slotsText, startPos, i - 1), i | |||
end | |||
else | |||
i = i + 1 | |||
end | |||
end | |||
return "", len + 1 | |||
end | |||
local cursor = 1 | |||
while true do | |||
local startPos = findNextSlotTemplate(cursor) | |||
if not startPos then | |||
break | |||
end | |||
local block, nextPos = readBalancedTemplate(startPos) | |||
if block == "" then | |||
break | |||
end | |||
-- Remove outer {{ }} | |||
local inner = string.sub(block, 3, -3) | |||
local parts = splitTopLevelPipes(inner) | |||
-- parts[1] = SpellSlotRow or SpellSlotRowSmart | |||
-- parts[2] = slot number | |||
-- parts[3] = effect text | |||
local slotNum = stripMarkup(parts[2] or "") | |||
local effectText = stripMarkup(parts[3] or "") | |||
if effectText ~= "" then | if effectText ~= "" then | ||
| Line 286: | Line 362: | ||
end | end | ||
end | end | ||
cursor = nextPos | |||
end | end | ||
if #effects > 0 then | if #effects > 0 then | ||
return table.concat(effects, "<br>") | return table.concat(effects, "<br>") | ||
end | |||
-- Safety fallback: if the source clearly had spell-slot templates but parsing failed, | |||
-- do not leak raw template/parser-function text into the tooltip. | |||
if mw.ustring.find(slotsText, "SpellSlotRow") or mw.ustring.find(slotsText, "SpellSlotRowSmart") then | |||
return "" | |||
end | end | ||
Latest revision as of 02:59, 22 May 2026
Documentation for this module may be created at Module:SpellHover/doc
local p = {}
local classAbbr = {
["Bard"] = "BRD",
["Beastlord"] = "BST",
["Berserker"] = "BER",
["Cleric"] = "CLR",
["Druid"] = "DRU",
["Enchanter"] = "ENC",
["Magician"] = "MAG",
["Monk"] = "MNK",
["Necromancer"] = "NEC",
["Paladin"] = "PAL",
["Ranger"] = "RNG",
["Rogue"] = "ROG",
["Shadow Knight"] = "SHD",
["Shaman"] = "SHM",
["Warrior"] = "WAR",
["Wizard"] = "WIZ"
}
local function trim(value)
if value == nil then
return ""
end
return mw.text.trim(tostring(value))
end
local function html(value)
return mw.text.encode(trim(value))
end
local function stripMarkup(value)
local s = trim(value)
if s == "" then
return ""
end
s = mw.ustring.gsub(s, "<br%s*/?>", " ")
s = mw.ustring.gsub(s, "<[^>]+>", "")
-- [[Page|Label]] -> Label
s = mw.ustring.gsub(s, "%[%[[^%]|]+|([^%]]+)%]%]", "%1")
-- [[Page]] -> Page
s = mw.ustring.gsub(s, "%[%[([^%]|]+)%]%]", "%1")
-- Remove simple bold/italic markup.
s = mw.ustring.gsub(s, "''+", "")
-- Common parser-function leftovers in simple spell-slot text.
s = mw.ustring.gsub(s, "{{#ifeq:[^{}]+}}", "")
s = mw.ustring.gsub(s, "{{#if:[^{}]+}}", "")
-- Very simple template cleanup fallback.
s = mw.ustring.gsub(s, "{{%s*([^{}|]+)%s*|%s*([^{}]+)%s*}}", "%2")
s = mw.ustring.gsub(s, "{{[^{}]+}}", "")
s = mw.ustring.gsub(s, "%s+", " ")
return trim(s)
end
local function formatSeconds(value)
local s = stripMarkup(value)
if s == "" then
return ""
end
if tonumber(s) ~= nil then
return s .. " sec"
end
return s
end
local function findTemplateBlock(text, names)
if not text or text == "" then
return ""
end
local lowerText = mw.ustring.lower(text)
for _, name in ipairs(names) do
local lowerName = mw.ustring.lower(name)
local startPos = mw.ustring.find(lowerText, "{{%s*" .. lowerName)
if startPos then
local depth = 0
local i = startPos
local len = #text
while i <= len - 1 do
local pair = string.sub(text, i, i + 1)
if pair == "{{" then
depth = depth + 1
i = i + 2
elseif pair == "}}" then
depth = depth - 1
i = i + 2
if depth == 0 then
return string.sub(text, startPos, i - 1)
end
else
i = i + 1
end
end
end
end
return ""
end
local function splitTopLevelPipes(text)
local parts = {}
local current = {}
local templateDepth = 0
local linkDepth = 0
local i = 1
local len = #text
while i <= len do
local pair = string.sub(text, i, i + 1)
local ch = string.sub(text, i, i)
if pair == "{{" then
templateDepth = templateDepth + 1
table.insert(current, pair)
i = i + 2
elseif pair == "}}" and templateDepth > 0 then
templateDepth = templateDepth - 1
table.insert(current, pair)
i = i + 2
elseif pair == "[[" then
linkDepth = linkDepth + 1
table.insert(current, pair)
i = i + 2
elseif pair == "]]" and linkDepth > 0 then
linkDepth = linkDepth - 1
table.insert(current, pair)
i = i + 2
elseif ch == "|" and templateDepth == 0 and linkDepth == 0 then
table.insert(parts, trim(table.concat(current)))
current = {}
i = i + 1
else
table.insert(current, ch)
i = i + 1
end
end
table.insert(parts, trim(table.concat(current)))
return parts
end
local function parseTemplateParams(block)
local params = {}
block = trim(block)
if mw.ustring.sub(block, 1, 2) == "{{" then
block = mw.ustring.sub(block, 3)
end
if mw.ustring.sub(block, -2) == "}}" then
block = mw.ustring.sub(block, 1, -3)
end
local parts = splitTopLevelPipes(block)
-- First part is the template name.
table.remove(parts, 1)
for _, part in ipairs(parts) do
local eq = mw.ustring.find(part, "=")
if eq then
local key = trim(mw.ustring.sub(part, 1, eq - 1))
local value = trim(mw.ustring.sub(part, eq + 1))
if key ~= "" then
params[key] = value
end
end
end
return params
end
local function readSpellParams(spellName)
local title = mw.title.new(spellName)
if not title then
return nil
end
local content = title:getContent()
if not content or content == "" then
return nil
end
local block = findTemplateBlock(content, {
"Spellpagesmart",
"Spellpage",
"spellpage"
})
if block == "" then
return nil
end
return parseTemplateParams(block)
end
local function parseClassRows(classesText)
local rows = {}
classesText = trim(classesText)
if classesText == "" then
return rows
end
for className, level in mw.ustring.gmatch(classesText, "%[%[([^%]|]+)[^%]]*%]%]%s*%-%s*Level%s*(%d+)") do
className = trim(className)
level = trim(level)
table.insert(rows, {
className = className,
abbr = classAbbr[className] or className,
level = level
})
end
return rows
end
local function classLineAndLevel(classesText, requestedClass)
local rows = parseClassRows(classesText)
if #rows == 0 then
return stripMarkup(classesText), ""
end
requestedClass = trim(requestedClass)
if requestedClass ~= "" then
for _, row in ipairs(rows) do
if row.className == requestedClass or row.abbr == requestedClass then
return row.abbr .. "(" .. row.level .. ")", row.level
end
end
end
local labels = {}
for _, row in ipairs(rows) do
table.insert(labels, row.abbr .. "(" .. row.level .. ")")
end
return table.concat(labels, " "), ""
end
local function parseSlots(slotsText)
slotsText = trim(slotsText)
if slotsText == "" then
return ""
end
local effects = {}
local function isSlotTemplateStart(pos)
local chunk = string.sub(slotsText, pos, pos + 80)
local lower = mw.ustring.lower(chunk)
return mw.ustring.match(lower, "^{{%s*spellslotrow%s*[|}]") or
mw.ustring.match(lower, "^{{%s*spellslotrowsmart%s*[|}]")
end
local function findNextSlotTemplate(startAt)
local searchAt = startAt
while true do
local pos = string.find(slotsText, "{{", searchAt, true)
if not pos then
return nil
end
if isSlotTemplateStart(pos) then
return pos
end
searchAt = pos + 2
end
end
local function readBalancedTemplate(startPos)
local depth = 0
local i = startPos
local len = #slotsText
while i <= len - 1 do
local pair = string.sub(slotsText, i, i + 1)
if pair == "{{" then
depth = depth + 1
i = i + 2
elseif pair == "}}" then
depth = depth - 1
i = i + 2
if depth == 0 then
return string.sub(slotsText, startPos, i - 1), i
end
else
i = i + 1
end
end
return "", len + 1
end
local cursor = 1
while true do
local startPos = findNextSlotTemplate(cursor)
if not startPos then
break
end
local block, nextPos = readBalancedTemplate(startPos)
if block == "" then
break
end
-- Remove outer {{ }}
local inner = string.sub(block, 3, -3)
local parts = splitTopLevelPipes(inner)
-- parts[1] = SpellSlotRow or SpellSlotRowSmart
-- parts[2] = slot number
-- parts[3] = effect text
local slotNum = stripMarkup(parts[2] or "")
local effectText = stripMarkup(parts[3] or "")
if effectText ~= "" then
if slotNum ~= "" then
table.insert(effects, slotNum .. ". " .. effectText)
else
table.insert(effects, effectText)
end
end
cursor = nextPos
end
if #effects > 0 then
return table.concat(effects, "<br>")
end
-- Safety fallback: if the source clearly had spell-slot templates but parsing failed,
-- do not leak raw template/parser-function text into the tooltip.
if mw.ustring.find(slotsText, "SpellSlotRow") or mw.ustring.find(slotsText, "SpellSlotRowSmart") then
return ""
end
return stripMarkup(slotsText)
end
local function fallbackLink(spellName, label)
if spellName == "" then
return " "
end
label = label ~= "" and label or spellName
return "[[" .. spellName .. "|" .. label .. "]]"
end
function p.link(frame)
local parent = frame:getParent()
local parentArgs = parent and parent.args or {}
local invokeArgs = frame.args or {}
local spellName = trim(invokeArgs[1] or parentArgs[1] or invokeArgs.name or parentArgs.name)
local label = trim(invokeArgs[2] or parentArgs[2] or invokeArgs.label or parentArgs.label)
local requestedClass = trim(invokeArgs.class or parentArgs.class or invokeArgs.Class or parentArgs.Class)
if spellName == "" or spellName == " " then
return " "
end
label = label ~= "" and label or spellName
local params = readSpellParams(spellName)
if not params then
return fallbackLink(spellName, label)
end
if requestedClass == "" then
local currentTitle = mw.title.getCurrentTitle()
local currentPage = currentTitle and currentTitle.text or ""
if classAbbr[currentPage] then
requestedClass = currentPage
end
end
local spellTitle = stripMarkup(params.spellname)
if spellTitle == "" then
spellTitle = spellName
end
local icon = trim(params.spellicon)
local description = stripMarkup(params.description)
local classLine, level = classLineAndLevel(params.classes, requestedClass)
local effects = parseSlots(params.slots)
local mana = stripMarkup(params.mana)
local range = stripMarkup(params.range)
local castingTime = formatSeconds(params.casting_time)
local recastTime = formatSeconds(params.recast_time)
local duration = stripMarkup(params.duration)
local skill = stripMarkup(params.skill)
local resist = stripMarkup(params.resist)
local targetType = stripMarkup(params.target_type)
local spellType = stripMarkup(params.spell_type)
local output = {}
table.insert(output, '<div class="hbdiv spell-hbdiv">')
table.insert(output, '[[' .. spellName .. '|' .. label .. ']]')
table.insert(output, '<span class="hb">')
table.insert(output, '<div class="spell-examine-window">')
table.insert(output, '<div class="spell-examine-main">')
if icon ~= "" then
table.insert(output, '<div class="spell-examine-icon">[[File:spellicon_' .. icon .. '.png|64px|link=|border]]</div>')
end
table.insert(output, '<div class="spell-examine-title">' .. html(spellTitle) .. '</div>')
if classLine ~= "" then
table.insert(output, '<div class="spell-examine-classline">' .. html(classLine) .. '</div>')
end
table.insert(output, '<div class="spell-examine-grid">')
table.insert(output, '<div class="spell-examine-left">')
if mana ~= "" then
table.insert(output, '<div>Mana: ' .. html(mana) .. '</div>')
end
if castingTime ~= "" then
table.insert(output, '<div>Cast: ' .. html(castingTime) .. '</div>')
end
if duration ~= "" then
table.insert(output, '<div>Duration: ' .. html(duration) .. '</div>')
end
if level ~= "" then
table.insert(output, '<div>Level: ' .. html(level) .. '</div>')
end
if skill ~= "" then
table.insert(output, '<div>Skill: ' .. html(skill) .. '</div>')
end
if recastTime ~= "" then
table.insert(output, '<div>Reuse: ' .. html(recastTime) .. '</div>')
end
if resist ~= "" then
table.insert(output, '<div>Resist: ' .. html(resist) .. '</div>')
end
if spellType ~= "" then
table.insert(output, '<div>Type: ' .. html(spellType) .. '</div>')
end
table.insert(output, '</div>')
table.insert(output, '<div class="spell-examine-right">')
if targetType ~= "" then
table.insert(output, '<div>Target: ' .. html(targetType) .. '</div>')
end
if range ~= "" then
table.insert(output, '<div>Range: ' .. html(range) .. '</div>')
end
table.insert(output, '</div>')
table.insert(output, '</div>')
if effects ~= "" then
table.insert(output, '<div class="spell-examine-effects">' .. effects .. '</div>')
end
if description ~= "" then
table.insert(output, '<div class="spell-examine-description">' .. html(description) .. '</div>')
end
table.insert(output, '</div>')
table.insert(output, '</div>')
table.insert(output, '</span>')
table.insert(output, '</div>')
return table.concat(output)
end
return p