FANDOM


--Retrieves csv data from a page & generates an html table
-- Syntax <nowiki>{{#invoke:csv|main|page=pagename}}</nowiki>
 
local p ={}
local getArgs = require("Dev:Arguments").getArgs
 
function p.main(frame)
    local args = getArgs(frame)
    local sPageName = args["page"]
 
    if sPageName then 
        return p.csvToHtmlTable(sPageName)
    end
end
 
function p.csvToHtmlTable(sPage)
    local tLuaTable = p.convertToLua(sPage)
    if tLuaTable then
        local hCsvTable =p.generateHtml(tLuaTable)
        return hCsvTable
    end
end
 
-- //Converts a csv page to lua table
function p.convertToLua(sPage)
    if (sPage) then
        local oData = mw.title.new( sPage)
        local arrColumns={}
        local sTable ={}
 
        if (oData and oData:getContent()) then
            oData = oData:getContent() .."\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
        end
        return arrColumns 
    end
end
 
-- //Converts a csv page to html table
function p.generateHtml(arrColumns)
    local hTable = mw.html.create("table")
    hTable:addClass("wikitable")
    local sTag =""
    for i,v in ipairs(arrColumns) do 
        sTag = "td"
        hTable:tag( 'tr' ) :done()
        for sKey,sData in ipairs(arrColumns[i]) do
            if( i==1) then
                sTag = "th"
            end
            hTable:tag( sTag )
                :wikitext(sData )
                :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.