Module:UpdateExcerpt: Difference between revisions

Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- ==========================================
-- =====================================================================
-- 1. CORE LOGIC (Private internal function)
-- 1. CORE LOGIC (Private internal function)
-- ==========================================
-- =====================================================================
-- This replaces or wraps your original render logic.
local function _render(month, day, year, maxLines, frame)
-- It accepts raw strings instead of a MediaWiki frame object.
    if not month or month == "" or not day or day == "" or not year or year == "" then
local function _render(month, day, year, height)
        return '<span class="error">Usage Error: Missing date components (Month, Day, or Year).</span>'
     -- -------------------------------------------------------------
    end
     -- [PASTE YOUR EXISTING EXCERPT RENDER LOGIC HERE]
 
     --
    local pageTitle = "Update:" .. month .. " " .. day .. ", " .. year
     -- For example, fetching the page "Update:Month Day, Year",
 
     -- cropping it to the given height, processing tags, etc.
    local titleObj = mw.title.new(pageTitle)
     -- -------------------------------------------------------------
     if not titleObj then
   
        return '<span class="error">Invalid page title: ' .. pageTitle .. '</span>'
     -- (Mock placeholder returning a string; replace with your actual logic)
    end
     return ""
 
    local content = titleObj:getContent()
     if not content then
        return '<span class="error">Page not found: [[' .. pageTitle .. ']]</span>'
     end
 
    local notesRaw = content:match("|%s*notes%s*=(.*)")
     if not notesRaw then
        return '<span class="error">No notes parameter found on [[' .. pageTitle .. ']]</span>'
    end
 
    notesRaw = notesRaw:gsub("%s*}}%s*$", "")
    notesRaw = mw.text.trim(notesRaw)
 
    if notesRaw == "" then
        return ""
    end
 
     local maxHeight = maxLines * 1.7
 
     local html = string.format(
        '<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>',
        maxHeight,
        frame:preprocess(notesRaw)
     )
 
     return html
end
end


 
-- =====================================================================
-- ==========================================
-- 2. OLD ENTRY POINT (For backwards compatibility)
-- 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
    -- Fallback to parent args if called directly or via wrapper template
     if not args[1] then args = frame:getParent().args end
     if not args[1] then args = frame:getParent().args end
      
      
     local month = args[1] or ""
     local month   = args[1]
     local day   = args[2] or ""
     local day     = args[2]
     local year   = args[3] or ""
     local year     = args[3]
     local height = args[4] or "15"
     local maxLines = tonumber(args[4]) or 10
   
 
     return _render(month, day, year, height)
     return _render(month, day, year, maxLines, frame)
end
end


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


     -- Dynamic Fallbacks: If dates aren't provided, fetch them via preprocess.
    month = mw.text.trim(month)
     -- This safely queries Module:LatestUpdate only if absolutely necessary.
    day  = mw.text.trim(day)
     if month == "" then month = frame:preprocess("{{#invoke:LatestUpdate|month}}") end
    year  = mw.text.trim(year)
     if day == ""  then day  = frame:preprocess("{{#invoke:LatestUpdate|day}}") end
 
     if year == ""  then year  = frame:preprocess("{{#invoke:LatestUpdate|year}}") end
     -- Native Lua loading: Bypasses frame:preprocess entirely
     local latestUpdate = require("Module:LatestUpdate")
 
     if month == "" then month = latestUpdate.month() end
     if day == ""  then day  = latestUpdate.day() end
     if year == ""  then year  = latestUpdate.year() end


     -- Evaluate the external "days since" text and template layout elements
     -- Execute days_since_text natively by passing a mock frame table
     local daysSinceText = frame:preprocess("{{#invoke:LatestUpdate|days_since_text|lang=" .. lang .. "}}")
     local daysSinceText = latestUpdate.days_since_text({ args = { lang = lang } })
   
    -- Standard template expansions for structural layout
     local translateText = frame:expandTemplate{ title = "Translate", args = { "Latest Update" } }
     local translateText = frame:expandTemplate{ title = "Translate", args = { "Latest Update" } }
     local updateLink    = frame:expandTemplate{ title = "Update link", args = { month, day, year } }
     local updateLink    = frame:expandTemplate{ title = "Update link", args = { month, day, year } }


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


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


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


     -- Body (The excerpt itself)
     -- Append Excerpt Body Content
     html:wikitext(excerptBody)
     html:wikitext(excerptBody)


     -- Footer
     -- Footer Segment
     local footer = html:tag("div")
     local footer = html:tag("div")
         :addClass("update-excerpt__footer nav-bg")
         :addClass("update-excerpt__footer nav-bg")