Module:Infobox Tools

From IcehouseOrg
Jump to: navigation, search

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

local str = {}

--Clean wiki text from list game designers and delimt with commas
function str.clean_designer(frame)
    local source_str = tostring(frame.args[1]);
    local output_str;
    
    --escape pattern characters and remove problem ones
    output_str = str._scrubtext(source_str)
    output_str = string.gsub(output_str, "%%", "");
    
    --removes usernames (looks cleaner)
    output_str = string.gsub(output_str, "User:%w+,", "");
    output_str = string.gsub(output_str, "user:%w+,", "");
    
    --remove extra text and delimit two items by a single comma
    output_str = string.gsub(output_str, "Designed", "");
    output_str = string.gsub(output_str, "designed", "");
    output_str = string.gsub(output_str, " by ", "");
    output_str = string.gsub(output_str, " with ", "");
    output_str = string.gsub(output_str, "&", ",");
    output_str = string.gsub(output_str, " and ", ",");
    output_str = string.gsub(output_str, ", ,", ",");
    output_str = string.gsub(output_str, ",,", ",");
    
    
    return output_str;
end

--Clean wiki text from list of description string
function str.clean_description(frame)
    local source_str = tostring(frame.args[1]);
    local output_str;
    
    --escape pattern characters and remove problem ones
    output_str = str._scrubtext(source_str)
    output_str = string.gsub(output_str, "%%", "");
    
    return output_str;
end

--Guess the qualitative game length based on input playing time
function str.guess_length(frame)
	local in_min_str = tostring(frame.args[1]);
	local in_max_str = tostring(frame.args[2]);
	local min_time;
	local max_time;
	local output_str;
	
	--test of numbers, strip text with units from input, and convert to numbers
	first_min_num = string.find(in_min_str, "%d")
    if first_min_num ~= nil then
		in_min_str = string.gsub(in_min_str, "%D", "")
		min_time = tonumber(in_min_str)
	else
		min_time = 1
	end
	
	first_max_num = string.find(in_max_str, "%d")
    if first_max_num ~= nil then
		in_max_str = string.gsub(in_max_str, "%D", "")
		max_time = tonumber(in_max_str)
	else
		max_time = -1
	end
	
	--guess game length based on playing time
	if (max_time < 0 and min_time < 30) then
		output_str = "unknown"
	elseif (min_time < 10 and max_time <= 15) then
		output_str = "Fast?"
	elseif (min_time < 30 and max_time <= 45) then
		output_str = "Medium?"
	else
		output_str = "Long?"
	end
	
	return output_str;
end

--Guess the number of trios per color needed
--NOTE WELL: once all of the game pages are upgraded, this function can be deleted
--"sets" means five-color "Treehouse stashes" of 1 trio each of 5 colors
function str.guess_trios_per_color(frame)
	local in_sets_str = tostring(frame.args[1]);
	local output_str;
	
	--Assumes 5 trios per color unless noted otherwise
	first_sets_alphanum = string.find(in_sets_str, "%w")
    if (first_sets_alphanum ~= nil) then
		output_str = in_sets_str
	else
		output_str = "5"
	end
	
	return output_str;
end

--Guess the number of colors needed
--NOTE WELL: once all of the game pages are upgraded, this function can be deleted
--"stashes" means old-style MONOCHROME stashes of 5 trios in a 1 color
function str.guess_number_of_colors(frame)
	local in_stashes_str = tostring(frame.args[1]);
	local output_str;
	
	--Assumes 5 five colors unless noted otherwise
	first_stashes_alphanum = string.find(in_stashes_str, "%w")
    if (first_stashes_alphanum) ~= nil then
		output_str = in_stashes_str
	else
		output_str = "5"
	end
	
	return output_str;
end

