Module:Title monthname/sandbox: Difference between revisions
Appearance
Content deleted Content added
fix |
args['page'] |
||
Line 66: | Line 66: | ||
function p._main(args) |
function p._main(args) |
||
thispagename = nil |
thispagename = nil |
||
if (args[ |
if (args['page'] ~= nil) then |
||
thispagename = args[1] |
thispagename = args[1] |
||
else |
else |
Revision as of 16:22, 16 July 2020
![]() | This is the module sandbox page for Module:Title monthname (diff). |
![]() | This Lua module is used on approximately 29,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
For use by Template:Title monthname. See docuentation there.
--[[
Split the title into words then test each of them against the list of months
v07 -- accepts a parameter
]]
local getArgs = require('Module:Arguments').getArgs
local p = {}
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)
-- 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
return thisMonth
end
end
end
--if we get here, none of the title words matches a whole month
return ""
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
function p._main(args)
thispagename = nil
if (args['page'] ~= nil) then
thispagename = args[1]
else
-- get the page title
thispage = mw.title.getCurrentTitle()
thispagename = thispage.text;
end
return checkPagename(thispagename)
end
return p