Jump to content

Module:NamespaceTable: Difference between revisions

From Costa Sano KB
No edit summary
No edit summary
 
Line 1: Line 1:
local p = {}
local p = {}
-- Extract Shortdesc from page content
local function getShortdesc(title)
    local t = mw.title.new(title)
    if not t or not t.exists then
        return ""
    end
    local content = t:getContent()
    if not content then
        return ""
    end
    -- Match {{Shortdesc|...}}
    local desc = content:match("{{%s*[Ss]hortdesc%s*|([^}]+)}}")
    if desc then
        return mw.text.trim(desc)
    end
    return ""
end


function p.table(frame)
function p.table(frame)
Line 7: Line 29:
     local out = '{| class="wikitable"\n! Page !! Description\n'
     local out = '{| class="wikitable"\n! Page !! Description\n'


     for _, line in ipairs(list) do
     for _, title in ipairs(list) do
         line = mw.text.trim(line)
         title = mw.text.trim(title)


         if line ~= "" then
         if title ~= "" then
            -- Split "Page|Description"
            local parts = mw.text.split(line, "|")
            local title = mw.text.trim(parts[1] or "")
            local desc = mw.text.trim(parts[2] or "")
 
            -- Determine subpage depth
             local depth = select(2, title:gsub("/", ""))
             local depth = select(2, title:gsub("/", ""))
             local indent = string.rep("— ", depth)
             local indent = string.rep("— ", depth)


             -- Output row
             local desc = getShortdesc(title)
 
             out = out .. '|-\n| ' .. indent .. '[[' .. title .. ']] || ' .. desc .. '\n'
             out = out .. '|-\n| ' .. indent .. '[[' .. title .. ']] || ' .. desc .. '\n'
         end
         end

Latest revision as of 22:16, 10 April 2026

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

local p = {}

-- Extract Shortdesc from page content
local function getShortdesc(title)
    local t = mw.title.new(title)
    if not t or not t.exists then
        return ""
    end

    local content = t:getContent()
    if not content then
        return ""
    end

    -- Match {{Shortdesc|...}}
    local desc = content:match("{{%s*[Ss]hortdesc%s*|([^}]+)}}")

    if desc then
        return mw.text.trim(desc)
    end

    return ""
end

function p.table(frame)
    local pages = frame.args.pages or ""
    local list = mw.text.split(pages, "\n")

    local out = '{| class="wikitable"\n! Page !! Description\n'

    for _, title in ipairs(list) do
        title = mw.text.trim(title)

        if title ~= "" then
            local depth = select(2, title:gsub("/", ""))
            local indent = string.rep("— ", depth)

            local desc = getShortdesc(title)

            out = out .. '|-\n| ' .. indent .. '[[' .. title .. ']] || ' .. desc .. '\n'
        end
    end

    out = out .. '|}'
    return out
end

return p