Module:Fnielsen
Appearance
Documentation for this module may be created at Module:Fnielsen/doc
-- Tutorial from Tim Starling
-- http://snippets.luacode.org/?p=snippets/Split_a_string_into_a_list_5
function split(s, delimiter)
local i1 = 1
local thelist = {}
while true do
local i2, i3 = s:find(delimiter, i1)
if not i2 then
local last = s:sub(i1)
if last ~= '' then table.insert(thelist, last) end
if #thelist == 1 and thelist[1] == '' then
return {}
else
return thelist
end
end
table.insert(thelist, s:sub(i1, i2-1))
i1 = i3+1
end
end
function table2string(t)
local s = ''
local element
for i,v in ipairs(t) do
s = s .. '"' .. v .. '", '
end
return s
end
function table2htmltablerow(t)
-- @t table with row elements
-- returns string formatted for a HTML wikitext row
local s = '<tr>'
local element
for i,v in ipairs(t) do
s = s .. '<td style="font-size:75%; border: 1px #000 solid; border-collapse: collapse; ">' .. v
end
return s
end
function array2htmltable(a)
-- @a array table of tables
-- returns string formatted for a HTML wikitext
s = '<table class=sortable border=2 style="font-size:75%; background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse;">'
local first = true
for i, row in ipairs(a) do
s = s .. '<tr>'
if first then
for j, element in ipairs(row) do
s = s .. '<th style="background: #f2f2f2; text-align: left; border: 1px #000 solid; border-collapse: collapse;">' .. element
end
first = false
else
for j, element in ipairs(row) do
s = s .. '<td style="font-size:75%; border: 1px #000 solid; border-collapse: collapse; ">' .. element
end
end
end
s = s .. '</table>'
return s
end
-- http://www.lua.org/pil/20.4.html
function fromCSV(s, delimiter)
local t = {}
local lines = {}
local array = {}
lines = split(s, '\n')
for i,line in ipairs(lines) do
t = split(line, delimiter)
table.insert(array, t)
end
return array
end
-- Interface --
local p = {}
function p.hello(frame)
return 'Hello ' .. frame.args[1]
end
function p.csvdata(frame)
local array = fromCSV(frame:preprocess('{{:Obsessive-compulsive disorder Neuroimaging Database - Amygdala.csv}}'), '\t')
s = 'The table has ' .. #array .. ' rows'
s = s .. array2htmltable(array)
return s
end
return p