Module:Csv: Difference between revisions
Jump to navigation
Jump to search
Mediawiki>KockaAdmiralac m (Undo - Module:Arguments provides argument inheritance, if this was used in a way that expects the arguments to be passed from a template instead of directly in the invocation it'll break) |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 3: | Line 3: | ||
local p = {} | local p = {} | ||
local getArgs = require(" | local getArgs = require("Module:Arguments").getArgs | ||
function p.main(frame) | function p.main(frame) |
Latest revision as of 05:28, 2 May 2021
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