ferron

Ferron is a fast and simple static website generator made with Lua.
Log | Files | Refs | Submodules | README | LICENSE

commit aca6cc3f8665365755ddec0ef85c73536b854a9f
parent 8ccbe25a19de6b0cc62e37ffc7e698d1d37ce363
Author: Hugo Soucy <hugo.soucy@savoirfairelinux.com>
Date:   Mon,  5 Feb 2018 16:33:51 -0500

Add the rewritemap funciton

Diffstat:
Mferron.lua | 3++-
Mferron/link.lua | 57++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/ferron.lua b/ferron.lua @@ -44,7 +44,8 @@ Ferron.build = { page.makepage, list.makelist, feed.makefeed, - link.makeshortlink, + link.makeshorts, + link.makerewritemap, static.move } diff --git a/ferron/link.lua b/ferron/link.lua @@ -1,20 +1,67 @@ -- +local json = require "dkjson" local path = require "path" local fileutils = require "ferron.utilities.file-utils" local tableutils = require "ferron.utilities.table-utils" local templateutils = require "ferron.utilities.template-utils" local link = {} -function link.makeshortlink() +function link.makeshorts() local links = {} + local links_db = Ferron.site.path .. Ferron.site.siteconfig.paths.data .. "/shortlinks.json" - for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do - table.insert(links, v) + -- If `links_db` dosen't exists create it + if path.isfile(links_db) == false then + local counter = 0 - print(inspect(k.rellink)) + fileutils.pushfilecontent(links_db, "") + + for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do + if v.rellink ~= "" then + counter = counter + 1 + + links[v.rellink] = fileutils.shorturlencode(counter) + end + end + else + links = json.decode(fileutils.pullfilecontent(links_db)) + + for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do + if v.rellink ~= "" and links[v.rellink] == nil then + links[v.rellink] = fileutils.shorturlencode(tableutils.length(links) + 1) + end + end + end + + fileutils.pushfilecontent(links_db, json.encode(links, {indent = true})) + + print("========== DONE") + -- print(inspect(links), tableutils.length(links)) + --print(tableutils.length(links)) +end + +function link.makerewritemap() + local links_db = Ferron.site.path .. Ferron.site.siteconfig.paths.data .. "/shortlinks.json" + local links_map = Ferron.site.path .. Ferron.site.siteconfig.paths.html .. "/shortlinks.map.txt" + + if path.isfile(links_db) then + links = json.decode(fileutils.pullfilecontent(links_db)) + + -- Create an empty map file + fileutils.pushfilecontent(links_map, "") + + -- Open it + map, map_err = io.open(links_map, "a") + + for k, v in pairs(links) do + map:write(v .. " " .. k, "\n") + end + + -- Close it when it's done + map:close() end - print("==========") + return end return link