ferron

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

commit 4e4c37f1bfcfc4719942ea0a358c7c5b5ad11982
parent e8334e06a3a40bace39813e3ca51e41d16fe7da3
Author: Hugo Soucy <hugo.soucy@equisoft.com>
Date:   Sun, 29 Sep 2019 15:05:32 -0400

Refactor the way Ferron consume sitedata

Diffstat:
Mferron/init.lua | 12+++---------
Mferron/page.lua | 42++++++++++++++++++++----------------------
Mferron/site.lua | 85+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mferron/utilities/file-utils.lua | 8++++----
Mferron/utilities/template-utils.lua | 4++--
5 files changed, 71 insertions(+), 80 deletions(-)

diff --git a/ferron/init.lua b/ferron/init.lua @@ -24,11 +24,7 @@ Ferron = { devmode = (arg[2] == '--dev' and true or false), site = { - location = lfs.currentdir() .. '/' .. config.sites, - config = nil, - path = nil, pagestable = {}, - navigation = {}, }, } @@ -43,11 +39,9 @@ if arg[1] == 'build' then end return tb.build{ - site.setsite(sitename), - site.setnavigation(), - page.makepages(), - link.makeshorts(), - link.makerewritemap(), + page.makepages(site.setsite(sitename)), + -- link.makeshorts(), + -- link.makerewritemap(), static.init() } end diff --git a/ferron/page.lua b/ferron/page.lua @@ -35,13 +35,13 @@ local function sortentries(entries_tb, list_mt) return list_mt['entries'] end -local function setentries(directory, list_mt) +local function setentries(directory, list_mt, sitedata) local entries_tb = {} tb.each( function(entry) local entry_metadatas = fl.getpageconf(entry) - local entry_key = entry_metadatas.date .. 'T' .. entry_metadatas.datetime .. ':' .. fl.getrelpath(fl.removeextension(entry)) + local entry_key = entry_metadatas.date .. 'T' .. entry_metadatas.datetime .. ':' .. fl.getrelpath(fl.removeextension(entry), sitedata.content) entries_tb[entry_key] = Ferron.site.pagestable[entry_key] end, @@ -61,36 +61,34 @@ local function setpagetable(meta, relpath) return end -local function setmetaprops(pagesource, relpath) +local function setmetaprops(pagesource, relpath, sitedata) return { content = markdown(fl.pullfilecontent(pagesource)), - navigation = Ferron.site.navigation, - permalink = site.config.baseurl .. (fl.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''), + navigation = sitedata.navigation, + permalink = sitedata.config.baseurl .. (fl.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''), rellink = (fl.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''), - site = site.config, + site = sitedata.config, updated = os.date('%Y-%m-%dT%H:%M:%S', lfs.attributes(pagesource).modification), - id = 'tag:' .. site.config.domainname .. ',' .. fl.getpageconf(pagesource).date .. ':' .. relpath, + id = 'tag:' .. sitedata.config.domainname .. ',' .. fl.getpageconf(pagesource).date .. ':' .. relpath, entries = {}, } end -function page.render(pagesource) +function page.render(pagesource, sitedata) local page = pagesource - local pathRel = fl.getrelpath(fl.removeextension(page)) - local pathToHtml = fl.sethtmlpath(fl.getrelpath(fl.getdirname(page))) + local pathRel = fl.getrelpath(fl.removeextension(page), sitedata.content) + local pathToHtml = fl.sethtmlpath(fl.getrelpath(fl.getdirname(page), sitedata.content), sitedata.html) local metaTable = setmetatable( - setmetaprops(page, pathRel), {__index = fl.getpageconf(page)} + setmetaprops(page, pathRel, sitedata), {__index = fl.getpageconf(page)} ) - local shortlinks_db = fl.isFile(site.data .. '/shortlinks.json') + local shortlinks_db = fl.isFile(sitedata.data .. '/shortlinks.json') local shortlinks_tb = (shortlinks_db ~= false and json.decode(fl.pullfilecontent(shortlinks_db)) or nil) - -- Populate Ferron.site.pagestable{} if fl.isNotIndex(page) then setpagetable(metaTable, pathRel) end - -- If exists set the shortlink if fl.isNotIndex(page) then if shortlinks_tb and shortlinks_db then @@ -100,7 +98,7 @@ function page.render(pagesource) -- For index page set and get entries if fl.isIndex(page) then - setentries(fl.getdirname(page), metaTable) + setentries(fl.getdirname(page), metaTable, sitedata) end -- If the ancestors of the page dosen't exists make it @@ -119,27 +117,27 @@ function page.render(pagesource) pathToHtml .. '/' .. fl.getplainname(page) .. '.html', -- With that mustache template tp.rendertemplate( - tp.selecttemplate(false, metaTable.template), + tp.selecttemplate(false, metaTable.template, sitedata.templates), metaTable, - assert(fl.isDirectory(site.templates)) .. '/partials' + assert(fl.isDirectory(sitedata.templates)) .. '/partials' ) ) print(lfs.attributes(page).size) - return print(metaTable.date .. ' - ' .. metaTable.title) + return --print(metaTable.date .. ' - ' .. metaTable.title) end -function page.makepages() - local contentpath = tb.from(fl.getdirtree(fl.isDirectory(site.content))) +function page.makepages(sitedata) + local contentpath = tb.from(fl.getdirtree(fl.isDirectory(sitedata.content))) local singlepages = tb.filter(fl.isNotIndex, contentpath) local indexpages = tb.filter(fl.isIndex, contentpath) -- Render single pages - tb.each(page.render, singlepages) + tb.each(function(p) page.render(p, sitedata) end, singlepages) -- Render index pages - tb.each(page.render, indexpages) + tb.each(function(p) page.render(p, sitedata) end, indexpages) print('==========') print('ยง ' .. tb.length(Ferron.site.pagestable) .. ' HTML pages have been created.') diff --git a/ferron/site.lua b/ferron/site.lua @@ -12,15 +12,14 @@ local tb = require 'ferron.utilities.table-utils' local fl = 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 +local function getsitelist() + local location = config.sites return tb.map( function(site, i) if fl.isDirectory(location .. site) and fl.isNotDotDirectory(site) then - print(site) return site end end, @@ -29,9 +28,9 @@ function site.getsitelist(siteslocation) end -- Check if the value of the arg[1] (sitename) exists in the table --- returns by site.getsitelist method. -function site.isSite(sitename) - if tb.hasvalue(site.getsitelist(), sitename) then +-- returns by getsitelist method. +local function isSite(sitename) + if tb.hasvalue(getsitelist(), sitename) then return sitename end @@ -39,31 +38,27 @@ function site.isSite(sitename) end -- Get the configuration file of the selected website. -function site.getsiteconfig(sitename) +local function getsiteconfig(sitename) package.path = package.path .. ';sites/'.. sitename ..'/?.lua' return require 'config' end -function site.setsiteconfig(sitename) - local thatsite = assert(site.isSite(sitename), "That is not a valid site") +local function setsiteconfig(sitename) + local thatsite = assert(isSite(sitename), "That is not a valid site") - site.config = tb.extend({}, config, site.getsiteconfig(thatsite)) - Ferron.site.config = site.config + site.config = tb.extend({}, config, getsiteconfig(thatsite)) return site.config end -function site.setsitepath(sitename) - local thatsite = assert(site.isSite(sitename), "That is not a valid site") +local function setsitepath(sitename) + local thatsite = assert(isSite(sitename), "That is not a valid site") - site.path = Ferron.site.location .. thatsite - Ferron.site.path = site.path - - return site.path + return config.sites .. thatsite end -function site.makepaths(sitepath, siteconfig) +local function makepaths(sitepath, siteconfig) for k, v in pairs(siteconfig.paths) do site[k] = sitepath .. siteconfig['paths'][k] end @@ -71,7 +66,7 @@ function site.makepaths(sitepath, siteconfig) return end -function site.choosesite(siteslist) +local function choosesite(siteslist) local whichsite assert(type(siteslist) == 'table', 'The argument "siteslist" must be a table') @@ -98,32 +93,14 @@ function site.choosesite(siteslist) return siteslist[tonumber(whichsite)] end -function site.setsite(sitename) - local thatsite = sitename and sitename or site.choosesite(site.getsitelist()) - - tb.build{ - -- Assign values to site.path and site.config - site.setsitepath(thatsite), - site.setsiteconfig(thatsite), - -- Create absolute path properties for the chosen site's folders - site.makepaths(site.setsitepath(thatsite), site.setsiteconfig(thatsite)), - } - - -- Have different values if devmode is at true or not - site.config.baseurl = (Ferron.devmode == true and site.config.urldev or site.config.url) - - return -end - -function site.setnavigation(contentlocation) - local location = assert(fl.isDirectory(contentlocation or site.content)) +local function setnavigation(contentlocation, baseurl) local navigation = tb.map( function(f, i) local meta = fl.getpageconf(f) if meta.navigation then local label = meta.navigation.label and meta.navigation.label or meta.title - local location = site.config.baseurl .. fl.removeextension(fl.getrelpath(f)) .. '.html' + local location = baseurl .. fl.removeextension(fl.getrelpath(f, contentlocation)) .. '.html' local attributes = meta.navigation.attributes local order = meta.navigation.order @@ -135,7 +112,7 @@ function site.setnavigation(contentlocation) end end, -- The data table - tb.filter(fl.isMarkdown, tb.from(fl.getdirtree(location))) + tb.filter(fl.isMarkdown, tb.from(fl.getdirtree(contentlocation))) ) -- It's a dumb way to sort but it works for now. @@ -160,7 +137,26 @@ function site.setnavigation(contentlocation) return _tbl end - return tb.extend(Ferron.site.navigation, sortnavigation(navigation)) + return tb.extend({}, sortnavigation(navigation)) +end + +function site.setsite(sitename) + local thatsite = sitename and sitename or choosesite(getsitelist()) + + tb.build{ + -- Assign values to site.path and site.config + setsitepath(thatsite), + setsiteconfig(thatsite), + -- Create absolute path properties for the chosen site's folders + makepaths(setsitepath(thatsite), setsiteconfig(thatsite)), + } + + -- Have different values if devmode is at true or not + site.config.baseurl = (Ferron.devmode == true and site.config.urldev or site.config.url) + -- Create the main navigation menu + site.navigation = setnavigation(site.content, site.config.baseurl) + + return site --, print(inspect(site)) end function site.startsite(samplelocation) @@ -169,7 +165,7 @@ function site.startsite(samplelocation) name = nil, location = nil, config = nil, - sample = Ferron.site.location .. 'ferron-ssg.tld', + sample = config.sites .. 'ferron-ssg.tld', } local location = samplelocation or newsite.sample @@ -178,7 +174,7 @@ function site.startsite(samplelocation) io.flush() newsite.name = io.read() - newsite.location = Ferron.site.location .. newsite.name + newsite.location = config.sites .. newsite.name fl.mkdir(newsite.location) @@ -230,4 +226,7 @@ function site.sitereset(htmllocation) removefiles(location) end +-- Init a site before generates it +-- function site.init() +-- end return setmetatable(site, sitemeta) diff --git a/ferron/utilities/file-utils.lua b/ferron/utilities/file-utils.lua @@ -164,12 +164,12 @@ function fileutils.pushfilecontent(pathtofile, data) return assert(fileutils.isFile(pathtofile)) end -function fileutils.getrelpath(file) - return file:sub((Ferron.site.path .. Ferron.site.config.paths.content):len() + 1) +function fileutils.getrelpath(file, contentdir) + return file:sub((contentdir):len() + 1) end -function fileutils.sethtmlpath(folder) - return Ferron.site.path .. Ferron.site.config.paths.html .. folder +function fileutils.sethtmlpath(folder, htmldir) + return htmldir .. folder end function fileutils.removeextension(file) diff --git a/ferron/utilities/template-utils.lua b/ferron/utilities/template-utils.lua @@ -11,8 +11,8 @@ local fl = require 'ferron.utilities.file-utils' -- set template -function templateutils.selecttemplate(index, templatename) - local templatepath = Ferron.site.path .. Ferron.site.config.paths.templates .. '/' +function templateutils.selecttemplate(index, templatename, templatesdir) + local templatepath = templatesdir .. '/' local template = nil if templatename and type(templatename) == 'string' then