ferron

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

commit fe9dc305975800f3e69b340b1e7a3c3073cf36bb
parent faec6382b1e0075109ac7ce0882fdd286f5b74ca
Author: Hugo Soucy <hs0ucy@h50ucy.local>
Date:   Fri, 26 Apr 2019 22:02:41 -0400

Merge the page and list modules

Diffstat:
Mferron/init.lua | 2--
Dferron/list.lua | 114-------------------------------------------------------------------------------
Mferron/page.lua | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
3 files changed, 62 insertions(+), 125 deletions(-)

diff --git a/ferron/init.lua b/ferron/init.lua @@ -10,7 +10,6 @@ local config = require 'ferron.config' local site = require 'ferron.site' local content = require 'ferron.content' local page = require 'ferron.page' -local list = require 'ferron.list' local link = require 'ferron.link' local static = require 'ferron.static' local plugin = require 'ferron.plugin' @@ -37,7 +36,6 @@ Ferron = { build = { page.init, - list.init, link.makeshorts, link.makerewritemap, static.init, diff --git a/ferron/list.lua b/ferron/list.lua @@ -1,114 +0,0 @@ --- list.lua -local list = {} -local listmeta = { - __call = function(self, key, vars) - print(key) - end -} - - -local json = require 'dkjson' -local markdown = require 'discount' -local fileutils = require 'ferron.utilities.file-utils' -local tableutils = require 'ferron.utilities.table-utils' -local templateutils = require 'ferron.utilities.template-utils' -local feed = require 'ferron.feed' -local static = require 'ferron.static' - -print(page) - -local function sortentries(entries_tb, list_mt) - local list_length = list_mt.length or nil - local entries_count = list_length and 0 or nil - - if entries_count then - for k, v in tableutils.sortdescendingpairs(entries_tb) do - if entries_count < list_length then - table.insert(list_mt['entries'], v) - - entries_count = entries_count + 1 - end - end - else - for k, v in tableutils.sortdescendingpairs(entries_tb) do - table.insert(list_mt['entries'], v) - end - end - - return list_mt['entries'] -end - -local function setentries(directory, list_mt) - local entries_tb = {} - - tableutils.each( - function(entry) - local entry_metadatas = fileutils.getpageconf(entry) - local entry_key = entry_metadatas.date .. 'T' .. entry_metadatas.datetime .. ':' .. fileutils.getrelpath(fileutils.removeextension(entry)) - - entries_tb[entry_key] = Ferron.site.pagestable[entry_key] - end, - tableutils.filter(fileutils.isNotIndex, tableutils.from(fileutils.getdirtree(directory))) - ) - - sortentries(entries_tb, list_mt) - - return list_mt['entries'] -end - -function list.render(listsource) - local list = listsource - local list_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(fileutils.getdirname(list))) - local list_conf = fileutils.getpageconf(list) - local list_conf_mt = setmetatable({}, { __index = list_conf }) - - print('§ Make a list page for the `' .. fileutils.getrelpath(fileutils.getdirname(list)) .. '` subdirectory ...') - - -- Convert the markdown file to HTML - list_conf_mt.content = markdown(fileutils.pullfilecontent(list)) - -- Set a dynamic permalink - list_conf_mt.permalink = Ferron.site.config.baseurl .. fileutils.getdirname(fileutils.getrelpath(fileutils.removeextension(list))) - -- Import site configuration in the metatable - list_conf_mt['site'] = Ferron.site.config - -- Import site navigation in the metatable - list_conf_mt['navigation'] = Ferron.site.navigation - -- Set a table for the list of entries - list_conf_mt['entries'] = {} - - -- Set and get entries of the list - setentries(fileutils.getdirname(list), list_conf_mt) - - if not fileutils.isDirectory(list_htmlpath) then - fileutils.mkdir(list_htmlpath) - end - - -- Create syndication feeds for each list - if list_conf.feed ~= false then - feed.makefeed(list_conf_mt, list_htmlpath) - end - - -- Move static files in the public HTML folder - -- static.dispatch(list) - - print('==========') - - -- Build and push the HTML page - fileutils.pushfilecontent( - list_htmlpath .. '/' .. fileutils.getplainname(list) .. '.html', - templateutils.rendertemplate( - templateutils.selecttemplate(false, list_conf.template), - list_conf_mt, - Ferron.site.templates .. '/partials' - ) - ) -end - -function list.init() - local contentpath = assert(fileutils.isDirectory(Ferron.site.content)) - - tableutils.each(list.render, tableutils.filter(fileutils.isIndex, tableutils.from(fileutils.getdirtree(contentpath)))) - - return -end - -return setmetatable(list, listmeta) diff --git a/ferron/page.lua b/ferron/page.lua @@ -11,6 +11,46 @@ local markdown = require 'discount' local fileutils = require 'ferron.utilities.file-utils' local tableutils = require 'ferron.utilities.table-utils' local templateutils = require 'ferron.utilities.template-utils' +local feed = require 'ferron.feed' + +local function sortentries(entries_tb, list_mt) + local list_length = list_mt.length or nil + local entries_count = list_length and 0 or nil + + if entries_count then + for k, v in tableutils.sortdescendingpairs(entries_tb) do + if entries_count < list_length then + table.insert(list_mt['entries'], v) + + entries_count = entries_count + 1 + end + end + else + for k, v in tableutils.sortdescendingpairs(entries_tb) do + table.insert(list_mt['entries'], v) + end + end + + return list_mt['entries'] +end + +local function setentries(directory, list_mt) + local entries_tb = {} + + tableutils.each( + function(entry) + local entry_metadatas = fileutils.getpageconf(entry) + local entry_key = entry_metadatas.date .. 'T' .. entry_metadatas.datetime .. ':' .. fileutils.getrelpath(fileutils.removeextension(entry)) + + entries_tb[entry_key] = Ferron.site.pagestable[entry_key] + end, + tableutils.filter(fileutils.isNotIndex, tableutils.from(fileutils.getdirtree(directory))) + ) + + sortentries(entries_tb, list_mt) + + return list_mt['entries'] +end local function setpagetable(meta, relpath) local pagekey = meta.date .. 'T' .. meta.datetime .. ':' .. relpath @@ -29,6 +69,7 @@ local function setmetaprops(pagesource, relpath) site = Ferron.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, + entries = {}, } end @@ -41,13 +82,25 @@ function page.render(pagesource) ) -- Populate Ferron.site.pagestable{} - setpagetable(metaTable, pathRel) + if fileutils.isNotIndex(page) then + setpagetable(metaTable, pathRel) + end + + -- For index page set and get entries + if fileutils.isIndex(page) then + setentries(fileutils.getdirname(page), metaTable) + end -- If the ancestors of the page dosen't exists make it if not fileutils.isDirectory(pathToHtml) then fileutils.mkdir(pathToHtml) end + -- For each index page create syndication feeds + if fileutils.isIndex(page) and metaTable.feed ~= false then + feed.makefeed(metaTable, pathToHtml) + end + -- Build and push the HTML page fileutils.pushfilecontent( -- Create that page @@ -64,15 +117,15 @@ function page.render(pagesource) end function page.init() - local contentpath = assert(fileutils.isDirectory(Ferron.site.content)) + local contentpath = tableutils.from(fileutils.getdirtree(fileutils.isDirectory(Ferron.site.content))) + local singlepages = tableutils.filter(fileutils.isNotIndex, contentpath) + local indexpages = tableutils.filter(fileutils.isIndex, contentpath) - tableutils.each( - page.render, - tableutils.filter( - fileutils.isNotIndex, - tableutils.from(fileutils.getdirtree(contentpath)) - ) - ) + -- Render single pages + tableutils.each(page.render, singlepages) + + -- Render index pages + tableutils.each(page.render, indexpages) print('==========') print('§ ' .. tableutils.length(Ferron.site.pagestable) .. ' HTML pages have been created.')