Index

resty-gitweb / adbeb10

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
113 Dec 2020 16:16e49d46eInitial commit (imported, read description)Josh Stockin11460G

Blob @ resty-gitweb / utils / utils.lua

text/plain4116 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.highlight = function(input, file_name)
77 local output
78 local status, err = pcall(function()
79 local t = os.tmpname()
80 io.open(t,"w"):close()
81 os.remove(t)
82 local tmpfile = t..file_name
83 local fp = io.open(tmpfile, "w")
84 fp:write(input)
85 fp:close()
86 local process = io.popen("highlight --stdout -f --failsafe --inline-css "..tmpfile, "r")
87 assert(process, "Error opening process")
88 output = process:read("*all")
89 process:close()
90 os.remove(tmpfile)
91 end)
92 if status then
93 return output
94 else
95 return string.format("Error in call: %s", err or command)
96 end
97end
98
99_M.html_sanitize = function(str)
100 return tostring(
101 tostring(str):gsub("&", "&amp;")
102 :gsub("<", "&lt;")
103 :gsub(">", "&gt;")
104 :gsub("\"", "&quot;")
105 :gsub("'", "&#039;")
106 )
107end
108
109_M.iso8601 = function(iso8601)
110 iso8601 = iso8601 or "0000-00-00T00:00:00GMT-5:00"
111 local y,mo,d,h,mi,s = iso8601:match("(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)")
112 local luatime = os.time{year=y,month=mo,day=d,hour=h,min=mi,sec=s}
113 return os.date("%d %b %Y %H:%M", luatime)
114end
115
116_M.print_table = function(t, l) -- for debugging
117 l = l or 0
118 local n = false
119 for i,v in pairs(t) do n = true break end
120 if n then
121 ngx.print("{\n")
122 for i,v in pairs(t) do
123 for i=0,l do ngx.print(" ") end
124 ngx.print("<span style='color:red'>",i,"</span>: ")
125 if type(v) ~= "table" then
126 if type(v) == "string" then
127 ngx.print("\"")
128 local s = v:gsub("&", "&amp;"):gsub("<","&lt;"):gsub(">","&gt;"):gsub("\"","\\&quot;")
129 ngx.print(s)
130 ngx.print("\"")
131 else
132 ngx.print(v)
133 end
134 else
135 _M.print_table(v,l+1)
136 end
137 ngx.print("\n")
138 end
139 for i=0,l-1 do ngx.print(" ") end
140 ngx.print("}")
141 else
142 ngx.print("{}")
143 end
144end
145
146return _M
147