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 13:25, 16 July 2020 (diag). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--[[ v02
 -- tries to use the first month in the page title
]]


-- globals for this module
local p = {}

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


-- 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 "no matches"	
	end
	
	-- so we have matches
--[[
	table.sort(foundMonthNames)
	return foundMonthNames[1]
]]

retval = ""
	for m, n in ipairs(foundMonthNames) do
		retval = retval .. m .. " = " .. n .. "\n"
	end
	return retval
end

function p.main(frame)
	-- get the page title
	thispage = mw.title.getCurrentTitle()
	thispagename = thispage.text;
	

return checkPagename(thispagename)

end

return p