Module:UpdateExcerpt: Difference between revisions

No edit summary
No edit summary
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- ==========================================
local function _render(month, day, year, maxLines, frame)
-- 1. CORE LOGIC (Private internal function)
    if not month or month == "" or not day or day == "" or not year or year == "" then
-- ==========================================
        return '<span class="error">Usage Error: Missing date components (Month, Day, or Year).</span>'
-- This replaces or wraps your original render logic.
    end
-- It accepts raw strings instead of a MediaWiki frame object.
 
local function _render(month, day, year, height)
    local pageTitle = "Update:" .. month .. " " .. day .. ", " .. year
     -- -------------------------------------------------------------
 
    -- [PASTE YOUR EXISTING EXCERPT RENDER LOGIC HERE]
    local titleObj = mw.title.new(pageTitle)
     --
    if not titleObj then
     -- For example, fetching the page "Update:Month Day, Year",
        return '<span class="error">Invalid page title: ' .. pageTitle .. '</span>'
     -- cropping it to the given height, processing tags, etc.
    end
     -- -------------------------------------------------------------
 
   
    local content = titleObj:getContent()
     -- (Mock placeholder returning a string; replace with your actual logic)
    if not content then
     return ""
        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; padding:14px 18px; font-family: sans-serif; font-size:0.92em; line-height:1.7;">\n__NOEDITSECTION__\n%s\n</div>',
        frame:preprocess(notesRaw)
     )
 
     return html
end
end


function p.getDateLink(frame)
    local parentArgs = frame:getParent().args or {}
    local args = frame.args or {}
    local month = parentArgs.month or args.month or parentArgs[1] or args[1] or ""
    local day  = parentArgs.day or args.day or parentArgs[2] or args[2] or ""
    local year  = parentArgs.year or args.year or parentArgs[3] or args[3] or ""
    month = mw.text.trim(month)
    day  = mw.text.trim(day)
    year  = mw.text.trim(year)
    if month == "" or day == "" or year == "" then
        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
    end
    return "[[Update:" .. month .. " " .. day .. ", " .. year .. "| ".. month .. " " .. day .. ", " .. year .. "]]"
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
    -- 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 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 81:
     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


     -- Evaluate the external "days since" text and template layout elements
     -- Native Lua loading: Bypasses frame:preprocess entirely
     local daysSinceText = frame:preprocess("{{#invoke:LatestUpdate|days_since_text|lang=" .. lang .. "}}")
     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
 
    -- Execute days_since_text natively by passing a mock frame table
    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")
Line 84: Line 123:
     footer:tag("span")
     footer:tag("span")
         :addClass("update-excerpt__readmore")
         :addClass("update-excerpt__readmore")
         :wikitext("Read more")
         :wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|Read more]]")
       
    footer:wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|<span class=\"update-excerpt__footer-overlay\"></span>]]")


     return tostring(html)
     return tostring(html)