Module:Csv
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Csv/doc
--Retrieves csv data from a page & generates an html table -- Syntax <nowiki>{{#invoke:csv|main|page=pagename}}</nowiki> local p = {} local getArgs = require("Module:Arguments").getArgs function p.main(frame) local sPageName = getArgs(frame).page if sPageName then return p.csvToHtmlTable(sPageName) end end function p.csvToHtmlTable(sPage) local tLuaTable = p.convertToLua(sPage) if tLuaTable then return p.generateHtml(tLuaTable) end end -- //Converts a csv page to lua table function p.convertToLua(sPage) if sPage then local oData = mw.title.new(sPage) local arrColumns = {} 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 arrColumns[#arrColumns + 1] = mw.text.split(s, ",") 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") for i, v in ipairs(arrColumns) do local sTag = i == 1 and "th" or "td" hTable:tag("tr"):done() for _, sData in ipairs(v) do hTable:tag(sTag):wikitext(sData):done() end end return hTable end return p