FANDOM


--Retrieves csv data from a page & generates an html table
-- Syntax <nowiki>{{#invoke:csv|main|csv=data}}</nowiki>
 
-- Modified by [[User:The JoTS]] from dev wiki
-- Takes csv data directly, instead from external page.
-- Style for individual cells are possible to appending styling in brackets
-- Style for entire table may be provided through "style" param
 
-- <nowiki>
 
local p = {}
 
function p.main(frame)
    return p.csvToHtmlTable(frame.args["csv"], frame.args["style"])
end
 
function p.csvToHtmlTable(csv, tStyle)
    local tLuaTable = p.convertToLua(csv)
    if tLuaTable then
        local hCsvTable = p.generateHtml(tLuaTable, tStyle)
        return hCsvTable
    end
end
 
-- //Converts a csv data to lua table
function p.convertToLua(csv)
    if (csv) then
        local arrColumns = {}
        local sTable = {}
 
        oData = csv .."\n\n"
        oData = string.gsub(oData,"((\n)%s*)","<line-end>")
 
        for s in string.gmatch(oData, "(.-)%<line%-end%>") do
        	sTable = mw.text.split( s, ',' )
        	table.insert(arrColumns,sTable)
        end
 
        return arrColumns 
    end
end
 
-- //Converts a csv page to html table
function p.generateHtml(arrColumns, tStyle)
    local hTable = mw.html.create("table")
    hTable:addClass("wikitable")
    hTable:attr("style", tStyle)
 
    for i,v in ipairs(arrColumns) do 
        local sTag = i > 1 and "td" or "th"
        hTable:tag( 'tr' ) :done()
 
        for sKey,sData in ipairs(arrColumns[i]) do
            local cell = hTable:tag( sTag )
 
            cell:wikitext(
                (sData:gsub("//(.-)//$", function(style)
                    cell:attr("style", style)
                    return ''
                end))
            ):done()
        end
    end
 
    return hTable
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.