Module:Segment

-- Purpose: Unify the handling of the growing lists of conditions and exceptions that appear in Template:Yahoo and Template:Segment,

local p = {}

function whitespace_sanitize(parameter_string) -- trying to clear out the leading and trailing whitespace that comes from block formatting. local results = nil if parameter_string ~= nil then results = string.match(parameter_string, "^[\n\r%s]*(.-)[\n\r%s]*$") -- I'm pretty new to regex stuff, this probably doesn't actually work. if results == "" then results = nil end end return results end

function name_replace(sender, autocat) local results if sender ~= nil or sender == "" then

local replace_list = { ["Rachel Spurling"] = "Rachel Rosing", ["Emma Kantt"] = "Ben Kantt", ["JPhonic"] = "Jordan Mallory", ["ActionAlan"] = "Alan Black", }		local sender_orig = sender local sender_repl = replace_list[sender_orig] or sender_orig if mw.title.new(sender_repl).exists then -- just ignore the original value altogether results = "" .. sender_repl .. "" --			if sender_orig ~= sender_repl then --				-- page exists, needs replacement --				results = "" .. sender_orig .. "" --			else --				-- page exists, no replacement --				results = "" .. sender_repl .. "" --			end else results = sender_repl end if autocat then results = results .. ""		end end

return results end

function time_stamping(timestamp) -- timestamping should be a pretty universal element to these segments, handle that here local results if timestamp ~= nil then timestamp = whitespace_sanitize(timestamp) or "" -- automatically convert h:mm:ss format to (m)mm:ss format. -- if string doesn't match regex, it'll just continue as usual results = string.gsub(timestamp, "([0-9]+):([0-9]+):([0-9]+)", function(h,m,s) return string.format("%d:%s", tonumber(h) * 60 + tonumber(m), s) end) else -- default value results = "??:??" end results = results .. " - "	return results end

function p.yahoo(frame) local label = "Y" local yahooService = "Yahoo! Answers" local link = whitespace_sanitize(frame.args["Link"]) local final = whitespace_sanitize(frame.args["Final"]) local autocat = whitespace_sanitize(frame.args["AutoCategory"]) or true local sender = name_replace(whitespace_sanitize(frame.args["Sender"]), autocat) -- Going with "sender" as the universal term for Yahoo and Segment local yuser = whitespace_sanitize(frame.args["YahooUser"]) local gamerec = whitespace_sanitize(frame.args["GameRecognition"]) local title = whitespace_sanitize(frame.args["Title"]) local body = whitespace_sanitize(frame.args["Body"]) local service = whitespace_sanitize(frame.args["ServiceName"]) or yahooService local details = whitespace_sanitize(frame.args["Details"])

-- timestamp local wikitext = time_stamping(frame.args["TimeStamp"])

-- link/label if final == "Yes" then label = "FY" end if link ~= nil then wikitext = wikitext .. "'''[" .. link .. " " .. label .. "]'''"	else wikitext = wikitext .. "'''" .. label .. "'''"	end wikitext = wikitext .. " - "

-- sender if sender ~= nil then wikitext = wikitext .. "Sent in by " .. sender .. ", from " else wikitext = wikitext .. "From " end

-- yahoo user if yuser ~= nil and yuser ~= "?" and string.sub(yuser, 1, 1) ~= "\"" then		wikitext = wikitext .. service .. " user " .. yuser	elseif yuser ~= nil and yuser ~= "?" then		wikitext = wikitext .. "an anonymous " .. service .. " user whom Griffin calls " .. yuser	else		wikitext = wikitext .. "an unknown " .. service .. " user" -- TODO: Maybe special handling for cases where Griffin says the user was deleted?	end	wikitext = wikitext .. ", who asks:"

