ferron

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

commit 766aa524671d55054e160b6de2b3abf540c0347e
parent 9a7f9bddb93f7bd2a6031a371d535385c25b5b86
Author: Hugo Soucy <hsoucy@kronostechnologies.com>
Date:   Wed, 30 Jan 2019 08:55:33 -0500

Clean site tableutils functions

Diffstat:
Mferron.lua | 6+++---
Mferron/site.lua | 101++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mferron/utilities/table-utils.lua | 42+++++++++++++++++++++---------------------
3 files changed, 75 insertions(+), 74 deletions(-)

diff --git a/ferron.lua b/ferron.lua @@ -26,8 +26,8 @@ local exec = require "ferron.utilities.exec" Ferron.init = { site.setsite, - site.reset, - site.makenavigation, + site.sitereset, + site.setnavigation, } Ferron.build = { @@ -43,7 +43,7 @@ if arg[1] == "--set" and arg[2] == "content" then end if arg[1] == "--make" and arg[2] == "site" then - return site.makesite() + return site.startsite() end if arg[1] == "--run" and type(arg[2]) == "string" and arg[2] ~= "" then diff --git a/ferron/site.lua b/ferron/site.lua @@ -11,20 +11,20 @@ local site = { navigation = {}, } -function site.listsites() +function site.getsitelist(siteslocation) + local location = siteslocation or site.location -- Create a simple array with the directory name of the sites - local siteslist = {} - - tableutils.each( - function(s) - if fileutils.isDirectory(site.location .. s) - and s ~= "." - and s ~= ".." + local siteslist = tableutils.map( + function(site, i) + if fileutils.isDirectory(location .. site) + and site ~= "." + and site ~= ".." then - siteslist[#siteslist+1] = s + return site end end, - tableutils.settable(lfs.dir(site.location)) + + tableutils.settable(lfs.dir(location)) ) return siteslist @@ -38,11 +38,11 @@ function site.getsiteconfig(sitename) return siteconfig end -function site.setsite(name) - local siteslist = site.listsites() +function site.setsite(sitename) + local siteslist = site.getsitelist() local whichsite = nil - if #siteslist > 1 and not name then + if #siteslist > 1 and not sitename then repeat io.write("For which website do you want to proceed? \n") @@ -55,13 +55,13 @@ function site.setsite(name) whichsite=io.read() until (tableutils.haskey(siteslist, tonumber(whichsite))) == true - elseif not name then + elseif not sitename then whichsite = 1 end -- Assign values to site.path and site.config - site.path = site.location .. (name ~= nil and name or siteslist[tonumber(whichsite)]) - site.config = tableutils.extend({}, config, site.getsiteconfig((name ~= nil and name or siteslist[tonumber(whichsite)]))) + site.path = site.location .. (sitename ~= nil and sitename or siteslist[tonumber(whichsite)]) + site.config = tableutils.extend({}, config, site.getsiteconfig((sitename ~= nil and sitename or siteslist[tonumber(whichsite)]))) -- Have different values if devmode is at trur or not site.config.baseurl = (Ferron.devmode == true and site.config.urldev or site.config.url) @@ -78,32 +78,10 @@ function site.setsite(name) return true end -function site.makenavigation() - local contentpath = assert(fileutils.isDirectory(site.content)) - local navigationTable = {} - - -- It's a dumb way to sort but it works for now. - -- Maybe it should be with while loop or coroutine. - local function sortNavigation(tbl) - local sortTable = {} - - for idx, val in ipairs(tbl) do - if val.order then - sortTable[val.order] = val - end - end - - for idx, val in ipairs(tbl) do - if not val.order then - sortTable[#sortTable+1] = val - end - end - - return sortTable - end - - tableutils.map( - function(f) +function site.setnavigation(contentlocation) + local location = assert(fileutils.isDirectory(contentlocation or site.content)) + local navigation = tableutils.map( + function(f, i) local meta = fileutils.getpageconf(f) if meta.navigation then @@ -116,17 +94,39 @@ function site.makenavigation() location = fileutils.getdirname(location) end - return table.insert(navigationTable, {label = label, location = location, attributes = attributes, order = order}) + return {label = label, location = location, attributes = attributes, order = order} end end, -- The data table - tableutils.filter(fileutils.isMarkdown, tableutils.settable(fileutils.getdirtree(contentpath))) + tableutils.filter(fileutils.isMarkdown, tableutils.settable(fileutils.getdirtree(location))) ) - return tableutils.extend(site.navigation, sortNavigation(navigationTable)) + -- It's a dumb way to sort but it works for now. + -- Maybe it should be coroutines. + local function sortnavigation(tbl) + local _tbl = {} + + -- First, add those who get an navigation.order property sets + for idx, val in ipairs(tbl) do + if val.order then + _tbl[val.order] = val + end + end + + --- Then add the rest + for idx, val in ipairs(tbl) do + if not val.order then + _tbl[#_tbl+1] = val + end + end + + return _tbl + end + + return tableutils.extend(site.navigation, sortnavigation(navigation)) end -function site.makesite() +function site.startsite(samplelocation) local newsite = { domain = nil, name = nil, @@ -134,6 +134,7 @@ function site.makesite() config = nil, sample = site.location .. "ferron-ssg.tld", } + local location = samplelocation or newsite.sample io.write("How do you want names your new website? \n") io.write("Please enter that name below... \n") @@ -144,7 +145,7 @@ function site.makesite() fileutils.mkdir(newsite.location) - os.execute("cp -Rp " .. newsite.sample .. "/*" .. " " .. newsite.location) + os.execute("cp -Rp " .. location .. "/*" .. " " .. newsite.location) if fileutils.isDirectory(newsite.location) then return print("Your new website is ready to be cutomize here '" .. newsite.location .. "'!") @@ -154,8 +155,8 @@ function site.makesite() end -- Reset the `public_html/` folder of the selected site -function site.reset() - local publichtml = site.html +function site.sitereset(htmllocation) + local location = htmllocation or site.html local function removefiles(dir) local ok, errormsg -- remove files from directory @@ -185,7 +186,7 @@ function site.reset() end end - removefiles(publichtml) + removefiles(location) end return site diff --git a/ferron/utilities/table-utils.lua b/ferron/utilities/table-utils.lua @@ -2,11 +2,11 @@ local tableutils = {} -- sortdescendingpairs -function tableutils.sortdescendingpairs(t) +function tableutils.sortdescendingpairs(tbl) local keys = {} local i = 0 - for k in pairs(t) do table.insert(keys, k) end + for k in pairs(tbl) do table.insert(keys, k) end table.sort(keys, function(a,b) if tonumber(a) and tonumber(b) then @@ -19,14 +19,14 @@ function tableutils.sortdescendingpairs(t) return function() if i < #keys then i = i + 1 - return keys[i], t[keys[i]] + return keys[i], tbl[keys[i]] end end end -- hasvalue -function tableutils.hasvalue(tab, val) - for index, value in ipairs(tab) do +function tableutils.hasvalue(tbl, val) + for index, value in ipairs(tbl) do if value == val then return true end @@ -36,8 +36,8 @@ function tableutils.hasvalue(tab, val) end -- haskey -function tableutils.haskey(tab, key) - for k, v in ipairs(tab) do +function tableutils.haskey(tbl, key) + for k, v in ipairs(tbl) do if k == key then return true end @@ -47,10 +47,10 @@ function tableutils.haskey(tab, key) end -- length -function tableutils.length(tab) +function tableutils.length(tbl) local count = 0 - for _ in pairs(tab) do + for _ in pairs(tbl) do count = count + 1 end @@ -72,18 +72,18 @@ end function tableutils.settable(...) - local t = {} + local _tbl = {} - for x in ... do t[#t + 1] = x end + for x in ... do _tbl[#_tbl + 1] = x end - return t + return _tbl end -- each(function, table) -- ex.: tableutils.each(double, {1,2,3}) -> {2,4,6} function tableutils.each(func, tbl) - for i,v in pairs(tbl) do - func(v) + for i, v in pairs(tbl) do + func(v, i) end return @@ -92,27 +92,27 @@ end -- map(function, table) -- ex.: tableutils.map(double, {1,2,3}) -> {2,4,6} function tableutils.map(func, tbl) - local newtbl = {} + local _tbl = {} - for i,v in pairs(tbl) do - newtbl[i] = func(v) + for i, v in pairs(tbl) do + _tbl[#_tbl+1] = func(v, i) end - return newtbl + return _tbl end -- filter(function, table) -- ex.: tableutils.filter(is_even, {1,2,3,4}) -> {2,4} function tableutils.filter(func, tbl) - local newtbl= {} + local _tbl= {} for i,v in pairs(tbl) do if func(v) then - newtbl[i] = v + _tbl[i] = v end end - return newtbl + return _tbl end return tableutils