| 1 | -- utils.lua | 
| 2 | -- basic utilities for git status site implementation | 
| 3 |  | 
| 4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin | 
| 5 | -- <https://github.com/JoshuaS3/joshstock.in> | 
| 6 | -- <https://joshstock.in> | 
| 7 |  | 
| 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 |  |