My Brother, My Brother and Me Wiki
Advertisement

Documentation for this module may be created at Module:Sandbox/doc

local p = {}

function p.test(frame)
	local number_of_args = ""
	
	for k,v in pairs(frame.args) do
		number_of_args = number_of_args .. k .. ": " .. v .. ", "
	end
	return number_of_args
end

function p.Sandbox(frame)
local title = mw.title.new("asdf")
local content = title:getContent()
local length = string.len(content)
return length
end


-- begin navbox
local function renderTitleRow(tbl, title)
	if title ~= nil then
		local titleRow  = tbl:tag('tr')
		-- this will probably always be 2 in our stripped-down implementation
		local colspan = 2
		
		local titleCell = titleRow:tag('th')
			:attr('colspan', colspan)
			:css('text-align', 'center')
			:wikitext(title)
	end
end

local function renderListRow(tbl, groupX, listX)
	if listX ~= nil then
		local listRow = tbl:tag('tr')
		local listCell
		if groupX ~= nil then
			local groupCell = listRow:tag('th')
				:attr('colspan', 1)
				:css('text-align', 'right')
				:wikitext(groupX)
			
			listCell = listRow:tag('td')
				:attr('colspan', 1)
				:css('text-align', 'left')
				:css('border-left', '1px #9ca6c2 solid')
		else
			listCell = listRow:tag('td')
				:attr('colspan', 2)
				:css('text-align', 'center')
		end
		-- args are automatically trimmed, so we need to insert newline to make lists work properly
		listCell:wikitext('\n' .. listX)
	end
end

local function renderTable(args, listnums)
	local tbl = mw.html.create('table')
		:addClass('article-table')
		:addClass('hlist')
		:addClass('nowraplinks')
		:css('border', '2px solid #9ca6c2')

	if args.title ~= nil then
		renderTitleRow(tbl, args.title)
	end
	
	for i, listnum in ipairs(listnums) do
		renderListRow(tbl, args["group" .. tostring(listnum)], args["list" .. tostring(listnum)])
	end
	
	return tbl
end

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 = nil
	
	if sender ~= nil or sender == "" then

		local replace_list = {
			["Rachel Spurling"] = "Rachel Rosing",
			["Emma Kantt"] = "Ben Kantt",
			["JPhonic"] = "Jordan Mallory",
			["ActionAlan"] = "Alan Black",
		}
		
		-- remove any trailing refs (or any <element>)
		-- append them back on later
		local sender_orig
		local additional
		for name, addtl in string.gmatch(sender, "([^<]+)(.*)$") do
			sender_orig = whitespace_sanitize(name)
			additional = addtl
		end
		
		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_repl .. "|" .. sender_orig .. "]]"
--			else
--				-- page exists, no replacement
--				results = "[[" .. sender_repl .. "]]"
--			end
		end
		results = results .. additional

		if autocat then
			results = results .. "[[Category:" .. sender_repl .. "]]"
		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
		-- 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.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|Munch Squad Jr]]"
		-- or "Second [[Munch Squad]]"
		-- it will add the correct category
		
		-- if you give it "[https://google.com 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 .. "[[Category:" .. segment_title .. "]]"
	end
	
	return wikitext
end

return p
Advertisement