Index

resty-gitweb / c69d6c0

A git web interface for Lua/OpenResty (you're on it right now!)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
315 Dec 2020 18:527890389Update copyright noticesJosh Stockin132G

Blob @ resty-gitweb / utils / tabulate.lua

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