Module:RandomCharacter

local p = {} local function escapePattern(str) return mw.ustring.gsub(str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1") end local start = '' local stop = '' 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('Wookieepedia:Catagory:Character'):getContent		fa_list = fa_list:match(listpatt)	end	if (mode == "ga" or mode == "both") then		ga_list = mw.title.new('Wookieepedia:Catagory:Character'):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(".*%.*")		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(".*%.*") if filename then filename = filename:gsub("%|.*", "") filename = filename:gsub("%]%].*", "") file_string = string.format("\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", pagename, bolded) s2 = s2 - 2 elseif front_italics then bolded = intro:sub(e1 - 1, s2 - 1) main_link = string.format("%s", pagename, bolded) e1 = e1 - 2 elseif end_italics then bolded = intro:sub(e1 + 1, s2 - 1) main_link = string.format("%s", pagename, bolded) else bolded = intro:sub(e1 + 1, s2 - 1) main_link = string.format("%s", pagename, bolded) end

intro = intro:sub(0, e1) .. main_link .. intro:sub(s2) -- Substitute templates and add intro = intro:gsub("\{\{'s\}\}", "' s") intro = intro:gsub("\n\n$", string.format(" (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