Index

joshstock.in / 81b4469

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9813 Dec 2020 01:05da32c63Add git blobsJosh Stockin11915G

Blob @ joshstock.in / lua-gitweb / utils / tabulate.lua

text/plain1644 bytesdownload raw
1-- tabulate.lua
2-- Formats input data with HTML table elements
3
4-- Copyright (c) 2020 Joshua 'joshuas3' Stockin
5-- <https://joshstock.in>
6
7local html_format = function(str, element, class)
8 str = str or ""
9 element = element or "td"
10 local open = class == nil and "<"..element..">" or "<"..element.." class=\""..class.."\">"
11 local close = "</"..element..">"
12 return open..str..close
13end
14
15local _M = function(table_data) -- table to HTML
16 local table_string
17 local classes = {}
18
19 -- table HTML class?
20 if table_data.class then
21 table_string = "<table class=\""..table_data.class.."\">\n"
22 else
23 table_string = "<table>\n"
24 end
25
26 -- format th row
27 if #table_data.headers > 0 then
28 local header_row = "<tr>"
29 for _, header in pairs(table_data.headers) do
30 table.insert(classes, header[1]) -- populate classes data
31 local th = html_format(header[2], "th", header[1])
32 header_row = header_row..th
33 end
34 header_row = header_row.."</tr>\n"
35 table_string = table_string..header_row
36 end
37
38 -- rows with td
39 if #table_data.rows > 0 then
40 for _, row in pairs(table_data.rows) do
41 local row_html = "<tr>"
42 for i, data in pairs(row) do
43 local class = classes[i]
44 local td = html_format(data, "td", class)
45 row_html = row_html..td
46 end
47 local row_html = row_html.."</tr>\n"
48 table_string = table_string..row_html
49 end
50 end
51
52 table_string = table_string.."</table>\n"
53
54 return table_string
55end
56
57return _M
58