Index

resty-gitweb / 7890389

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 / utils.lua

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