Module:UpdateExcerpt: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
Performance optimizations
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- ==========================================
-- 1. CORE LOGIC (Private internal function)
-- ==========================================
-- This replaces or wraps your original render logic.
-- It accepts raw strings instead of a MediaWiki frame object.
local function _render(month, day, year, height)
    -- -------------------------------------------------------------
    -- [PASTE YOUR EXISTING EXCERPT RENDER LOGIC HERE]
    --
    -- For example, fetching the page "Update:Month Day, Year",
    -- cropping it to the given height, processing tags, etc.
    -- -------------------------------------------------------------
   
    -- (Mock placeholder returning a string; replace with your actual logic)
    return ""
end
-- ==========================================
-- 2. OLD ENTRY POINT (For backwards compatibility)
-- ==========================================
-- Keeps old template calls working. It parses sequential arguments.
function p.render(frame)
function p.render(frame)
     local args = frame.args
     local args = frame.args
     local month   = args[1]
    -- Fallback to parent args if called directly or via wrapper template
     local day     = args[2]
    if not args[1] then args = frame:getParent().args end
     local year     = args[3]
   
     local maxLines = tonumber(args[4]) or 10
     local month = args[1] or ""
     local day   = args[2] or ""
     local year   = args[3] or ""
     local height = args[4] or "15"
   
    return _render(month, day, year, height)
end


    if not month or not day or not year then
        return '<span class="error">Usage: {{#invoke:UpdateExcerpt|render|Month|Day|Year|lines}}</span>'
    end


     local pageTitle = "Update:" .. month .. " " .. day .. ", " .. year
-- ==========================================
-- 3. NEW OPTIMIZED WIDGET ENTRY POINT
-- ==========================================
-- The ultra-fast function that renders the entire HTML frame at once.
function p.drawWidget(frame)
    -- Gather arguments from the template call template {{UpdateExcerpt|month=...}}
     local parentArgs = frame:getParent().args
    local args = frame.args
   
    local month = parentArgs.month or args.month or ""
    local day    = parentArgs.day or args.day or ""
    local year  = parentArgs.year or args.year or ""
    local height = parentArgs.height or args.height or "15"
    local lang  = parentArgs.lang or args.lang or ""


     local titleObj = mw.title.new(pageTitle)
     -- Dynamic Fallbacks: If dates aren't provided, fetch them via preprocess.
     if not titleObj then
    -- This safely queries Module:LatestUpdate only if absolutely necessary.
        return '<span class="error">Invalid page title: ' .. pageTitle .. '</span>'
    if month == "" then month = frame:preprocess("{{#invoke:LatestUpdate|month}}") end
    end
     if day == ""  then day  = frame:preprocess("{{#invoke:LatestUpdate|day}}") end
    if year == "" then year  = frame:preprocess("{{#invoke:LatestUpdate|year}}") end


     local content = titleObj:getContent()
    -- Evaluate the external "days since" text and template layout elements
     if not content then
     local daysSinceText = frame:preprocess("{{#invoke:LatestUpdate|days_since_text|lang=" .. lang .. "}}")
        return '<span class="error">Page not found: [[' .. pageTitle .. ']]</span>'
     local translateText = frame:expandTemplate{ title = "Translate", args = { "Latest Update" } }
     end
     local updateLink    = frame:expandTemplate{ title = "Update link", args = { month, day, year } }


     local notesRaw = content:match("|%s*notes%s*=(.*)")
    -- Run our internal core render function to get the actual excerpt body
    if not notesRaw then
     local excerptBody = _render(month, day, year, height)
        return '<span class="error">No notes parameter found on [[' .. pageTitle .. ']]</span>'
    end


     notesRaw = notesRaw:gsub("%s*}}%s*$", "")
     -- Build the HTML utilizing MediaWiki's highly optimized html library
     notesRaw = mw.text.trim(notesRaw)
     local html = mw.html.create("div")
        :addClass("nav-bg update-excerpt")


     if notesRaw == "" then
     -- Header
         return ""
    html:tag("div")
    end
         :addClass("update-excerpt__header")
        :wikitext(translateText .. ": " .. updateLink .. " (" .. daysSinceText .. ")")


     local maxHeight = maxLines * 1.7
     -- Body (The excerpt itself)
    html:wikitext(excerptBody)


     local html = string.format(
    -- Footer
         '<div style="flex:1; min-height:0; overflow-y:auto; max-height:%.1fem; padding:14px 18px; font-family: sans-serif; font-size:0.92em; line-height:1.7;">\n__NOEDITSECTION__\n%s\n</div>',
     local footer = html:tag("div")
        maxHeight,
         :addClass("update-excerpt__footer nav-bg")
        frame:preprocess(notesRaw)
       
    )
    footer:tag("span")
        :addClass("update-excerpt__readmore")
        :wikitext("Read more")
       
    footer:wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|<span class=\"update-excerpt__footer-overlay\"></span>]]")


     return html
     return tostring(html)
