Module:ManualDescriptions

From August Wiki
Revision as of 06:36, 16 August 2025 by Steve (talk | contribs)
Jump to navigation Jump to search

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:ManualDescriptions.csv')
    if not csvTitle or not csvTitle.exists then
        return string.format("''CSV data page [[Data:ManualDescriptions.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:ManualDescriptions.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:ManualDescriptions.csv]] to add one.''", itemName)
end

return p