跳至內容

「模組:FUMOCount」:修訂間差異

出自微國家百科
Naritaroad留言 | 貢獻
建立內容為「local p = {} local TARGET_PAGE = "聯合微國家組織會員國列表" local function getContent() return mw.title.new(TARGET_PAGE):getContent() or "" end local function countIndex(content, startPattern, endPattern) local s = endPattern and content:match(startPattern .. "(.-)" .. endPattern) or content:match(startPattern .. "(.*)") if not s then return 0 end local n = 0 for _ in s:gmatch("AutonumTable/index") do n = n…」的新頁面
 
Naritaroad留言 | 貢獻
無編輯摘要
 
(未顯示同一使用者於中間所作的 6 次修訂)
第7行: 第7行:
end
end


local function countIndex(content, startPattern, endPattern)
local function countMatches(s)
    local s = endPattern
        and content:match(startPattern .. "(.-)" .. endPattern)
        or  content:match(startPattern .. "(.*)")
    if not s then return 0 end
     local n = 0
     local n = 0
     for _ in s:gmatch("AutonumTable/index") do n = n + 1 end
     for _ in s:gmatch("AutonumTable/index") do n = n + 1 end
第17行: 第13行:
end
end


-- 成員國數(fumochengyuanguo 區段)
-- 從區段內依序擷取每個 AutonumTable/index 後的第一個 flag 名稱
local function extractFlags(section)
    local flags = {}
    local pos = 1
    while true do
        local iStart, iEnd = section:find("AutonumTable/index", pos)
        if not iStart then break end
        -- 從 index 之後找最近的 {{flag|...}} 或 {{Flag|...}}
        local flagName = section:match("%{%{[Ff]lag%|([^}]+)%}%}", iEnd)
        if flagName then
            flagName = flagName:match("^%s*(.-)%s*$")
            table.insert(flags, flagName)
        end
        pos = iEnd + 1
    end
    return flags
end
 
-- 成員國數
function p.chengyuan(frame)
function p.chengyuan(frame)
     local c = getContent()
     local c = getContent()
     return countIndex(c, "id=fumochengyuanguo", "id=fumoguanchaguo")
     local section = c:match("%{%{!!%}%} 成員國 %{%{!!%}%}(.-)%{%{!!%}%} 觀察國 %{%{!!%}%}")
    if not section then return 0 end
    return countMatches(section)
end
end


-- 觀察國數(fumoguanchaguo 區段)
-- 觀察國數(限定至 id=fumoguanchaguo,避免計入前會員國)
function p.guancha(frame)
function p.guancha(frame)
     local c = getContent()
     local c = getContent()
     return countIndex(c, "id=fumoguanchaguo", nil)
     local section = c:match("%{%{!!%}%} 觀察國 %{%{!!%}%}(.-)id=fumoguanchaguo")
    if not section then return 0 end
    return countMatches(section)
end
end


-- 會員國總數
-- 會員國總數(限定至 id=fumoguanchaguo,避免計入前會員國)
function p.total(frame)
function p.total(frame)
     local c = getContent()
     local c = getContent()
     local n = 0
     local section = c:match("(.-)id=fumoguanchaguo")
     for _ in c:gmatch("AutonumTable/index") do n = n + 1 end
    if not section then return 0 end
     return n
    return countMatches(section)
end
 
-- 輸出成員國列表,供導覽框使用
function p.chengyuanList(frame)
    local c = getContent()
    local section = c:match("%{%{!!%}%} 成員國 %{%{!!%}%}(.-)%{%{!!%}%} 觀察國 %{%{!!%}%}")
    if not section then return "" end
    local flags = extractFlags(section)
    local lines = {}
     for _, name in ipairs(flags) do
        table.insert(lines, "* <span style=\"white-space:nowrap;\">{{flag|" .. name .. "}}</span>")
    end
    local wikitext = table.concat(lines, "\n")
    return frame:preprocess(wikitext)
