Module:ItemEffect: Difference between revisions

From He is Coming Official Wiki
Benjiminio (talk | contribs)
Created page with "local p = {} function p.main(frame) local item = frame.args[1] or "" if item == "" then return "No item specified" end -- Normalize item name (replace spaces with underscores) local safeItem = item:gsub(" ", "_") -- Build effect page title local effectPage = mw.title.new("He_is_Coming/" .. safeItem .. "/effect") if not effectPage or not effectPage.exists then return "Effect page not found" end -- Get the raw wik..."
 
Benjiminio (talk | contribs)
mNo edit summary
Line 1: Line 1:
-- Module:ItemEffect
local p = {}
local p = {}


-- List of keywords & colors comes from ColorText
local colorModule = require("Module:ColorText")  -- Adjusted for subspace if needed
-- Main function: fetch effect content and color keywords
function p.main(frame)
function p.main(frame)
     local item = frame.args[1] or ""
     local itemName = frame.args[1] or ""
     if item == "" then
     if itemName == "" then return "" end
        return "No item specified"
    end
 
    -- Normalize item name (replace spaces with underscores)
    local safeItem = item:gsub(" ", "_")


     -- Build effect page title
     -- Construct the effect page title under He_is_Coming
     local effectPage = mw.title.new("He_is_Coming/" .. safeItem .. "/effect")
     local effectTitle = mw.title.new(itemName .. "/effect", "He_is_Coming")
     if not effectPage or not effectPage.exists then
     if not effectTitle or not effectTitle.exists then
         return "Effect page not found"
         return "Effect page not found: " .. itemName .. "/effect"
     end
     end


    -- Get the raw wikitext of the effect page
     local effectContent = effectTitle:getContent() or ""
     local effectText = effectPage:getContent() or ""
    if effectContent == "" then return "(No effect text)" end


     -- Get ColorText module
     -- Color each keyword using ColorText
     local ColorText = mw.getContentModule("ColorText")
     local function colorKeyword(word)
    if not ColorText or not ColorText.colors then
         return colorModule.main({ args = { word } })
         return effectText  -- fallback: no coloring if ColorText missing
     end
     end


     -- Replace all keywords dynamically
     -- Simple pattern to find keywords (adjust if needed)
    for keyword, _ in pairs(ColorText.colors) do
    -- Only match words that exist in ColorText's color table
        local colored = ColorText.main{args={keyword}}
    effectContent = effectContent:gsub("%w+", function(w)
        -- Replace whole words only, case-insensitive
        local lower = w:lower()
        effectText = effectText:gsub("(%f[%w])"..keyword.."(%f[%W])", colored)
        if colorModule.colors and colorModule.colors[lower] then
     end
            return colorKeyword(w)
        else
            return w
        end
     end)


     return effectText
     return effectContent
end
end


return p
return p

Revision as of 11:13, 14 November 2025

Documentation for this module may be created at Module:ItemEffect/doc

-- Module:ItemEffect
local p = {}

-- List of keywords & colors comes from ColorText
local colorModule = require("Module:ColorText")  -- Adjusted for subspace if needed

-- Main function: fetch effect content and color keywords
function p.main(frame)
    local itemName = frame.args[1] or ""
    if itemName == "" then return "" end

    -- Construct the effect page title under He_is_Coming
    local effectTitle = mw.title.new(itemName .. "/effect", "He_is_Coming")
    if not effectTitle or not effectTitle.exists then
        return "Effect page not found: " .. itemName .. "/effect"
    end

    local effectContent = effectTitle:getContent() or ""
    if effectContent == "" then return "(No effect text)" end

    -- Color each keyword using ColorText
    local function colorKeyword(word)
        return colorModule.main({ args = { word } })
    end

    -- Simple pattern to find keywords (adjust if needed)
    -- Only match words that exist in ColorText's color table
    effectContent = effectContent:gsub("%w+", function(w)
        local lower = w:lower()
        if colorModule.colors and colorModule.colors[lower] then
            return colorKeyword(w)
        else
            return w
        end
    end)

    return effectContent
end

return p