Jump to content

Module:Title monthname/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
add nomatch parameter
v08 -- supports parameter match=
Line 1: Line 1:
--[[
--[[
Split the title into words then test each of them against the list of months
Split the title into words then test each of them against the list of months
v07 -- accepts a parameter
v08 -- supports parameter match=
]]
]]


local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}

-- config
local nomatch = ""
local matchnum = 1


local monthList = {
local monthList = {
Line 41: Line 45:
function checkPagename(pn)
function checkPagename(pn)
-- split the pagename into sparate words
-- split the pagename into sparate words
titleWords =splitIntoWords(pn)
titleWords = splitIntoWords(pn)
nMatches = 0
myMatches ={}
-- check each words in turn, to see if it matches a month
-- check each words in turn, to see if it matches a month
Line 49: Line 56:
for i, thisMonth in ipairs(monthList) do
for i, thisMonth in ipairs(monthList) do
if (thisMonth == thisWord) then
if (thisMonth == thisWord) then
nMatches = nMatches + 1
return thisMonth
myMatches[nMatches] = thisMonth
end
end
end
end
end
end


if (nMatches == 0) then
--if we get here, none of the title words matches a whole month
-- none of the title words matches a whole month
return ""
return nomatch
end
if ((matchnum >= 1) and (matchnum <= nMatches)) then
return myMatches[matchnum]
end


if (matchnum < 0) then
matchnum = matchnum + 1 -- so that -1 is the last match etc
if ((matchnum + nMatches) >= 1) then
return myMatches[matchnum]
end
end
return nomatch
end
end


Line 65: Line 87:


function p._main(args)
function p._main(args)
nomatch = ""
if (args['nomatch'] ~= nil) then
if (args['nomatch'] ~= nil) then
nomatch = args['nomatch']
nomatch = args['nomatch']
end
if (args['match'] ~= nil) then
matchnum = tonumber(args['match'])
if ((matchnum == nil) or (matchnum == 0)) then
matchnum = 1
end
end
end

Revision as of 17:52, 16 July 2020

--[[ 
     Split the title into words then test each of them against the list of months
     v08 -- supports parameter match=
]]

local getArgs = require('Module:Arguments').getArgs
local p = {}

-- config
local nomatch = ""
local matchnum = 1

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

-- splits a string into "words"
-- a "word" is a set of characters delineated at each end by one 
--    or more whitespace characters or punctaution charaters
function splitIntoWords(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
		s3 = mw.ustring.gsub(s2, "[%s%p]+$", "") -- strip trailing separators
		result[index] = s3
		index = index + 1
	end
return result
end

-- returns the first word is the pagename which matches the name of a month
-- ... or an empty string if there is no match
function checkPagename(pn)
	-- split the pagename into sparate words
	titleWords = splitIntoWords(pn)
	
	nMatches = 0
	myMatches ={}
	
	-- check each words in turn, to see if it matches a month
	for w, thisWord in ipairs(titleWords) do
		-- check agaist each month
		-- if there is a match, then return that monthname
		for i, thisMonth in ipairs(monthList) do
			if (thisMonth == thisWord) then
				nMatches = nMatches + 1
				myMatches[nMatches] = thisMonth
			end
		end
	end

	if (nMatches == 0) then
		-- none of the title words matches a whole month
		return nomatch
	end
	
	if ((matchnum >= 1) and (matchnum <= nMatches)) then
		return myMatches[matchnum]
	end

	if (matchnum < 0) then
		matchnum = matchnum + 1 -- so that -1 is the last match etc
		if ((matchnum + nMatches) >= 1) then
			return myMatches[matchnum]
		end
	end
	
	return nomatch
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	if (args['nomatch'] ~= nil) then
		nomatch = args['nomatch']
	end
	
	if (args['match'] ~= nil) then
		matchnum = tonumber(args['match'])
		if ((matchnum == nil) or (matchnum == 0)) then
			matchnum = 1
		end
	end
	
	thispagename = nil
	if (args['page'] ~= nil) then
		thispagename = args['page']
	else
		-- get the page title
		thispage = mw.title.getCurrentTitle()
		thispagename = thispage.text;
	end
	result = checkPagename(thispagename)
	if (result == "") then
		return nomatch
	end
	return result
end

return p