end
 
-- 輸出觀察國列表,供導覽框使用(限定至 id=fumoguanchaguo,避免輸出前會員國)
function p.guanchaList(frame)
    local c = getContent()
    local section = c:match("%{%{!!%}%} 觀察國 %{%{!!%}%}(.-)id=fumoguanchaguo")
    if not section then return "" end
    local flags = extractFlags(section)
    local lines = {}
    for _, name in ipairs(flags) do
        table.insert(lines, "* <span style=\"white-space:nowrap;\">{{flag|" .. name .. "}}</span>")
    end
    local wikitext = table.concat(lines, "\n")
     return frame:preprocess(wikitext)
end
end


return p
return p

於 2026年6月11日 (四) 02:26 的最新修訂

可在模組:FUMOCount/doc建立此模組的說明文件

local p = {}

local TARGET_PAGE = "聯合微國家組織會員國列表"

local function getContent()
    return mw.title.new(TARGET_PAGE):getContent() or ""
end

local function countMatches(s)
    local n = 0
    for _ in s:gmatch("AutonumTable/index") do n = n + 1 end
    return n
end

-- 從區段內依序擷取每個 AutonumTable/index 後的第一個 flag 名稱
local function extractFlags(section)
    local flags = {}
    local pos = 1
    while true do
        local iStart, iEnd = section:find("AutonumTable/index", pos)
        if not iStart then break end
        -- 從 index 之後找最近的 {{flag|...}} 或 {{Flag|...}}
        local flagName = section:match("%{%{[Ff]lag%|([^}]+)%}%}", iEnd)
        if flagName then
            flagName = flagName:match("^%s*(.-)%s*$")
            table.insert(flags, flagName)
        end
        pos = iEnd + 1
    end
    return flags
end

-- 成員國數
function p.chengyuan(frame)
    local c = getContent()
    local section = c:match("%{%{!!%}%} 成員國 %{%{!!%}%}(.-)%{%{!!%}%} 觀察國 %{%{!!%}%}")
    if not section then return 0 end
    return countMatches(section)
end

-- 觀察國數(限定至 id=fumoguanchaguo,避免計入前會員國)
function p.guancha(frame)
    local c = getContent()
    local section = c:match("%{%{!!%}%} 觀察國 %{%{!!%}%}(.-)id=fumoguanchaguo")
    if not section then return 0 end
    return countMatches(section)
end

-- 會員國總數(限定至 id=fumoguanchaguo,避免計入前會員國)
function p.total(frame)
    local c = getContent()
    local section = c:match("(.-)id=fumoguanchaguo")
    if not section then return 0 end
    return countMatches(section)
end

-- 輸出成員國列表,供導覽框使用
function p.chengyuanList(frame)
    local c = getContent()
    local section = c:match("%{%{!!%}%} 成員國 %{%{!!%}%}(.-)%{%{!!%}%} 觀察國 %{%{!!%}%}")
    if not section then return "" end
    local flags = extractFlags(section)
    local lines = {}
    for _, name in ipairs(flags) do
        table.insert(lines, "* <span style=\"white-space:nowrap;\">{{flag|" .. name .. "}}</span>")
    end
    local wikitext = table.concat(lines, "\n")
    return frame:preprocess(wikitext)
end

-- 輸出觀察國列表,供導覽框使用(限定至 id=fumoguanchaguo,避免輸出前會員國)
function p.guanchaList(frame)
    local c = getContent()
    local section = c:match("%{%{!!%}%} 觀察國 %{%{!!%}%}(.-)id=fumoguanchaguo")
    if not section then return "" end
    local flags = extractFlags(section)
    local lines = {}
    for _, name in ipairs(flags) do
        table.insert(lines, "* <span style=\"white-space:nowrap;\">{{flag|" .. name .. "}}</span>")
    end
    local wikitext = table.concat(lines, "\n")
    return frame:preprocess(wikitext)
end

return p