--Clean wiki text from list of other_equip and delimit with commas
function str.clean_equip(frame)
	local source_str = tostring(frame.args[1]);
    local output_str;
    
    --escape pattern characters and remove problem ones
	output_str = str._scrubtext(source_str)
    output_str = string.gsub(output_str, "%%", "");
    
    --delimit two items by a single comma
    output_str = string.gsub(output_str, "&", ",");
    output_str = string.gsub(output_str, " and ", ",");
    output_str = string.gsub(output_str, ", ,", ",");
    output_str = string.gsub(output_str, ",,", ",");
    output_str = string.gsub(output_str, " , ", "");
    
    return output_str;
end

--Create wiki links to common extra equipment
--note: this only works for certain standardized component names (NEEDS UPDATING)
--WARNING! This might run slowly!
function str.linkify_equip(frame)
	local source_str = tostring(frame.args[1]);
    local output_str;
    
    --escape pattern characters and remove problem ones
	output_str = str._scrubtext(source_str)
    output_str = string.gsub(output_str, "%%", "");
    
    --create links
    output_str = string.gsub(output_str, "Chessboard", "[[Chessboard]]");
    output_str = string.gsub(output_str, "chessboard", "[[Chessboard]]");
    output_str = string.gsub(output_str, "Volcano board", "[[Volcano board]]");
    output_str = string.gsub(output_str, "Martian Coaster", "[[Martian Coaster board|Martian Coaster]]");
    output_str = string.gsub(output_str, "Wheel board", "[[Wheel board]]");
    output_str = string.gsub(output_str, "Stash pad", "[[Stash pad]]");
    
    output_str = string.gsub(output_str, "Piecepack", "[[Piecepack]]");
    
    output_str = string.gsub(output_str, "Playing cards", "[[Playing cards]]");
    output_str = string.gsub(output_str, "playing cards", "[[playing cards]]");
    output_str = string.gsub(output_str, "Zark City", "[[Zark City deck|Zark City]]");
    
    output_str = string.gsub(output_str, "Dice", "[[Dice|Dice]]");
    output_str = string.gsub(output_str, "dice", "[[Dice|dice]]");
    output_str = string.gsub(output_str, "Die", "[[Dice|Die]]");
    output_str = string.gsub(output_str, "die", "[[Dice|die]]");
    
    output_str = string.gsub(output_str, "bag", "[[Opaque bag|bag]]");
    output_str = string.gsub(output_str, "tokens", "[[Marking_stone|tokens]]");
    output_str = string.gsub(output_str, "stones", "[[Marking_stone|stones]]");
    output_str = string.gsub(output_str, "Poker chips", "[[Poker chips]]");
    output_str = string.gsub(output_str, "poker chips", "[[poker chips]]");
    	
    return output_str;
end

--Clean and concat list of game mechanics
function str.clean_mechs(frame)
	local game_m = tostring(frame.args[1]) or "";
	local old_prime_m = tostring(frame.args[2]) or "";
	local old_add_m = tostring(frame.args[3]) or "";
    local merged_str = " ";
    local output_str;
    local any_vals_inputted = 0;
    
    --merging the list together in (hopefuly) a smart way
    first_game_alphanum = string.find(game_m, "%w")
    if first_game_alphanum ~= nil then
    	merged_str = game_m
    	any_vals_inputted = 1
    end
    
    first_old_prime_alphanum = string.find(old_prime_m, "%w")
    if (any_vals_inputted > 0 and first_old_prime_alphanum ~= nil) then
    	merged_str = merged_str..","..old_prime_m
    elseif first_old_prime_alphanum ~= nil then
    	merged_str = old_prime_m
    	any_vals_inputted = 1
    end

    first_old_add_alphanum = string.find(old_add_m, "%w")
    if (any_vals_inputted > 0 and first_old_add_alphanum ~= nil) then
    	merged_str = merged_str..","..old_add_m
    elseif first_old_add_alphanum ~= nil then
    	merged_str = old_add_m
    	any_vals_inputted = 1
    end
    
    if any_vals_inputted > 0 then
	    --escape pattern characters and remove problem ones
	    output_str = str._scrubtext(merged_str)
	    output_str = string.gsub(output_str, "%%", "");
	    
	    --remove extra text (if empty or the user tried to link pages themselves)
	    output_str = string.gsub(output_str, "Category:", "");
	    output_str = string.gsub(output_str, ", ,", ",");
	    output_str = string.gsub(output_str, ",,", ",");
	    output_str = string.gsub(output_str, " , ", ", ");
	    output_str = string.gsub(output_str, " , ", "");
    else
    	output_str = 'unknown';
	end
    
    return output_str;
