FANDOM


local p = {}
 
local function escapePattern(str)
    return mw.ustring.gsub(str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
end
 
local start = '<!--Start-->'
local stop = '<!--End-->'
local listpatt = escapePattern(start) .. '(.-)' .. escapePattern(stop)
local linkpatt = "%[%[[^%]\n]+%]%]"
 
local function pick_article(mode)
	local list = {}
	local fa_list = " "
	local ga_list = " "
	local pagename, pagetext, filename = " "
 
	if (mode == "fa" or mode == "both") then
		fa_list = mw.title.new('The Puppet Saga Wiki:Featured articles'):getContent()
		fa_list = fa_list:match(listpatt)
	end
	if (mode == "ga" or mode == "both") then
		ga_list = mw.title.new('The Puppet Saga Wiki:Good articles'):getContent()
		ga_list = ga_list:match(listpatt)
	end
 
	local raw_list = mw.ustring.format("%s\n%s", fa_list, ga_list)
	for link in raw_list:gmatch(linkpatt) do
		pagename = link:gsub("%[%[(.*)%]%]", "%1")
		pagename = pagename:gsub("(.*)%|.*", "%1")
		if not pagename:find("Unidentified") then
			list[#list + 1] = pagename
		end
	end
 
	local not_done = true
	local num = 0
	local selected = ""
	while not_done do
		math.randomseed(os.time())
		num = math.random(1, #list)
		pagetext = mw.title.new(list[num]):getContent()
		pagetext = pagetext:gsub("%=%=(.*)", "")
		filename = pagetext:match(".*%[%[File:(.*)%]%].*")
		if filename then
			selected = list[num]
			not_done = false
		end
	end
 
-- Extract the page name and load it
	return list[num]
end
 
local function queueify(pagename)
	local file_string = ""
	local main_link = ""
	local pagetext = mw.title.new(pagename):getContent()
	local x = pagetext:gsub("%=%=(.*)", "")
 
-- Generate image
	local filename = x:match(".*%[%[File:(.*)%]%].*")
	if filename then
		filename = filename:gsub("%|.*", "")
		filename = filename:gsub("%]%].*", "")
		file_string = string.format("[[File:%s|left|link=%s]]\n", filename, pagename)
	end
 
-- Extract the bolded segment
	local bolded = ""
	local front_italics = false
	local end_italics = false
	local intro = x:gsub(".*()%}%}\n(.*)", "%2")
 
	local s1, e1 = intro:find("'''")
	if intro:sub(e1 + 1, e1 + 2) == "''" then
		e1 = e1 + 2
		front_italics = true
	end
	local s2, e2 = intro:find("'''", e1 + 1)
	if intro:sub(e2 + 1, e2 + 2) == "''" then
		s2 = s2 + 2
		end_italics = true
	end
 
	if front_italics and end_italics then
		bolded = intro:sub(e1 + 1, s2 - 3)
		main_link = string.format("[[%s|%s]]", pagename, bolded)
		s2 = s2 - 2
	elseif front_italics then
		bolded = intro:sub(e1 - 1, s2 - 1)
		main_link = string.format("[[%s|%s]]", pagename, bolded)
		e1 = e1 - 2
	elseif end_italics then
		bolded = intro:sub(e1 + 1, s2 - 1)
		main_link = string.format("[[%s|%s]]", pagename, bolded)
	else
		bolded = intro:sub(e1 + 1, s2 - 1)
		main_link = string.format("[[%s|%s]]", pagename, bolded)
	end
 
	intro = intro:sub(0, e1) .. main_link .. intro:sub(s2)
 
-- Substitute templates and add {{Rm}}
	intro = intro:gsub("\{\{'s\}\}", "<span style=\"padding-left: 0.1em;\">'</span>s")
	intro = intro:gsub("\n\n$", string.format(" '''([[%s|Read more&hellip;]])'''", pagename))
 
	return file_string .. intro
end
 
function p._main(args)
	local pagename, result
	if not args.mode then
		args.mode = "both"
	end
	if args.page then
		pagename = args.page
	else
		pagename = pick_article(string.lower(args.mode))
	end
 
	result = queueify(pagename)
	return result
end
 
function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		v = v:match('^%s*(.-)%s*$') -- trim whitespace
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.