Module:UpdateExcerpt: Difference between revisions

Undo revision 62037 by Monster Domosed (talk)
Tag: Undo
No edit summary
 
(23 intermediate revisions by 4 users not shown)
Line 1: Line 1:
-- Module:UpdateExcerpt
-- Usage: {{#invoke:UpdateExcerpt|render|Month|Day|Year|number_of_lines}}
-- Example: {{#invoke:UpdateExcerpt|render|March|6|2026|5}}
local p = {}
local p = {}


function p.render(frame)
local function _render(month, day, year, maxLines, frame)
    local args = frame.args
     if not month or month == "" or not day or day == "" or not year or year == "" then
    local month   = args[1]
         return '<span class="error">Usage Error: Missing date components (Month, Day, or Year).</span>'
    local day     = args[2]
    local year     = args[3]
    local maxLines = tonumber(args[4]) or 5
 
    -- Validate arguments
     if not month or not day or not year then
         return '<span class="error">Usage: {{#invoke:UpdateExcerpt|render|Month|Day|Year|lines}}</span>'
     end
     end


    -- Build the page title  (e.g. "Update:March 6, 2026")
     local pageTitle = "Update:" .. month .. " " .. day .. ", " .. year
     local pageTitle = "Update:" .. month .. " " .. day .. ", " .. year


Line 30: Line 18:
     end
     end


    -----------------------------------------------------------------
     local notesRaw = content:match("|%s*notes%s*=(.*)")
    -- Locate the  | notes =  parameter inside {{Update layout …}}
     if not notesRaw then
    -----------------------------------------------------------------
     local notesStart = content:find("|%s*notes%s*=")
     if not notesStart then
         return '<span class="error">No notes parameter found on [[' .. pageTitle .. ']]</span>'
         return '<span class="error">No notes parameter found on [[' .. pageTitle .. ']]</span>'
     end
     end


     -- Grab everything after the '=' sign
     notesRaw = notesRaw:gsub("%s*}}%s*$", "")
    local sub  = content:sub(notesStart)
    local eqPos = sub:find("=")
    local notesRaw = sub:sub(eqPos + 1)
 
    -- Strip the final closing  }}  that belongs to {{Update layout}}
    -- Using the *last* occurrence so nested templates inside notes
    -- (if any) are preserved.
    local lastClose = notesRaw:find("}}%s*$")
    if lastClose then
        notesRaw = notesRaw:sub(1, lastClose - 1)
    end
 
     notesRaw = mw.text.trim(notesRaw)
     notesRaw = mw.text.trim(notesRaw)


Line 57: Line 30:
     end
     end


     -----------------------------------------------------------------
     local maxHeight = maxLines * 1.7
     -- Split into non-empty lines
 
     -----------------------------------------------------------------
    local html = string.format(
     local lines = {}
        '<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>',
     for line in notesRaw:gmatch("[^\n]+") do
        frame:preprocess(notesRaw)
        -- keep the line only if it isn't pure whitespace
    )
        if mw.text.trim(line) ~= "" then
 
            table.insert(lines, line)
     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
     end


     if #lines == 0 then
    return "[[Update:" .. month .. " " .. day .. ", " .. year .. "| ".. month .. " " .. day .. ", " .. year .. "]]"
        return ""
end
     end
 
function p.render(frame)
    local args = frame.args
     if not args[1] then args = frame:getParent().args end
   
    local month    = args[1]
    local day      = args[2]
    local year    = args[3]
    local maxLines = tonumber(args[4]) or 10
 
    return _render(month, day, year, maxLines, frame)
end
 
function p.drawWidget(frame)
    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 = tonumber(parentArgs.height or args.height) or 15
    local lang  = parentArgs.lang or args.lang or ""
 
    month = mw.text.trim(month)
    day  = mw.text.trim(day)
    year  = mw.text.trim(year)
 
    -- 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
 
    -- 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 updateLink    = frame:expandTemplate{ title = "Update link", args = { month, day, year } }
 
    -- Fetch the inner excerpt HTML
    local excerptBody = _render(month, day, year, height, frame)
 
    -- Build optimized wrapper HTML layout
    local html = mw.html.create("div")
        :addClass("nav-bg update-excerpt")


     -----------------------------------------------------------------
     -- Header Segment
     -- Build output: first N lines, then "" linking to the full page
     html:tag("div")
    -----------------------------------------------------------------
        :addClass("update-excerpt__header")
    local count  = math.min(maxLines, #lines)
         :wikitext(translateText .. ": " .. updateLink .. " (" .. daysSinceText .. ")")
    local result = {}
    for i = 1, count do
         table.insert(result, lines[i])
    end


     local output = table.concat(result, "\n")
     -- Append Excerpt Body Content
    html:wikitext(excerptBody)


     if #lines > maxLines then
     -- Footer Segment
         output = output .. "\n[[" .. pageTitle .. "|Read more]]"
    local footer = html:tag("div")
    end
        :addClass("update-excerpt__footer nav-bg")
       
    footer:tag("span")
        :addClass("update-excerpt__readmore")
         :wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|Read more]]")


     return frame:preprocess(output)
     return tostring(html)
end
end


return p
return p