Difference between revisions of "Module:Materials"
From Nookipedia, the Animal Crossing wiki
PanchamBro (talk | contribs) (using the getArgs function from Module:Arguments) |
PanchamBro (talk | contribs) (Fixing SentenceCase usage) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
+ | local convertCase = require('Module:SentenceCase').firstToUpper | ||
+ | checkCase = mw.loadData("Module:CapitalizationCheck/Data") | ||
data = mw.loadData("Module:Materials/Data") | data = mw.loadData("Module:Materials/Data") | ||
Line 10: | Line 12: | ||
local material = args[1] or args.material or '' | local material = args[1] or args.material or '' | ||
local num = args[2] or args.num or '' | local num = args[2] or args.num or '' | ||
+ | local sentenceCase = args[3] or args.sentencecase or '' | ||
if game == "PC" then | if game == "PC" then | ||
− | return p.outputMaterialPC(material, num, nb, size) | + | return p.outputMaterialPC(material, num, nb, size, sentenceCase) |
elseif game == "NH" then | elseif game == "NH" then | ||
− | return p.outputMaterialNH(material, num, nb, size) | + | return p.outputMaterialNH(material, num, nb, size, sentenceCase) |
end | end | ||
end | end | ||
− | function p.outputMaterialNH(material, num, nb, size) | + | function firstToUpper(str) |
+ | return (str:gsub("^%l", string.upper)) | ||
+ | end | ||
+ | |||
+ | function p.outputMaterialNH(material, num, nb, size, sentenceCase) | ||
local function isEmpty(s) | local function isEmpty(s) | ||
return s == nil or s == '' | return s == nil or s == '' | ||
Line 47: | Line 54: | ||
end | end | ||
if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][3]) then | if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][3]) then | ||
− | + | if not isEmpty(sentenceCase) then | |
+ | print = print .. '[[' .. data.materialNH[material][3] .. '|' .. convertCase(data.materialNH[material][4]) .. ']]' | ||
+ | else | ||
+ | print = print .. '[[' .. data.materialNH[material][3] .. '|' .. data.materialNH[material][4] .. ']]' | ||
+ | end | ||
else | else | ||
if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][5]) then | if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][5]) then | ||
print = print .. data.materialNH[material][5] | print = print .. data.materialNH[material][5] | ||
else | else | ||
− | print = print .. '[[Item:' .. material .. ' (New Horizons)|' .. material .. ']]' | + | if not isEmpty(sentenceCase) then |
+ | print = print .. '[[Item:' .. material .. ' (New Horizons)|' .. convertCase(material) .. ']]' | ||
+ | else | ||
+ | print = print .. '[[Item:' .. material .. ' (New Horizons)|' .. material .. ']]' | ||
+ | end | ||
end | end | ||
end | end | ||
Line 60: | Line 75: | ||
else | else | ||
print = print .. '<br/>' | print = print .. '<br/>' | ||
+ | end | ||
+ | -- Check capitalization | ||
+ | if not isEmpty(checkCase.newHorizons[material:lower()]) and material == firstToUpper(checkCase.newHorizons[material:lower()]) then | ||
+ | print = print .. "" | ||
+ | elseif not isEmpty(checkCase.newHorizons[material:lower()]) and material == checkCase.newHorizons[material:lower()] then | ||
+ | print = print .. "" | ||
+ | else | ||
+ | print = print .. "[[Category:Pages with item names improperly capitalized]]" | ||
end | end | ||
return print | return print | ||
end | end | ||
− | function p.outputMaterialPC (material, num, nb, size) | + | function p.outputMaterialPC (material, num, nb, size, sentenceCase) |
local function isEmpty(s) | local function isEmpty(s) | ||
return s == nil or s == '' | return s == nil or s == '' |
Revision as of 12:13, November 28, 2022
Module documentation (view)
Usage
This Lua module outputs the list of materials for the {{Material}} and {{PC Material}} templates to account for preprocessor node count limits. Data is stored in a table, and can determine how certain variables are used.
To add additional materials, see Module:Materials/Data.
See also
local p = {}
local getArgs = require('Module:Arguments').getArgs
local convertCase = require('Module:SentenceCase').firstToUpper
checkCase = mw.loadData("Module:CapitalizationCheck/Data")
data = mw.loadData("Module:Materials/Data")
function p.main(frame)
local args = getArgs(frame)
local game = args.game or ''
local nb = args.nb or ''
local size = args.size or ''
local material = args[1] or args.material or ''
local num = args[2] or args.num or ''
local sentenceCase = args[3] or args.sentencecase or ''
if game == "PC" then
return p.outputMaterialPC(material, num, nb, size, sentenceCase)
elseif game == "NH" then
return p.outputMaterialNH(material, num, nb, size, sentenceCase)
end
end
function firstToUpper(str)
return (str:gsub("^%l", string.upper))
end
function p.outputMaterialNH(material, num, nb, size, sentenceCase)
local function isEmpty(s)
return s == nil or s == ''
end
local print = '<span style="display:inline-block; white-space:nowrap;">'
if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][1]) then
print = print .. '[[File:'.. data.materialNH[material][1]
if not isEmpty(size) then
print = print .. '|' .. size
else
print = print .. '|' .. data.materialNH[material][2]
end
print = print .. '|link=|alt=]] '
else
print = print .. '[[File:Leaf NH Icon cropped.png'
if not isEmpty(size) then
print = print .. '|' .. size
else
print = print .. '|15px'
end
print = print .. '|link=|alt=]] '
end
if not isEmpty(num) then
print = print .. num .. '× '
else
print = print .. ''
end
if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][3]) then
if not isEmpty(sentenceCase) then
print = print .. '[[' .. data.materialNH[material][3] .. '|' .. convertCase(data.materialNH[material][4]) .. ']]'
else
print = print .. '[[' .. data.materialNH[material][3] .. '|' .. data.materialNH[material][4] .. ']]'
end
else
if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][5]) then
print = print .. data.materialNH[material][5]
else
if not isEmpty(sentenceCase) then
print = print .. '[[Item:' .. material .. ' (New Horizons)|' .. convertCase(material) .. ']]'
else
print = print .. '[[Item:' .. material .. ' (New Horizons)|' .. material .. ']]'
end
end
end
print = print .. '</span>'
if not isEmpty(nb) then
print = print .. ''
else
print = print .. '<br/>'
end
-- Check capitalization
if not isEmpty(checkCase.newHorizons[material:lower()]) and material == firstToUpper(checkCase.newHorizons[material:lower()]) then
print = print .. ""
elseif not isEmpty(checkCase.newHorizons[material:lower()]) and material == checkCase.newHorizons[material:lower()] then
print = print .. ""
else
print = print .. "[[Category:Pages with item names improperly capitalized]]"
end
return print
end
function p.outputMaterialPC (material, num, nb, size, sentenceCase)
local function isEmpty(s)
return s == nil or s == ''
end
local print = '<span style="display:inline-block; white-space:nowrap;">'
if not isEmpty(data.materialPC[material]) and not isEmpty(data.materialPC[material][1]) then
print = print .. '[[File:'.. data.materialPC[material][1]
if not isEmpty(size) then
print = print .. '|' .. size
else
print = print .. '|' .. data.materialPC[material][2]
end
print = print .. '|link=|alt=]] '
end
if not isEmpty(num) then
print = print .. num .. '× '
else
print = print .. ''
end
if not isEmpty(data.materialPC[material]) and not isEmpty(data.materialPC[material][3]) then
print = print .. '[[' .. data.materialPC[material][3] .. '|' .. data.materialPC[material][4] .. ']]'
else
if not isEmpty(data.materialPC[material]) and not isEmpty(data.materialPC[material][5]) then
print = print .. data.materialPC[material][5]
else
print = print .. '[[Item:' .. material .. ' (Pocket Camp)|' .. material .. ']]'
end
end
print = print .. '</span>'
if not isEmpty(nb) then
print = print .. ''
else
print = print .. '<br/>'
end
return print
end
return p