end

--Concat and create internal wiki links for a list of game mechanics
function str.linkify_mechs(frame)
	local game_m = tostring(frame.args[1]) or "";
	local old_prime_m = tostring(frame.args[2]) or "";
	local old_add_m = tostring(frame.args[3]) or "";
    local merged_str = " ";
    local output_str;
    local any_vals_inputted = 0;
    
    --merging the list together in (hopefuly) a smart way
    first_game_alphanum = string.find(game_m, "%w")
    if first_game_alphanum ~= nil then
    	merged_str = game_m
    	any_vals_inputted = 1
    end
    
    first_old_prime_alphanum = string.find(old_prime_m, "%w")
    if (any_vals_inputted > 0 and first_old_prime_alphanum ~= nil) then
    	merged_str = merged_str..","..old_prime_m
    elseif first_old_prime_alphanum ~= nil then
    	merged_str = old_prime_m
    	any_vals_inputted = 1
    end

    first_old_add_alphanum = string.find(old_add_m, "%w")
    if (any_vals_inputted > 0 and first_old_add_alphanum ~= nil) then
    	merged_str = merged_str..","..old_add_m
    elseif first_old_add_alphanum ~= nil then
    	merged_str = old_add_m
    	any_vals_inputted = 1
    end
    
    if any_vals_inputted > 0 then
	    --escape pattern characters and remove problem ones
	    output_str = str._scrubtext(merged_str)
	    output_str = string.gsub(output_str, "%%", "");
	    
	    --remove extra text (in case the user tried to link pages themselves)
	    output_str = string.gsub(output_str, "Category:", "");
	    output_str = string.gsub(output_str, ", ,", ",");
	    output_str = string.gsub(output_str, ",,", ",");
	   	output_str = string.gsub(output_str, " , ", ", ");
	    output_str = string.gsub(output_str, " , ", "");
	    
	    --expand commas and create links (in theory)
	    output_str = string.gsub(output_str, ",", "]], [[");
	    output_str = "[["..output_str.."]]";
    else
    	output_str = ""
	end
    	
    return output_str;
end

--Clean wiki text from theme
function str.clean_theme(frame)
    local source_str = tostring(frame.args[1]);
    local output_str;
    
    --escape pattern characters and remove problem ones
    output_str = str._scrubtext(source_str)
    output_str = string.gsub(output_str, "%%", "");
    
    return output_str;
end

--Clean wiki text from BGG_link
function str.clean_url(frame)
	--NOTE: this function does not seem to work as expected. Use the {{clean_URL}} template instead.
    local source_str = tostring(frame.args[1]);
    local output_str;
    
    --escape pattern characters and remove problem ones
	output_str = str._scrubtext(source_str)
	output_str = string.gsub(output_str, ",", " ");
    
    --cut off link text
    end_index = string.find(output_str, "%s+", 10)
    if end_index then
    	output_str = string.sub(output_str, 1, end_index)
    end
 
    --put back certain escaped charaters that might haave been part of the URL   
    output_str = string.gsub(output_str, "%+", "+");
    output_str = string.gsub(output_str, "%-", "-");
    output_str = string.gsub(output_str, "%.", ".");
    output_str = string.gsub(output_str, "%%", "%");

    return output_str;
end

--Helper function that escapes pattern characters and cleans out wiki markup
function str._scrubtext(str_with_wikitext)
	clean_str = string.gsub(str_with_wikitext, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1");
    clean_str = string.gsub(clean_str, "|", ",");
    clean_str = string.gsub(clean_str, "%[", "");
    clean_str = string.gsub(clean_str, "%]", "");
    
    return clean_str;
end

return str