-- game recognition if gamerec ~= nil then wikitext = wikitext .. frame:expandTemplate{ title = 'GameRecognition', args = { gamerec } } end --	local block = mw.html.create('blockquote') --	block:css('padding-bottom', '0') --	if title ~= nil then --		block --			:tag('span') --			:css('font-size', '24px') --			:css('font-weight', '300') --			:css('margin-bottom', '10px') --			:wikitext(title) --			:done --	end --	if body ~= nil then --		if title ~= nil then block:tag('br') end -- Does scribunto auto-close br in this instance? Does it matter? --		block --			:tag('span') --			:css('font-size', '13px') --			:css('font-weight', 'normal') --			:wikitext(body) --			:done --	end --	if details ~= nil then --		if title ~= nil or body ~= nil then block:tag('br') end --		block --			:wikitext("Additional Details: " .. details) --			:alldone --	end

local block = "" -- title if title ~= nil then block = block .. "" .. title .. " "		-- body if body ~= nil then block = block.. " " .. body .. " "		end end block = block .. " "

wikitext = wikitext .. block if service ~= yahooService then wikitext = wikitext .. ""		end

return wikitext end

function p.money_zone(frame) -- Purpose: Standardize the way that sponsors and jumbotrons are listed. Also, filter out sponsors who have done questionable things, so that the data is preserved but not displayed in actual wikitext. local sponsors   = whitespace_sanitize(frame.args["Sponsors"]) local podcasts   = whitespace_sanitize(frame.args["Podcasts"]) local jumbotrons = whitespace_sanitize(frame.args["Jumbotrons"])

local wikitext = "" local cats = "" local ignored_sponsors = {} if sponsors ~= nil then for i, ignore in ipairs(ignored_sponsors) do string.gsub(sponsors, ignore .. "?", "") end

wikitext = "Sponsored by " .. sponsors .. "."		for substr in string.gmatch(sponsors, "([^,]+)") do cats = cats .. ""		end end if podcasts ~= nil then if wikitext ~= "" then wikitext = wikitext .. " "		end wikitext = wikitext .. "Advertisement for ''" .. podcasts .. "''" .. "."	end if jumbotrons ~= nil then if wikitext ~= "" then wikitext = wikitext .. " "		end wikitext = wikitext .. "Jumbotron message for " .. jumbotrons .. "."	end wikitext = time_stamping(frame.args["TimeStamp"]) .. "MZ - " .. wikitext .. cats return wikitext end

function p.segment(frame) -- Like Template:Segment, handle any segments that don't have specific handling here. local title = whitespace_sanitize(frame.args["Title"]) local body = whitespace_sanitize(frame.args["Body"]) local sender = name_replace(whitespace_sanitize(frame.args["Sender"]), nil) local segment_title

-- still using a string, because I think lua sets are ugly local segments_without_category = "Intro, Email, Question, Housekeeping, Quote" local question_segments = "Question, Email, Formspring, Twitter" -- timestamp local wikitext = time_stamping(frame.args["TimeStamp"]) -- title -- uncaught error when title is not provided local link_start = string.find(title, "%[") if link_start == nil then if mw.title.new(title).exists then wikitext = wikitext .. "" .. title .. "" else wikitext = wikitext .. "'''" .. title .. "'''"		end segment_title = title else -- if you give it "Munch Squad Jr" -- or "Second Munch Squad" -- it will add the correct category -- if you give it "Munch Squad]" -- it will only add it as wikitext with no additional processing -- this is not recommended behavior, but the code falls here as a failsafe wikitext = wikitext .. "'''" .. title .. "'''"		-- unhandled behavior: multiple links for match in string.gmatch(title, "%[%[([^%|%]]*)") do			segment_title = match end end -- body if body ~= nil then wikitext = wikitext .. " - " .. body end -- sender if sender ~= nil then if string.find(question_segments, segment_title) ~= nil then wikitext = wikitext .. " -- " .. sender else -- addtl context for other segments where someone may send something in (eg Munch Squad) wikitext = wikitext .. " -- Sent in by " .. sender end end if string.find(segments_without_category, segment_title) == nil then wikitext = wikitext .. ""	end return wikitext end

return p