ferron

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

commit 4094c300e49a548876d7bdd1a74cab30476c9279
parent 1b0dc87e3a87447f7a1aac7102f149b7a0317778
Author: Hugo Soucy <hs0ucy@h50ucy.local>
Date:   Thu, 27 Jun 2019 22:07:54 -0400

Try to simplify, split to multiple functions.

Diffstat:
Mferron/content.lua | 13+++++++------
Mferron/feed.lua | 7++++---
Mferron/link.lua | 8++++----
Mferron/page.lua | 13+++++++------
Mferron/plugin.lua | 2+-
Mferron/site.lua | 75+++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mferron/static.lua | 9+++++----
7 files changed, 71 insertions(+), 56 deletions(-)

diff --git a/ferron/content.lua b/ferron/content.lua @@ -10,6 +10,7 @@ local contentmeta = { } -- Required Packages +local site = require 'ferron.site' local fileutils = require 'ferron.utilities.file-utils' local slugify = require 'ferron.utilities.lua-slugify.slugify' local tableutils = require 'ferron.utilities.table-utils' @@ -17,7 +18,7 @@ local templateutils = require 'ferron.utilities.template-utils' function content.getarchetypes(dir) local archetypes = {} - local archetypesdir = dir or Ferron.site.archetypes + local archetypesdir = dir or site.archetypes if fileutils.isDirectory(archetypesdir) then for archetype, attr in fileutils.getdirtree(archetypesdir) do @@ -33,7 +34,7 @@ function content.getarchetypes(dir) end function content.setcontent() - local archetypes = content.getarchetypes(Ferron.site.archetypes) + local archetypes = content.getarchetypes(site.archetypes) local whicharchetype = nil local whichtitle = nil @@ -66,8 +67,8 @@ function content.makecontent(contenttype, contenttitle) if tableutils.hasvalue(content.getarchetypes(), archetypetype) and type(title) == 'string' then local archetype = {} - local contentpath = Ferron.site.content .. '/' .. archetypetype .. '/' .. os.date('%Y') ..'/'..os.date('%m') .. '/' - local archetypepath = Ferron.site.archetypes .. '/' + local contentpath = site.content .. '/' .. archetypetype .. '/' .. os.date('%Y') ..'/'..os.date('%m') .. '/' + local archetypepath = site.archetypes .. '/' archetype.title = title archetype.filename = slugify(archetype.title) @@ -84,7 +85,7 @@ function content.makecontent(contenttype, contenttitle) contentpath .. archetype.filename .. '.md', templateutils.rendermustache( fileutils.pullfilecontent(archetypepath .. archetypetype .. '.md' ), - Ferron.site.templates .. '/partials', + site.templates .. '/partials', archetype ) ) @@ -94,7 +95,7 @@ function content.makecontent(contenttype, contenttitle) contentpath .. archetype.filename .. '.json', templateutils.rendermustache( fileutils.pullfilecontent(archetypepath .. archetypetype .. '.json'), - Ferron.site.templates .. '/partials', + site.templates .. '/partials', archetype ) ) diff --git a/ferron/feed.lua b/ferron/feed.lua @@ -6,16 +6,17 @@ local feedmeta = { end } +local site = require 'ferron.site' local fileutils = require 'ferron.utilities.file-utils' local tableutils = require 'ferron.utilities.table-utils' local templateutils = require 'ferron.utilities.template-utils' function feed.makefeed(entries, destination) - local feedtypes = Ferron.site.config.feedtypes - local feedpartials = fileutils.isDirectory(Ferron.site.templates .. '/partials') + local feedtypes = site.config.feedtypes + local feedpartials = fileutils.isDirectory(site.templates .. '/partials') for k, v in pairs(feedtypes) do - local feedtemplate = Ferron.site.templates .. '/feed/' .. k .. '.mustache' + local feedtemplate = site.templates .. '/feed/' .. k .. '.mustache' if fileutils.isFile(feedtemplate) then fileutils.pushfilecontent( diff --git a/ferron/link.lua b/ferron/link.lua @@ -6,8 +6,8 @@ local linkmeta = { end } --- @TODO Make sure to always keep the alphanumerical order local json = require 'dkjson' +local site = require 'ferron.site' local fileutils = require 'ferron.utilities.file-utils' local tableutils = require 'ferron.utilities.table-utils' local templateutils = require 'ferron.utilities.template-utils' @@ -15,7 +15,7 @@ local templateutils = require 'ferron.utilities.template-utils' function link.makeshorts() local links = {} local links_keyorder = {} - local links_db = Ferron.site.data .. '/shortlinks.json' + local links_db = site.data .. '/shortlinks.json' -- If `links_db` dosen't exists create it if fileutils.isFile(links_db) == false then @@ -50,8 +50,8 @@ function link.makeshorts() end function link.makerewritemap() - local links_db = Ferron.site.data .. '/shortlinks.json' - local links_map = Ferron.site.html .. '/shortlinksmap.txt' + local links_db = site.data .. '/shortlinks.json' + local links_map = site.html .. '/shortlinksmap.txt' if fileutils.isFile(links_db) then links = json.decode(fileutils.pullfilecontent(links_db)) diff --git a/ferron/page.lua b/ferron/page.lua @@ -8,6 +8,7 @@ local pagemeta = { local json = require 'dkjson' local markdown = require 'discount' +local site = require 'ferron.site' local fileutils = require 'ferron.utilities.file-utils' local tableutils = require 'ferron.utilities.table-utils' local templateutils = require 'ferron.utilities.template-utils' @@ -64,11 +65,11 @@ local function setmetaprops(pagesource, relpath) return { content = markdown(fileutils.pullfilecontent(pagesource)), navigation = Ferron.site.navigation, - permalink = Ferron.site.config.baseurl .. (fileutils.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''), + permalink = site.config.baseurl .. (fileutils.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''), rellink = (fileutils.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''), - site = Ferron.site.config, + site = site.config, updated = os.date('%Y-%m-%dT%H:%M:%S', lfs.attributes(pagesource).modification), - id = 'tag:' .. Ferron.site.config.domainname .. ',' .. fileutils.getpageconf(pagesource).date .. ':' .. relpath, + id = 'tag:' .. site.config.domainname .. ',' .. fileutils.getpageconf(pagesource).date .. ':' .. relpath, entries = {}, } end @@ -80,7 +81,7 @@ function page.render(pagesource) local metaTable = setmetatable( setmetaprops(page, pathRel), {__index = fileutils.getpageconf(page)} ) - local shortlinks_db = fileutils.isFile(Ferron.site.data .. '/shortlinks.json') + local shortlinks_db = fileutils.isFile(site.data .. '/shortlinks.json') local shortlinks_tb = (shortlinks_db ~= false and json.decode(fileutils.pullfilecontent(shortlinks_db)) or nil) @@ -120,7 +121,7 @@ function page.render(pagesource) templateutils.rendertemplate( templateutils.selecttemplate(false, metaTable.template), metaTable, - assert(fileutils.isDirectory(Ferron.site.templates)) .. '/partials' + assert(fileutils.isDirectory(site.templates)) .. '/partials' ) ) @@ -128,7 +129,7 @@ function page.render(pagesource) end function page.makepages() - local contentpath = tableutils.from(fileutils.getdirtree(fileutils.isDirectory(Ferron.site.content))) + local contentpath = tableutils.from(fileutils.getdirtree(fileutils.isDirectory(site.content))) local singlepages = tableutils.filter(fileutils.isNotIndex, contentpath) local indexpages = tableutils.filter(fileutils.isIndex, contentpath) diff --git a/ferron/plugin.lua b/ferron/plugin.lua @@ -9,7 +9,7 @@ local pluginmeta = { local fileutils = require 'ferron.utilities.file-utils' function plugin.run(site, plugname) - local plugin = require(Ferron.site.config.sites .. fileutils.getbasename(Ferron.site.path) .. Ferron.site.config.paths.plugins .. '/' .. plugname) + local plugin = require(site.config.sites .. fileutils.getbasename(site.path) .. site.config.paths.plugins .. '/' .. plugname) return plugin.init(site) end diff --git a/ferron/site.lua b/ferron/site.lua @@ -11,10 +11,11 @@ local config = require 'ferron.config' local tableutils = require 'ferron.utilities.table-utils' local fileutils = require 'ferron.utilities.file-utils' +-- Create a simple array with the directory name of the sites function site.getsitelist(siteslocation) local location = siteslocation or Ferron.site.location - -- Create a simple array with the directory name of the sites - local siteslist = tableutils.map( + + return tableutils.map( function(site, i) if fileutils.isDirectory(location .. site) and site ~= '.' @@ -23,11 +24,8 @@ function site.getsitelist(siteslocation) return site end end, - tableutils.from(lfs.dir(location)) ) - - return siteslist end function site.isSite(sitename) @@ -45,26 +43,38 @@ function site.getsiteconfig(sitename) end function site.setsiteconfig(sitename) - local thatSite = assert(site.isSite(sitename), "That is not a valid site") + local thatsite = assert(site.isSite(sitename), "That is not a valid site") + + site.config = tableutils.extend({}, config, site.getsiteconfig(thatsite)) + Ferron.site.config = site.config - Ferron.site.config = tableutils.extend({}, config, site.getsiteconfig(thatSite)) - return Ferron.site.config + return site.config end function site.setsitepath(sitename) - local thatSite = assert(site.isSite(sitename), "That is not a valid site") + local thatsite = assert(site.isSite(sitename), "That is not a valid site") - Ferron.site.path = Ferron.site.location .. thatSite + site.path = Ferron.site.location .. thatsite + Ferron.site.path = site.path - return Ferron.site.path + return site.path end -function site.setsite(sitename) - local siteslist = site.getsitelist() +function site.makepaths(sitepath, siteconfig) + for k, v in pairs(siteconfig.paths) do + site[k] = sitepath .. siteconfig['paths'][k] + end + + return +end + +function site.choosesite(siteslist) local whichsite - if #siteslist > 1 and not sitename then + assert(type(siteslist) == 'table', 'The argument "siteslist" must be a table') + + if #siteslist > 1 then repeat io.write('For which website do you want to proceed? \n') @@ -77,39 +87,40 @@ function site.setsite(sitename) whichsite=io.read() until (tableutils.haskey(siteslist, tonumber(whichsite))) == true - elseif not sitename then + elseif #siteslist == 1 then whichsite = 1 + else + return 'Something is wrong!' end - -- Assign values to Ferron.site.path and Ferron.site.config - site.setsitepath(sitename and sitename or siteslist[tonumber(whichsite)]) - site.setsiteconfig((sitename and sitename or siteslist[tonumber(whichsite)])) + return siteslist[tonumber(whichsite)] +end + +function site.setsite(sitename) + local thatsite = sitename and sitename or site.choosesite(site.getsitelist()) + + -- Assign values to site.path and site.config + site.setsitepath(thatsite) + site.setsiteconfig(thatsite) -- Have different values if devmode is at true or not - Ferron.site.config.baseurl = (Ferron.devmode == true and Ferron.site.config.urldev or Ferron.site.config.url) + site.config.baseurl = (Ferron.devmode == true and site.config.urldev or site.config.url) -- Create absolute path properties for the chosen site's folders - Ferron.site.archetypes = Ferron.site.path .. Ferron.site.config.paths.archetypes - Ferron.site.content = Ferron.site.path .. Ferron.site.config.paths.content - Ferron.site.data = Ferron.site.path .. Ferron.site.config.paths.data - Ferron.site.html = Ferron.site.path .. Ferron.site.config.paths.html - Ferron.site.plugins = Ferron.site.path .. Ferron.site.config.paths.plugins - Ferron.site.static = Ferron.site.path .. Ferron.site.config.paths.static - Ferron.site.static_src = Ferron.site.path .. Ferron.site.config.paths.static_src - Ferron.site.templates = Ferron.site.path .. Ferron.site.config.paths.templates - - return Ferron.site + site.makepaths(site.setsitepath(thatsite), site.setsiteconfig(thatsite)) + + return end function site.setnavigation(contentlocation) - local location = assert(fileutils.isDirectory(contentlocation or Ferron.site.content)) + 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 local label = meta.navigation.label and meta.navigation.label or meta.title - local location = Ferron.site.config.baseurl .. fileutils.removeextension(fileutils.getrelpath(f)) .. '.html' + local location = site.config.baseurl .. fileutils.removeextension(fileutils.getrelpath(f)) .. '.html' local attributes = meta.navigation.attributes local order = meta.navigation.order @@ -179,7 +190,7 @@ end -- Reset the `public_html/` folder of the selected site function site.sitereset(htmllocation) - local location = htmllocation or Ferron.site.html + local location = htmllocation or site.html local function removefiles(dir) local ok, errormsg diff --git a/ferron/static.lua b/ferron/static.lua @@ -7,21 +7,22 @@ local staticmeta = { } -- Required Packages +local site = require 'ferron.site' local fileutils = require 'ferron.utilities.file-utils' local tableutils = require 'ferron.utilities.table-utils' local function dispatchnontextual(file) - return os.execute('cp -p ' .. file .. ' ' .. Ferron.site.html) + return os.execute('cp -p ' .. file .. ' ' .. site.html) end local function movestaticfolder() - return os.execute('cp -Rp ' .. Ferron.site.static .. '/*' .. ' ' .. Ferron.site.html) + return os.execute('cp -Rp ' .. site.static .. '/*' .. ' ' .. site.html) end function static.init() - local contentpath = assert(fileutils.isDirectory(Ferron.site.content)) + local contentpath = assert(fileutils.isDirectory(site.content)) - print('¤¤ Your site is ready at `' .. Ferron.site.html .. '` ¤¤') + print('¤¤ Your site is ready at `' .. site.html .. '` ¤¤') return movestaticfolder(), tableutils.each(dispatchnontextual, tableutils.filter(fileutils.isNonTextual, tableutils.from(fileutils.getdirtree(contentpath))))