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 |
|
7 | local _M = {} |
8 |
|
9 | string.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 |
33 | end |
34 |
|
35 |
|
36 | string.trim = function(str) |
37 | return str:match('^()%s*$') and '' or str:match('^%s*(.*%S)') |
38 | end |
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 |
54 | end |
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 |
74 | end |
75 |
|
76 | _M.html_sanitize = function(str) |
77 | return tostring(str):gsub("<", "<") |
78 | :gsub(">", ">") |
79 | :gsub("&", "&") |
80 | :gsub("\"", """) |
81 | :gsub("'", "'") |
82 | end |
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) |
89 | end |
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("&", "&"):gsub("<","<"):gsub(">",">"):gsub("\"","\\"") |
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 |
119 | end |
120 |
|
121 | return _M |
122 |
|