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 14:00, 16 July 2020 (tweak diagnostics). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--[[ v03
 -- try again 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
	position = mw.ustring.find(pn, month)
	if (position == nil) then
		return 0
	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 1
	end
	-- check for match at start of string
	if (mw.ustring.match(pn, "^" .. month .. "[%s%p]") ~= nil) then
		return position
	end
	-- check for match at end of string
	if (mw.ustring.match(pn, "[%s%p]" .. month .. "$") ~= nil) then
		return position
	end
	-- check for match in middle of string
	if (mw.ustring.match(pn, "[%s%p]" .. month .. "[%s%p]") ~= nil) then
		return position
	end
	
	-- we haven't got a match, so this is a fail
	return 0
end

function checkPagename(pn)
	-- check each month in turn
	-- if there is a match, then 
	debuglog = ""
	foundMonthNames = {}
	for i, aMonth in ipairs(monthList) do
		posn = findMonthNameInPagename(pn, aMonth)
		foundMonthNames[aMonth] = posn
		debuglog = debuglog .. "* " .. aMonth .. ":" .. posn .. "\n"
	end
	debuglog = debuglog .. "\n----\n"
	debuglog = debuglog .. "<nowiki>#</nowiki>foundMonthNames = " .. #foundMonthNames .. "\n----\n"
	-- did we get any matches?
	if (table.getn(foundMonthNames) == 0) then
		-- no match
		return debuglog .. "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 debuglog .. retval
end

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

return p