hugo.soucy.cc

The personnal website source code of Hugo Soucy. Creates from an homebrewed static website generator (Ferron).
Log | Files | Refs

commit 27b05fc859e55800ddd4397f1d4c0333d7020b67
parent dda4a094dc9ba30d4ab006815ab581fbdfa22f05
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Sun, 26 Apr 2020 21:51:53 -0400

Add a rewitemap plugin

Diffstat:
Aplugins/rewritemap/init.lua | 38++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+), 0 deletions(-)

diff --git a/plugins/rewritemap/init.lua b/plugins/rewritemap/init.lua @@ -0,0 +1,38 @@ +-- rewitemap.init.lua +local rewitemap = {} +local rewitemapmeta = { + __call = function(self, key, vars) + print(key) + end +} + +function rewitemap.init(sitedata) + local json = require 'dkjson' + local fl = require 'ferron.utilities.file-utils' + + local links_db = sitedata.data .. '/shortlinks.json' + local links_map = sitedata.html .. '/shortlinksmap' + + if fl.isFile(links_db) then + local links = assert(json.decode(fl.pullfilecontent(links_db)), "Can't load shortlinks.json, make sure it exists") + local map + + if links then + -- Create an empty map file + fl.pushfilecontent(links_map, '') + + -- Open it + map, map_err = io.open(links_map, 'a') + + for k, v in pairs(links) do + -- Redirection for the OpenBSD HTTPD web server + map:write('location match ' .. '"^/' .. tostring(v) .. '$" { block return 301 "https://$SERVER_NAME' .. tostring(k), '"}\n') + end + + -- Close it when it's done + map:close() + end + end +end + +return setmetatable(rewitemap, rewitemapmeta)