模組:FUMOCount
外觀
可在模組: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 = {}
-- 逐一找每個 index 標記,然後往後找最近的 {{flag|XXX}} 或 {{Flag|XXX}}
for block in section:gmatch("AutonumTable/index(.-)%{%{[Ff]lag%|") do
-- block 是 index 和 flag 之間的內容,只要確認有對應關係即可
end
-- 改用更直接的方式:找所有「index 緊接著出現的 flag」配對
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
function p.guancha(frame)
local c = getContent()
local section = c:match("%{%{!!%}%} 觀察國 %{%{!!%}%}([%s%S]*)")
if not section then return 0 end
return countMatches(section)
end
function p.total(frame)
local c = getContent()
return countMatches(c)
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
return table.concat(lines, "\n")
end
-- 輸出觀察國列表,供導覽框使用
function p.guanchaList(frame)
local c = getContent()
local section = c:match("%{%{!!%}%} 觀察國 %{%{!!%}%}([%s%S]*)")
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
return table.concat(lines, "\n")
end
return p