Module:Csv

--Retrieves csv data from a page & generates an html table -- Syntax

local p ={} local getArgs = require("Module: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*)","")

for s in string.gmatch(oData, "(.-)%") 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