| 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 |
|
| 8 | local _M = {} |
| 9 |
|
| 10 | string.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 |
| 34 | end |
| 35 |
|
| 36 |
|
| 37 | string.trim = function(str) |
| 38 | return str:match('^()%s*$') and '' or str:match('^%s*(.*%S)') |
| 39 | end |
| 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 ('"..command.."').") |
| 47 | output = process:read("*a") |
| 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 |
| 55 | end |
| 56 |
|
| 57 | _M.markdown = function(input) |
| 58 | local tmpfile = os.tmpname() |
| 59 | local fp = io.open(tmpfile, "w") |
| 60 | fp:write(input) |
| 61 | fp:close() |
| 62 | local stdout = _M.process("md2html --github "..tmpfile) |
| 63 | os.remove(tmpfile) |
| 64 | return stdout |
| 65 | end |
| 66 |
|
| 67 | _M.highlight = function(input, file_name) |
| 68 | local t = os.tmpname() |
| 69 | io.open(t,"w"):close() |
| 70 | os.remove(t) |
| 71 | local tmpfile = t..file_name |
| 72 | local fp = io.open(tmpfile, "w") |
| 73 | fp:write(input) |
| 74 | fp:close() |
| 75 | local stdout = _M.process("highlight --stdout -f --failsafe --inline-css "..tmpfile) |
| 76 | os.remove(tmpfile) |
| 77 | return stdout |
| 78 | end |
| 79 |
|
| 80 | _M.html_sanitize = function(str) |
| 81 | local sanitized = tostring(str):gsub("&", "&") |
| 82 | :gsub("<", "<") |
| 83 | :gsub(">", ">") |
| 84 | :gsub("\"", """) |
| 85 | :gsub("'", "'") |
| 86 | return sanitized |
| 87 | end |
| 88 |
|
| 89 | _M.iso8601 = function(iso8601) |
| 90 | iso8601 = iso8601 or "0000-00-00T00:00:00GMT-5:00" |
| 91 | local y,mo,d,h,mi,s = iso8601:match("(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)") |
| 92 | local luatime = os.time{year=y,month=mo,day=d,hour=h,min=mi,sec=s} |
| 93 | return os.date("%d %b %Y %H:%M", luatime) |
| 94 | end |
| 95 |
|
| 96 | _M.print_table = function(t, l) -- for debugging |
| 97 | l = l or 0 |
| 98 | local n = false |
| 99 | for i,v in pairs(t) do n = true break end |
| 100 | if n then |
| 101 | ngx.print("{\n") |
| 102 | for i,v in pairs(t) do |
| 103 | for i=0,l do ngx.print(" ") end |
| 104 | ngx.print("<span style='color:red'>",i,"</span>: ") |
| 105 | if type(v) ~= "table" then |
| 106 | if type(v) == "string" then |
| 107 | ngx.print("\"") |
| 108 | local s = v:gsub("&", "&"):gsub("<","<"):gsub(">",">"):gsub("\"","\\"") |
| 109 | ngx.print(s) |
| 110 | ngx.print("\"") |
| 111 | else |
| 112 | ngx.print(v) |
| 113 | end |
| 114 | else |
| 115 | _M.print_table(v,l+1) |
| 116 | end |
| 117 | ngx.print("\n") |
| 118 | end |
| 119 | for i=0,l-1 do ngx.print(" ") end |
| 120 | ngx.print("}") |
| 121 | else |
| 122 | ngx.print("{}") |
| 123 | end |
| 124 | end |
| 125 |
|
| 126 | return _M |
| 127 |
|