Difference between revisions of "Module:Sandbox"
From Nookipedia, the Animal Crossing wiki
PanchamBro (talk | contribs) m |
PanchamBro (talk | contribs) (adding citation) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 59: | Line 59: | ||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
− | + | local name = args['1'] or '' | |
− | return p. | + | local titleCase = titlecase(name) |
+ | return p.checkWords(titleCase) | ||
end | end | ||
− | function p. | + | -- From Lua-User Wiki: http://lua-users.org/wiki/SciteTitleCase |
+ | function titlecase(str) | ||
+ | local titleName = '' | ||
+ | local buf = {} | ||
+ | local inWord = false | ||
+ | for i = 1, #str do | ||
+ | local c = string.sub(str, i, i) | ||
+ | if inWord then | ||
+ | table.insert(buf, string.lower(c)) | ||
+ | if string.find(c, '%s') or string.find(c, '-') then | ||
+ | inWord = false | ||
+ | end | ||
+ | else | ||
+ | table.insert(buf, string.upper(c)) | ||
+ | inWord = true | ||
+ | end | ||
+ | end | ||
+ | return table.concat(buf) | ||
+ | end | ||
+ | |||
+ | function p.checkWords(name) | ||
local words = split( name, " " ) | local words = split( name, " " ) | ||
local word = "" | local word = "" | ||
local wordList = "" | local wordList = "" | ||
local fullName = "" | local fullName = "" | ||
− | |||
for k, w in ipairs(words) do | for k, w in ipairs(words) do | ||
− | + | finalWord = keepLowercase(w:gsub("^%s*(.-)%s*$", "%1")) .. " " | |
− | wordList = wordList .. string.gsub( | + | wordList = wordList .. string.gsub(finalWord, "\s+$", "") |
end | end | ||
Revision as of 17:13, November 21, 2022
Module documentation (view)
Usage
This is the general sandbox page to test out Lua modules. The module should not be implemented in a template.
local p = {}
local getArgs = require('Module:Arguments').getArgs
function split(str, pattern)
local out = {}
for m in string.gmatch(str, "[^" .. pattern .. "]+") do
table.insert(out, m)
end
return out
end
function isEmpty(s)
return s == nil or s == ''
end
function keepLowercase(str)
local small = {
["And"] = "and",
["An"] = "an",
["As"] = "as",
["At"] = "at",
["By"] = "by",
["En"] = "en",
["De"] = "de",
["For"] = "for",
["If"] = "if",
["In"] = "in",
["N"] = "n",
["Nor"] = "nor",
["Of"] = "of",
["On"] = "on",
["Or"] = "or",
["Only"] = "only",
["Over"] = "over",
["Per"] = "per",
["So"] = "so",
["Some"] = "some",
["That"] = "that",
["Than"] = "than",
["The"] = "the",
["To"] = "to",
["Upon"] = "upon",
["Vs"] = "vs",
["Versus"] = "versus",
["Via"] = "via",
["Where"] = "where",
["When"] = "when",
["With"] = "with",
["Without"] = "without",
["Yet"] = "yet"
}
if small[str:gsub("^%l", string.upper)] then
return small[str:gsub("^%l", string.upper)]
else
return str:gsub("^%l", string.upper)
end
end
function p.main(frame)
local args = getArgs(frame)
local name = args['1'] or ''
local titleCase = titlecase(name)
return p.checkWords(titleCase)
end
-- From Lua-User Wiki: http://lua-users.org/wiki/SciteTitleCase
function titlecase(str)
local titleName = ''
local buf = {}
local inWord = false
for i = 1, #str do
local c = string.sub(str, i, i)
if inWord then
table.insert(buf, string.lower(c))
if string.find(c, '%s') or string.find(c, '-') then
inWord = false
end
else
table.insert(buf, string.upper(c))
inWord = true
end
end
return table.concat(buf)
end
function p.checkWords(name)
local words = split( name, " " )
local word = ""
local wordList = ""
local fullName = ""
for k, w in ipairs(words) do
finalWord = keepLowercase(w:gsub("^%s*(.-)%s*$", "%1")) .. " "
wordList = wordList .. string.gsub(finalWord, "\s+$", "")
end
local root = mw.html.create()
root:wikitext(wordList)
return root
end
return p