We are actively looking for staff to help us build the wiki. If you are interested please join our Discord server and apply.
Module:Items: Difference between revisions
Jump to navigation
Jump to search
(Start creating a module for generating item info (like tables for recipes)) |
(Remove the width style from the output table) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
function p.createItemTableRow(frame) | function p.createItemTableRow(frame) | ||
-- Get the | |||
local items = mw.text.split(frame.args | -- Get the items and header passed to the module by their names, defaulting items to an empty string and header to "Items" | ||
local header = frame.args | local items = mw.text.split(frame.args.items or "", ",") | ||
local header = frame.args.header or "Items" | |||
-- Store the number of items | -- Store the number of items | ||
| Line 12: | Line 13: | ||
-- Initialize the result string | -- Initialize the result string | ||
local result = "" | local result = "" | ||
-- if the item is wrapped in brackets, remove them | -- if the item is wrapped in brackets, remove them | ||
| Line 22: | Line 20: | ||
-- open a table to contain the items, spannin the header by the number of items | -- open a table to contain the items, spannin the header by the number of items | ||
result = result .. "{| class=\"wikitable | result = result .. "{| class=\"wikitable\"\n" | ||
result = result .. "! colspan=\"" .. numItems .. "\" | " .. header .. "\n" | result = result .. "! colspan=\"" .. numItems .. "\" | " .. header .. "\n" | ||
result = result .. "|-\n" | result = result .. "|-\n" | ||
| Line 31: | Line 29: | ||
-- Replace the NAME placeholder with the item name | -- Replace the NAME placeholder with the item name | ||
local item_template = item_template: | local item_template = "{{Item | name = " .. item .. " }}" | ||
-- Use frame:preprocess to ensure the template call is correctly parsed | |||
item_template = mw.text.trim(frame:preprocess(item_template)) | |||
-- Append the item template to the result string | -- Append the item template to the result string | ||
Latest revision as of 02:04, 14 July 2024
Documentation for this module may be created at Module:Items/doc
-- Module:Items
local p = {}
function p.createItemTableRow(frame)
-- Get the items and header passed to the module by their names, defaulting items to an empty string and header to "Items"
local items = mw.text.split(frame.args.items or "", ",")
local header = frame.args.header or "Items"
-- Store the number of items
local numItems = #items
-- Initialize the result string
local result = ""
-- if the item is wrapped in brackets, remove them
for i, item in ipairs(items) do
items[i] = item:gsub("%[", ""):gsub("%]", "")
end
-- open a table to contain the items, spannin the header by the number of items
result = result .. "{| class=\"wikitable\"\n"
result = result .. "! colspan=\"" .. numItems .. "\" | " .. header .. "\n"
result = result .. "|-\n"
-- Iterate over the items and create an item template for each
for i, item in ipairs(items) do
item = mw.text.trim(item)
-- Replace the NAME placeholder with the item name
local item_template = "{{Item | name = " .. item .. " }}"
-- Use frame:preprocess to ensure the template call is correctly parsed
item_template = mw.text.trim(frame:preprocess(item_template))
-- Append the item template to the result string
result = result .. "| " .. item_template .. "\n"
end
-- close the table
result = result .. "|}\n"
return result
end
return p