Module:Sandbox/BrownHairedGirl/valueFunc

This is an old revision of this page, as edited by BrownHairedGirl (talk | contribs) at 19:36, 31 March 2018 (myArgs). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
	-- each title consists of 3 parts
	--    * prefix
	--    * county name
	--    * suffix
	-- e.g. "Foo in County Mayo"
	--    * prefix = "Foo in "
	--    * county name = "County Mayo"
	--    * suffix = ""
	-- e.g. "County Sligo-related lists"
	--    * prefix = ""
	--    * county name = "County Sligo"
	--    * suffix = "-related lists"

function nil_or_value(s)
	if (s == nil) then
		return "nil"
	end
	return s
end
	


local debugmsg = " "
local getArgs = require('Module:Arguments').getArgs
local p = {}
	
	
function make_cat_link(catname, disp)
	local displaytext
	if (disp ~= "") then
		displaytext = disp
	else
		displaytext = catname
	end
	local link = "[[:Category:" .. catname .. "|" .. displaytext .. "]]"
	local fmtlink
	local linktitle = mw.title.new( catname, "Category" )
	if (linktitle.exists) then
		fmtlink = link
	else
		fmtlink = "<span style=\"color:#888\">" .. displaytext .. "</span>"
	end

	return fmtlink
end

function make_cat_name(countyname, prefix, suffix, nocounty)
	local this_cat_name = '';
	this_cat_name = this_cat_name .. prefix
	if not (nocounty) then
		this_cat_name = this_cat_name .. 'County ';
	end
	this_cat_name = this_cat_name .. countyname
	this_cat_name = this_cat_name .. suffix
	return this_cat_name
end

function parse_pagename(pn)
	debuglog(1, "parse_pagename: [" .. pn .. "]")
	debuglog(2, "simple parse")
	match_prefix, match_county, match_suffix = string.match(pn, "^(.*)(County%s+%a+)(.*)$")
	if (match_county == nil or match_county == '') then
		debuglog(3, "No match_county")
		return false
	end
	debuglog(3, "match_prefix = [" .. match_prefix .. "]")
	debuglog(3, "match_county = [" .. match_county .. "]")
	debuglog(3, "match_suffix = [" .. match_suffix .. "]")
	return true
end

function debuglog(level, msg)
if false then
	if (debugmsg == "") then -- we are not debugging
		return false
	end

	if (string.match(debugmsg, "^%s+$")) then
		debugmsg = "'''Debugging''':\n\n"
	end
end
	debugmsg = debugmsg .. "\n"
	if (level == 1) then
		debugmsg = debugmsg .. "# "
	elseif  (level == 2) then
		debugmsg = debugmsg .. "#* "
	elseif  (level == 3) then
		debugmsg = debugmsg .. "#*# "
	end
	debugmsg = debugmsg .. msg
	return true
end


function argValueFunc(value)
	if (value == nil) then
		value = '' -- nil value = blank
	end
	value = mw.ustring.gsub(value, "^%s+$", "") -- only whitespace, so replace with ''
	return value
end

		
function p.main(frame)
-- getArgs
-- In all cases, convert to blank (i.e. '')
--   * a nil value
--   * a value consisting only of whitespace
-- for the third parameter ("nospace"), trim whitespace and convert to lowercase

local myArgs = {}
	myArgs[1] = argValueFunc(frame.args[1])
	myArgs[2] = argValueFunc(frame.args[2])
	myArgs[3] = argValueFunc(frame.args[3])
	myArgs[3] = mw.text.trim(myArgs[3]:lower())


	debuglog(1, "myArgs")
	debuglog(2, "myArgs[1] = [" .. myArgs[1] .. "]")
	debuglog(2, "myArgs[2] = [" .. myArgs[2] .. "]")
	debuglog(2, "myArgs[3] = [" .. myArgs[3] .. "]")

	-- now set the key variables
	title_prefix = myArgs[1]
	title_suffix = myArgs[2]
	title_nocountyword = false
	if (myArgs[3] == 'nocountyword') then
		title_nocountyword = true
	end
	debuglog(1, "set main variables")
	debuglog(2, "title_prefix = [" .. title_prefix .. "]")
	debuglog(2, "title_prefix = [" .. title_prefix .. "]")
	
	-- get the page title
	thispage = mw.title.getCurrentTitle()
	thispagename = thispage.text;
	
	debuglog(1, "mw.title.getCurrentTitle()")
	debuglog(2, "thispage.text = [" .. thispage.text .."]")
	debuglog(2, "thispage.namespace = [" .. thispage.namespace .."]")
	debuglog(2, "thispage.nsText = [" .. thispage.nsText .."]")
	debuglog(2, "is it a cat? using (thispage:inNamespace(14))")
	if (thispage:inNamespace(14)) then
		debuglog(3, "yes, this is a category")
	else
		debuglog(3, "no, this is not a category")
	end

	-- do we need to parse the page title?
	-- if neither title_prefix nor title_suffix was supplied as a parameter, then yes
	-- test by concatenating them, and see if the combination is blank
	if ((title_prefix .. title_suffix) == '') then
		debuglog(1, "Yes, we need to parse the page title")
		parse_pagename()
	else
		debuglog(1, "No, we don't need to parse the page title")
	end
	return debugmsg


end

return p