Jump to content

Module:Title monthname/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrownHairedGirl (talk | contribs) at 15:01, 16 July 2020 (tweak). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--[[ v02
]]


-- globals for this module
local p = {}

local MyLog = ""

local monthList = {
	'January',
	'February',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December'
}

function splitToWords(str)
	result = {}
	index = 1
	s = mw.ustring.gsub(str, "^[%s%p]+", "") -- strip leading whitespace or punctuation
	for s2 in mw.ustring.gmatch(s, "^[^%s%p]+[%s%p]+") do
		result[index] = s2
		index = index + 1
	end
return result

end
-- parse the pagename to see if the supplied monthname can be matched as a complete word
-- i.e. that any preceding or trailing character is eithar a space character or a punctuation character
function findMonthNameInPagename(pn, month)
	-- first check whether the string is found at all
	if (mw.ustring.find(pn, month) == nil) then
		return nil
	end
	
	-- So the pagename does contain the monthname
	-- Now we need to check that it's a complete word
	-- The test above will match the month "May" in the string "County Mayo"
	-- ... and we need to eliminate such matches
	
	-- check for complete match
	if pn == month  then
		return month
	end
	-- check for match at start of string
	if (mw.ustring.match(pn, "^" .. month .. "[%s%p]") ~= nil) then
		return month
	end
	-- check for match at end of string
	if (mw.ustring.match(pn, "[%s%p]" .. month .. "$") ~= nil) then
		return month
	end
	-- check for match in middle of string
	if (mw.ustring.match(pn, "[%s%p]" .. month .. "[%s%p]") ~= nil) then
		return month
	end
	
	-- we haven't got a match, so this is a fail
	return nil
end

function checkPagename(pn)
	-- check each month in turn
	-- if there is a match, then 
	foundMonthNames = {}
	for i, aMonth in ipairs(monthList) do
		if (findMonthNameInPagename(pn, aMonth)  ~= nil) then
			foundMonthNames[aMonth] = mw.ustring.find(pn, aMonth)
		end
	end
	-- did we get any matches?
	if (table.getn(foundMonthNames) == 0) then
		-- no match
		return nil
	end
	
	-- so we have matches
	table.sort(foundMonthNames)
	return foundMonthNames[1]

end

function p.main(frame)
	-- get the page title
		thispage = mw.title.getCurrentTitle()
		thispagename = thispage.text;
	
retvalx = ""
	for n, w in ipairs (splitToWords(thispagename)) do
		retvalx = retvalx .. "* " .. n .. " = " .. w .. "\n"
	end
	return retvalx

-- return checkPagename(thispagename)

end

return p