Module:Sandbox/LVL: Difference between revisions

LVL (talk | contribs)
mNo edit summary
LVL (talk | contribs)
No edit summary
Line 1: Line 1:
-- This module powers the Plink template, automatically finding the correct file extension for an icon.
local p = {}
local p = {}


-- Load language codes
function p.main(frame)
local lang_codes = mw.loadJsonData("Data:LangCodes.json")
    -- Get the arguments passed from the template.
    local args = frame:getParent().args
   
    local link_target = args[1] or ''
    local display_text = args.altText or args[2] or link_target
    local base_filename = args.pic or link_target
    local size = args.size or args[3] or '24'


-- cache page contents
    -- A list of common file extensions to check, in order of preference.
local cache = {}
    local extensions = { '.png', '.svg', '.jpg', '.jpeg', '.gif' }
 
   
-- Find the end by counting braces (unchanged)
    local final_filename = base_filename .. '.png' -- Default to .png to create a redlink if no file is found.
local function findBlockEnd(content, start_pos)
   
     local open_braces = 0
     -- Loop through the extensions and check if a file with that name exists.
     for i = start_pos, #content do
     for _, ext in ipairs(extensions) do
         local char = content:sub(i, i)
         local title = mw.title.new('File:' .. base_filename .. ext)
         if char == "{" then
         if title and title.exists then
             open_braces = open_braces + 1
             final_filename = base_filename .. ext
        elseif char == "}" then
             break -- Stop searching once we find a match.
             open_braces = open_braces - 1
            if open_braces == 0 then
                return i
            end
        end
         end
         end
    return nil
end
-- Detect language and base page name (unchanged)
local function getLangAndBase()
    local fullTitle = mw.title.getCurrentTitle().fullText
    local parts = mw.text.split(fullTitle, "/")
    local last = parts[#parts]
    if lang_codes[last] then
        table.remove(parts)
        return last, table.concat(parts, "/")
    else
        return "en", fullTitle
     end
     end
end
   
 
    -- Build the final wikitext output, same as the original template.
-- Helper to get page content (cached)
     local file_link = string.format('[[File:%s|link=%s|%spx]]', final_filename, link_target, size)
local function getPageContent(title)
     local text_link = string.format('[[%s|%s]]', link_target, display_text)
    if cache[title] then return cache[title] end
     local span_tag = string.format('<span class="pic-link" style="filter: brightness(100) saturate(100)%%;">%s</span>', file_link) -- Use %% to escape the % character
     local page = mw.title.new(title)
      
    local raw  = page and page:getContent() or ''
     return span_tag .. ' ' .. text_link
    cache[title] = raw
    return raw
end
 
-- Main function to extract recent updates
function p.getRecentUpdates(frame)
     local args = frame.args
    local forceDefault = args.default == "true"
    local limit = math.min(tonumber(args.limit) or 3, 50)
    local lang, basePage = getLangAndBase()
    if forceDefault then lang = "en" end
 
    -- Try localized version first (cached)
     local localizedTitle = basePage .. "/Update history" .. (lang ~= "en" and ("/" .. lang) or "")
    local content = getPageContent(localizedTitle)
 
    -- Fallback to english (cached)
    if not content or content == "" then
        content = getPageContent(basePage .. "/Update history") or ""
    end
 
    local blocks = {}
    local pattern = "{{Update history table/row"
    local start = content:find(pattern)
    local count, limit = 0, limit
 
    while start and count < limit do
        local finish = findBlockEnd(content, start)
        if not finish then break end
        table.insert(blocks, frame:preprocess(content:sub(start, finish)))
        count = count + 1
        start = content:find(pattern, finish + 1)
    end
 
    if #blocks == 0 then
        return '<span class="update-history-empty">No recent updates.</span>'
     end
 
     return table.concat(blocks, "\n")
end
end


return p
return p