יחידה:Csv
ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:Csv/תיעוד
local function csv_to_table( csv )
local tab = {}
for row in mw.text.gsplit( csv, '\n') do
table.insert(tab, mw.text.split(row, ','))
end
return tab
end
local function table_to_html( tab, th_first, table_attrib, tr_attrib, th_attrib, td_attrib, content_only )
local htmlrows = {}
for ind, line in ipairs(tab) do
local titlerow = th_first and ind == 1
local opener = titlerow and '<th%s%s>' or '<td%s%s>'
local closer = titlerow and '</th>' or '</td>'
local row_attrib = titlerow and (th_attrib or '') or (td_attrib or '')
mw.log(ind, ' td_attrib', td_attrib)
opener = mw.ustring.format(opener, row_attrib and ' ' or '', row_attrib)
local htmlrow = opener .. table.concat(line, closer .. opener) .. closer
table.insert(htmlrows, mw.ustring.format('<tr%s%s>%s</tr>', tr_attrib and ' ' or '', tr_attrib or '', htmlrow))
end
local content = table.concat(htmlrows)
return content_only and content
or mw.ustring.format('<table%s%s>%s</table>', table_attrib and ' ' or '', table_attrib or '', content)
end
local function csv_to_wikitable( frame )
local args = frame.args
local csv = args.csv
local tab = csv_to_table(csv)
local table_attrib = args['table-attrib'] or 'class="wikitable sortable"'
return table_to_html(tab, not args['no-title'], table_attrib, args['tr-attrib'], args['th-attrib'], args['td-attrib'], args['content-only'])
end
return {
['wikitable'] = csv_to_wikitable,
}