模組:沙盒/a2569875/PJTool

local p={}

function p.listProjects(frame, _code, _comma, _max_num)
	local project_list = p._getProjects(frame)
	if #project_list <= 0 then return '' end
	local code = _code or '$1$2'
	local comma = _comma or '、'
	local max_num = tonumber("inf")
	local tail = "專題"
	if type(frame.args) == type({"table"}) then
		code = frame.args.code or code
		comma = frame.args.comma or comma
		tail = frame.args.tail or tail
		max_num = tonumber(frame.args.max_num or frame.args["max num"]) or max_num
	elseif type(frame) == type({"table"}) then
		code = frame.code or frame[2] or code
		comma = frame.comma or frame[3] or comma
		max_num = tonumber(frame.max_num or frame["max num"] or frame[4]) or max_num
		tail = frame.tail or tail
	end
	local result = ''
	for i=1,#project_list do
		if i > max_num then break end
		result = result .. mw.ustring.gsub(code, "%$([%+%-]?%d+)", function(_id)
			local id = tonumber(_id)
			if id == 1 then
				return project_list[i]
			elseif id == 2 then
				return (i + 1 == max_num or i + 1 == #project_list) and "和" or ((i==#project_list or i == max_num) and '' or comma)
			end
			return "$".._id
		end)
	end
	if max_num > 1 and #project_list > 1 then
		result = result .. "等"
	end
	result = result .. tail
	return result
end
function p._getProjects(frame)
	local text = frame
	if type(frame.args) == type({"table"}) then
		text = frame.args[1] or ''
	elseif type(frame) == type({"table"}) then
		text = frame[1] or ''
	end
	local project_list = {}
	local _,j,i,length = 1,1,1,mw.ustring.len(text)
	while i <= length do
		_,j = mw.ustring.find(text, "class%s*=%s*\"wpb%-project\"", i)
		if j then
			local k = mw.ustring.find(text, "<%s*/%s*span%s*>", j)
			local pj_text = mw.ustring.sub(text, j+1, k or -1)
			project_list[#project_list + 1] = mw.ustring.match(pj_text, "^>%s*(.*)%s*<$")
			i = k or length
		else break end
	end
	return project_list
end

function p.main()
local talkpage = mw.title.getCurrentTitle().talkPageTitle
local text=talkpage:getContent() or ""
local norm_name = "WPBS"
local match_list = {"wiki%s*project%s*banner%s*shell", "w?pj?%s*banner%s*shell", "wiki%s*project%s*banners", "multiple%s*wikiprojects?", "[維维]?基?[专專][题題][橫横]?幅?", "多?[個个]?[专專][题題][橫横]?幅?"}
local lotext = mw.ustring.lower (text)
local normtext = text
for i = 1,#match_list do
 local j, k = mw.ustring.find(lotext, match_list[i])
 while j ~= nil do 
  normtext = mw.ustring.sub(normtext, 1, j-1)..norm_name..mw.ustring.sub(normtext, k+1, -1) 
  lotext = mw.ustring.lower(normtext)
  j, k = mw.ustring.find(lotext, match_list[i])
 end
end
normtext = mw.ustring.gsub(normtext, norm_name, "WikiProject banner shell")
text=normtext

local i,j = mw.ustring.find(text, "%=+[^\n]*%=+%s*\n")
if i == nil then
 i,j = mw.ustring.find(text, "%{%{%s*[Dd][Yy][Kk][Ee]ntry/archive")
end
text= mw.ustring.sub(text, 1, (i or 0)-1)
mw.log("get-text=", text)
local re_WPBS_header = "%{%{%s*[Ww]iki[Pp]roject [Bb]anner [Ss]hell"
local it = text
local old_it = text.."-"
while it ~= old_it do
  if not mw.ustring.match(it, re_WPBS_header) then break end
  old_it = it
  it = mw.ustring.gsub(it, "%{[^%{%}]*%}", "")
end
local WPBS = mw.ustring.match(old_it, re_WPBS_header.."[^%{%}]*%}%}")
mw.log("WPBS=", WPBS)
local select_class =  mw.ustring.match(WPBS, "%|%s*[Cc][Ll][Aa][Ss][Ss]%s*%=%s*([^%|%}]+)")
return select_class

end
return p