end
end


return p
return p

Revision as of 23:17, 27 June 2026

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

local p = {}

-- ==========================================
-- 1. CORE LOGIC (Private internal function)
-- ==========================================
-- This replaces or wraps your original render logic.
-- It accepts raw strings instead of a MediaWiki frame object.
local function _render(month, day, year, height)
    -- -------------------------------------------------------------
    -- [PASTE YOUR EXISTING EXCERPT RENDER LOGIC HERE]
    -- 
    -- For example, fetching the page "Update:Month Day, Year", 
    -- cropping it to the given height, processing tags, etc.
    -- -------------------------------------------------------------
    
    -- (Mock placeholder returning a string; replace with your actual logic)
    return ""
end


-- ==========================================
-- 2. OLD ENTRY POINT (For backwards compatibility)
-- ==========================================
-- Keeps old template calls working. It parses sequential arguments.
function p.render(frame)
    local args = frame.args
    -- Fallback to parent args if called directly or via wrapper template
    if not args[1] then args = frame:getParent().args end
    
    local month  = args[1] or ""
    local day    = args[2] or ""
    local year   = args[3] or ""
    local height = args[4] or "15"
    
    return _render(month, day, year, height)
end


-- ==========================================
-- 3. NEW OPTIMIZED WIDGET ENTRY POINT
-- ==========================================
-- The ultra-fast function that renders the entire HTML frame at once.
function p.drawWidget(frame)
    -- Gather arguments from the template call template {{UpdateExcerpt|month=...}}
    local parentArgs = frame:getParent().args
    local args = frame.args
    
    local month  = parentArgs.month or args.month or ""
    local day    = parentArgs.day or args.day or ""
    local year   = parentArgs.year or args.year or ""
    local height = parentArgs.height or args.height or "15"
    local lang   = parentArgs.lang or args.lang or ""

    -- Dynamic Fallbacks: If dates aren't provided, fetch them via preprocess.
    -- This safely queries Module:LatestUpdate only if absolutely necessary.
    if month == "" then month = frame:preprocess("{{#invoke:LatestUpdate|month}}") end
    if day == ""   then day   = frame:preprocess("{{#invoke:LatestUpdate|day}}") end
    if year == ""  then year  = frame:preprocess("{{#invoke:LatestUpdate|year}}") end

    -- Evaluate the external "days since" text and template layout elements
    local daysSinceText = frame:preprocess("{{#invoke:LatestUpdate|days_since_text|lang=" .. lang .. "}}")
    local translateText = frame:expandTemplate{ title = "Translate", args = { "Latest Update" } }
    local updateLink    = frame:expandTemplate{ title = "Update link", args = { month, day, year } }

    -- Run our internal core render function to get the actual excerpt body
    local excerptBody = _render(month, day, year, height)

    -- Build the HTML utilizing MediaWiki's highly optimized html library
    local html = mw.html.create("div")
        :addClass("nav-bg update-excerpt")

    -- Header
    html:tag("div")
        :addClass("update-excerpt__header")
        :wikitext(translateText .. ": " .. updateLink .. " (" .. daysSinceText .. ")")

    -- Body (The excerpt itself)
    html:wikitext(excerptBody)

    -- Footer
    local footer = html:tag("div")
        :addClass("update-excerpt__footer nav-bg")
        
    footer:tag("span")
        :addClass("update-excerpt__readmore")
        :wikitext("Read more")
        
    footer:wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|<span class=\"update-excerpt__footer-overlay\"></span>]]")

    return tostring(html)
end

return p