Actions

Module

Module:DataTableParserV2

From Dune Awakening DB

Revision as of 23:15, 25 March 2025 by Operator (talk | contribs) (Created page with "-- Module:DataTableParser -- Handles display and formatting of building data for Templates -- Updated to use External Data instead of parsing a wiki table page local p = {} -- Function to generate icon file reference for a resource local function getResourceIcon(resourceName) if not resourceName or resourceName == "" then return "" end local fileName = resourceName:gsub("%s+", "_") .. "_-_Icon.png" local fileTitle = mw.title.new("File:" .. fileN...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:DataTableParserV2/doc

-- Module:DataTableParser
-- Handles display and formatting of building data for Templates
-- Updated to use External Data instead of parsing a wiki table page

local p = {}

-- Function to generate icon file reference for a resource
local function getResourceIcon(resourceName)
    if not resourceName or resourceName == "" then
        return ""
    end
    local fileName = resourceName:gsub("%s+", "_") .. "_-_Icon.png"
    local fileTitle = mw.title.new("File:" .. fileName)
    if fileTitle and fileTitle.exists then
        return "[[File:" .. fileName .. "|20px]]"
    else
        return ""
    end
end

-- Function to add icons to resource links in text
function p.iconize(frame)
    local text = frame.args[1] or ""
    text = text:gsub("%[%[([^%]]+)%]%]", function(resourceName)
        local icon = getResourceIcon(resourceName)
        if icon ~= "" then
            return icon .. " [[" .. resourceName .. "]]"
        else
            return "[[" .. resourceName .. "]]"
        end
    end)
    return text
end

----------------------------------------------------------------------
-- Updated: Helper function to load building data from ExternalData
----------------------------------------------------------------------
local function loadBuildingWikiTable(frame)
    -- Build the externaldata query string.
    local edQuery = [[
{{#get_external_data: source=externaldb
 |from=data_buildings
 |data=Name=name,Description=description,JourneyRequirement=journey_requirement,Health=health,PowerCost=power_cost,GeneratesPower=generates_power,StorageSlots=storage_slots,StorageVolume=storage_capacity,RecipeToBuild=recipe_to_build,PlacedWith=placed_with,ImageFile=image_file,AdditionalNotes=additional_notes
 |cache=yes
}}
{| class="wikitable"
! Name
! Description
! JourneyRequirement
! Health
! PowerCost
! GeneratesPower
! StorageSlots
! StorageVolume
! RecipeToBuild
! PlacedWith
! ImageFile
! AdditionalNotes
{{#for_external_table:|
{{!}}-
{{!}} {{{Name}}}
{{!}} {{{Description}}}
{{!}} {{{JourneyRequirement}}}
{{!}} {{{Health}}}
{{!}} {{{PowerCost}}}
{{!}} {{{GeneratesPower}}}
{{!}} {{{StorageSlots}}}
{{!}} {{{StorageVolume}}}
{{!}} {{{RecipeToBuild}}}
{{!}} {{{PlacedWith}}}
{{!}} {{{ImageFile}}}
{{!}} {{{AdditionalNotes}}}
}}
|}
]]
    local content = frame:preprocess(edQuery)
    
    if not content or content == "" then
        return nil, "Error: No building data returned from external source!"
    end
    
    local lines = mw.text.split(content, "\n")
    local filtered = {}
    for _, line in ipairs(lines) do
        line = mw.text.trim(line)
        if line ~= "" and not line:match("^__") and not line:match("<noindex>") and not line:match("</noindex>") then
            table.insert(filtered, line)
        end
    end
    if #filtered < 2 then
        return nil, "Error: No valid building data found!"
    end

    local inTable = false
    local headers = {}  -- table headers
    local colMap = {}   -- header name -> column index
    local data = {}     -- array of row arrays
    local currentRow = {}
    local readingHeader = true

    for _, line in ipairs(filtered) do
        if line:match("^{|") then
            inTable = true
        elseif line:match("^|%-") then
            if not readingHeader and #currentRow > 0 then
                table.insert(data, currentRow)
            end
            currentRow = {}
            readingHeader = false
        elseif line:match("^!") then
            local header = line:gsub("^!+%s*", "")
            header = mw.text.trim(header)
            table.insert(headers, header)
        elseif line:match("^|%+") then
            -- Skip caption
        elseif line:match("^|}") then
            if not readingHeader and #currentRow > 0 then
                table.insert(data, currentRow)
            end
            inTable = false
        elseif inTable and line:match("^|[^%-+}]") then
            local cell = line:gsub("^|+%s*", "")
            cell = mw.text.trim(cell)
            if not readingHeader then
                table.insert(currentRow, cell)
            end
        end
    end

    for i, h in ipairs(headers) do
        colMap[mw.text.trim(h)] = i
    end

    return { rows = data, colMap = colMap }, nil
end

----------------------------------------------------------------------
-- Updated: Helper function to load refining data from ExternalData
----------------------------------------------------------------------
local function loadRefiningWikiTable(frame)
    local edQuery = [[
{{#get_external_data: source=externaldb
 |from=data_refining_recipes
 |data=Refiner=Refiner,Output=Output,Ingredients=Ingredients,Time=Time,Recipe=Recipe
 |cache=yes
}}
{| class="wikitable"
! Refiner
! Output
! Ingredients
! Time
! Recipe
{{#for_external_table:|
{{!}}-
{{!}} {{{Refiner}}}
{{!}} {{{Output}}}
{{!}} {{{Ingredients}}}
{{!}} {{{Time}}}
{{!}} {{{Recipe}}}
}}
|}
]]
    local content = frame:preprocess(edQuery)
    if not content or content == "" then
        return nil, "Error: No refining data returned from external source!"
    end

    local lines = mw.text.split(content, "\n")
    local filtered = {}
    for _, line in ipairs(lines) do
        line = mw.text.trim(line)
        if line ~= "" and not line:match("^__") and not line:match("<noindex>") and not line:match("</noindex>") then
            table.insert(filtered, line)
        end
    end
    if #filtered < 2 then
        return nil, "Error: No valid refining data found!"
    end

    local inTable = false
    local headers = {}
    local colMap = {}
    local data = {}
    local currentRow = {}
    local readingHeader = true

    for _, line in ipairs(filtered) do
        if line:match("^{|") then
            inTable = true
        elseif line:match("^|%-") then
            if not readingHeader and #currentRow > 0 then
                table.insert(data, currentRow)
            end
            currentRow = {}
            readingHeader = false
        elseif line:match("^!") then
            local header = line:gsub("^!+%s*", "")
            header = mw.text.trim(header)
            table.insert(headers, header)
        elseif line:match("^|%+") then
            -- Skip caption
        elseif line:match("^|}") then
            if not readingHeader and #currentRow > 0 then
                table.insert(data, currentRow)
            end
            inTable = false
        elseif inTable and line:match("^|[^%-+}]") then
            local cell = line:gsub("^|+%s*", "")
            cell = mw.text.trim(cell)
            if not readingHeader then
                table.insert(currentRow, cell)
            end
        end
    end

    for i, h in ipairs(headers) do
        colMap[mw.text.trim(h)] = i
    end

    if #data == 0 then
        return nil, "Error: No data rows found in refining data!"
    end

    return { rows = data, colMap = colMap }, nil
end

-- [Rest of your module functions remain unchanged]
-- For example, p.iconize, p.formatComponent, p.getBuildingData, etc.
-- They will use the table data returned from loadBuildingWikiTable() or loadRefiningWikiTable(frame)

return p