Module:ManualDescriptions: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
-- Module:ManualDescriptions | -- Module:ManualDescriptions | ||
local p = {} | local p = {} | ||
-- | -- Function to parse CSV content | ||
local | local function parseCSV(csvText) | ||
[" | local data = {} | ||
local lines = {} | |||
-- Split into lines | |||
for line in csvText:gmatch("[^\r\n]+") do | |||
table.insert(lines, line) | |||
end | |||
if #lines < 2 then | |||
return data -- No data to parse | |||
end | |||
-- Skip header line, parse data lines | |||
for i = 2, #lines do | |||
local line = lines[i] | |||
local fields = {} | |||
-- Simple CSV parsing (handles quoted fields with commas) | |||
local fieldstart = 1 | |||
repeat | |||
local nexti = nil | |||
if string.sub(line, fieldstart, fieldstart) == '"' then | |||
-- Quoted field | |||
local a, b = string.find(line, '^"(.-)"', fieldstart) | |||
if a then | |||
table.insert(fields, string.sub(line, a+1, b-1)) | |||
nexti = b + 2 -- Skip quote and comma | |||
end | |||
else | |||
-- Unquoted field | |||
local a, b = string.find(line, '^([^,]*)', fieldstart) | |||
if a then | |||
table.insert(fields, string.sub(line, a, b)) | |||
nexti = b + 2 -- Skip comma | |||
end | |||
end | |||
fieldstart = nexti | |||
until fieldstart == nil or fieldstart > #line | |||
if #fields >= 2 then | |||
data[fields[1]] = fields[2] | |||
end | |||
end | |||
return data | |||
end | |||
function p.getDescription(frame) | function p.getDescription(frame) | ||
local itemName = frame.args[1] or "" | local itemName = frame.args[1] or "" | ||
-- Get the CSV page content | |||
local csvTitle = mw.title.new('Data:ItemDescriptions.csv') | |||
if not csvTitle or not csvTitle.exists then | |||
return string.format("''CSV data page [[Data:ItemDescriptions.csv]] not found. Please create it.''") | |||
end | |||
local csvContent = csvTitle:getContent() | |||
if not csvContent then | |||
return string.format("''Could not read CSV data. Please check [[Data:ItemDescriptions.csv]].''") | |||
end | |||
-- Parse the CSV | |||
local descriptions = parseCSV(csvContent) | |||
local description = descriptions[itemName] | local description = descriptions[itemName] | ||
if description then | if description then | ||
return description | return description | ||
end | end | ||
return string.format("''No description available for %s. Please edit [[ | return string.format("''No description available for %s. Please edit [[Data:ItemDescriptions.csv]] to add one.''", itemName) | ||
end | end | ||
return p | return p |
Revision as of 06:35, 16 August 2025
Documentation for this module may be created at Module:ManualDescriptions/doc
-- Module:ManualDescriptions
local p = {}
-- Function to parse CSV content
local function parseCSV(csvText)
local data = {}
local lines = {}
-- Split into lines
for line in csvText:gmatch("[^\r\n]+") do
table.insert(lines, line)
end
if #lines < 2 then
return data -- No data to parse
end
-- Skip header line, parse data lines
for i = 2, #lines do
local line = lines[i]
local fields = {}
-- Simple CSV parsing (handles quoted fields with commas)
local fieldstart = 1
repeat
local nexti = nil
if string.sub(line, fieldstart, fieldstart) == '"' then
-- Quoted field
local a, b = string.find(line, '^"(.-)"', fieldstart)
if a then
table.insert(fields, string.sub(line, a+1, b-1))
nexti = b + 2 -- Skip quote and comma
end
else
-- Unquoted field
local a, b = string.find(line, '^([^,]*)', fieldstart)
if a then
table.insert(fields, string.sub(line, a, b))
nexti = b + 2 -- Skip comma
end
end
fieldstart = nexti
until fieldstart == nil or fieldstart > #line
if #fields >= 2 then
data[fields[1]] = fields[2]
end
end
return data
end
function p.getDescription(frame)
local itemName = frame.args[1] or ""
-- Get the CSV page content
local csvTitle = mw.title.new('Data:ItemDescriptions.csv')
if not csvTitle or not csvTitle.exists then
return string.format("''CSV data page [[Data:ItemDescriptions.csv]] not found. Please create it.''")
end
local csvContent = csvTitle:getContent()
if not csvContent then
return string.format("''Could not read CSV data. Please check [[Data:ItemDescriptions.csv]].''")
end
-- Parse the CSV
local descriptions = parseCSV(csvContent)
local description = descriptions[itemName]
if description then
return description
end
return string.format("''No description available for %s. Please edit [[Data:ItemDescriptions.csv]] to add one.''", itemName)
end
return p