Module:UpdateExcerpt: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(21 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
 
     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


Line 28: Line 18:
     end
     end


     local notesStart = content:find("|%s*notes%s*=")
     local notesRaw = content:match("|%s*notes%s*=(.*)")
     if not notesStart then
     if not notesRaw 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


     local sub  = content:sub(notesStart)
     notesRaw = notesRaw:gsub("%s*}}%s*$", "")
    local eqPos = sub:find("=")
    local notesRaw = sub:sub(eqPos + 1)
 
    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 48: Line 30:
     end
     end


     local lines = {}
     local maxHeight = maxLines * 1.7
     for line in notesRaw:gmatch("[^\n]+") do
 
        if mw.text.trim(line) ~= "" then
     local html = string.format(
            table.insert(lines, line)
        '<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>',
         end
        frame:preprocess(notesRaw)
    )
 
    return html
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)


     local count  = math.min(maxLines, #lines)
    -- Build optimized wrapper HTML layout
    local result = {}
     local html = mw.html.create("div")
    for i = 1, count do
         :addClass("nav-bg update-excerpt")
         table.insert(result, lines[i])
    end


     local output = table.concat(result, "\n")
     -- Header Segment
    local hasMore = #lines > maxLines
    html:tag("div")
        :addClass("update-excerpt__header")
        :wikitext(translateText .. ": " .. updateLink .. " (" .. daysSinceText .. ")")


     -- Wrap content in a scrollable div, put "Read more" outside it
     -- Append Excerpt Body Content
     local html = '<div style="flex:1; min-height:0; overflow-y:auto; padding:14px 18px; font-family:\'Retail Demo\',\'Open Sans\',sans-serif; font-size:0.92em; line-height:1.7;">'
     html:wikitext(excerptBody)
        .. frame:preprocess(output)
        .. '</div>'


     if hasMore then
     -- Footer Segment
        html = html .. '<div style="flex-shrink:0; padding:10px 18px; text-align:center; border-top:1px solid rgba(0,0,0,0.2); font-family:\'Retail Demo\',\'Open Sans\',sans-serif;">'
    local footer = html:tag("div")
            .. frame:preprocess('[[' .. pageTitle .. '|<span style="color:#e8be86; font-weight:bold;">Read more →</span>]]')
        :addClass("update-excerpt__footer nav-bg")
            .. '</div>'
       
    end
    footer:tag("span")
        :addClass("update-excerpt__readmore")
        :wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|Read more]]")


     return html
     return tostring(html)
end
end


return p
return p

Latest revision as of 20:53, 25 August 2026

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

local p = {}

local function _render(month, day, year, maxLines, frame)
    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>'
    end

    local pageTitle = "Update:" .. month .. " " .. day .. ", " .. year

    local titleObj = mw.title.new(pageTitle)
    if not titleObj then
        return '<span class="error">Invalid page title: ' .. pageTitle .. '</span>'
    end

    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; 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

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

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
    html:tag("div")
        :addClass("update-excerpt__header")
        :wikitext(translateText .. ": " .. updateLink .. " (" .. daysSinceText .. ")")

    -- Append Excerpt Body Content
    html:wikitext(excerptBody)

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

    return tostring(html)
end

return p