Index

joshstock.in / 4deb1aa

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9512 Dec 2020 00:224deb1aaUpdate lua-gitwebJosh Stockin1392G

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

text/plain3387 bytesdownload raw
1-- utils.lua
2-- Basic utilities/resources for git HTTP site implementation
3
4-- Copyright (c) 2020 Joshua 'joshuas3' Stockin
5-- <https://joshstock.in>
6
7local _M = {}
8
9string.split = function(str, delimiter)
10 local t = {}
11 if not str or str == "" then
12 return t
13 end
14 if delimiter == "" then -- string to table
15 string.gsub(str, ".", function(c) table.insert(t, c) end)
16 return t
17 end
18 delimiter = delimiter or "%s"
19 local str_len = string.len(str)
20 local ptr = 1
21 while true do
22 local sub = string.sub(str, ptr, str_len)
23 local pre, after = string.find(sub, delimiter)
24 if pre then -- delimiter found
25 table.insert(t, string.sub(str, ptr, ptr + (pre - 2)))
26 ptr = ptr + after
27 else -- delimiter not found
28 table.insert(t, string.sub(str, ptr, str_len))
29 break
30 end
31 end
32 return t
33end
34
35
36string.trim = function(str)
37 return str:match('^()%s*$') and '' or str:match('^%s*(.*%S)')
38end
39
40
41_M.process = function(command)
42 local output
43 local status, err = pcall(function()
44 local process = io.popen(command, "r")
45 assert(process, "Error opening process")
46 output = process:read("*all")
47 process:close()
48 end)
49 if status then
50 return output
51 else
52 return string.format("Error in call: %s", err or command)
53 end
54end
55
56_M.markdown = function(input)
57 local output
58 local status, err = pcall(function()
59 local tmpfile = os.tmpname()
60 local fp = io.open(tmpfile, "w")
61 fp:write(input)
62 fp:close()
63 local process = io.popen("md2html --github "..tmpfile, "r")
64 assert(process, "Error opening process")
65 output = process:read("*all")
66 process:close()
67 os.remove(tmpfile)
68 end)
69 if status then
70 return output
71 else
72 return string.format("Error in call: %s", err or command)
73 end
74end
75
76_M.html_sanitize = function(str)
77 return tostring(str):gsub("<", "&lt;")
78 :gsub(">", "&gt;")
79 :gsub("&", "&amp;")
80 :gsub("\"", "&quot;")
81 :gsub("'", "&#039;")
82end
83
84_M.iso8601 = function(iso8601)
85 iso8601 = iso8601 or "0000-00-00T00:00:00GMT-5:00"
86 local y,mo,d,h,mi,s = iso8601:match("(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)")
87 local luatime = os.time{year=y,month=mo,day=d,hour=h,min=mi,sec=s}
88 return os.date("%d %b %Y %H:%M", luatime)
89end
90
91_M.print_table = function(t, l) -- for debugging
92 l = l or 0
93 local n = false
94 for i,v in pairs(t) do n = true break end
95 if n then
96 ngx.print("{\n")
97 for i,v in pairs(t) do
98 for i=0,l do ngx.print(" ") end
99 ngx.print("<span style='color:red'>",i,"</span>: ")
100 if type(v) ~= "table" then
101 if type(v) == "string" then
102 ngx.print("\"")
103 local s = v:gsub("&", "&amp;"):gsub("<","&lt;"):gsub(">","&gt;"):gsub("\"","\\&quot;")
104 ngx.print(s)
105 ngx.print("\"")
106 else
107 ngx.print(v)
108 end
109 else
110 _M.print_table(v,l+1)
111 end
112 ngx.print("\n")
113 end
114 for i=0,l-1 do ngx.print(" ") end
115 ngx.print("}")
116 else
117 ngx.print("{}")
118 end
119end
120
121return _M
122