ferron

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

commit f0f98f392dac884dd81c8a2d19efea42f8f6255b
parent ea08df6c676cb58508d677782e092d7d87efc08f
Author: Hugo Soucy <hugo.soucy@savoirfairelinux.com>
Date:   Thu,  1 Feb 2018 16:32:04 -0500

Refactoring modules.

Diffstat:
Acornelius | 6++++++
Mferron.lua | 38++++++++++++++++++++++++--------------
Aferron/archetype.lua | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dferron/archetypes.lua | 72------------------------------------------------------------------------
Dferron/exec.lua | 14--------------
Aferron/feed.lua | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dferron/get-sites.lua | 28----------------------------
Aferron/list.lua | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dferron/make-atom-feed.lua | 53-----------------------------------------------------
Dferron/make-lists-of-pages.lua | 123-------------------------------------------------------------------------------
Dferron/make-pages.lua | 88-------------------------------------------------------------------------------
Aferron/page.lua | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dferron/set-site.lua | 28----------------------------
Aferron/site.lua | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Mferron/static.lua | 4++--
Dferron/template-utils.lua | 20--------------------
Rferron/file-utils.lua -> ferron/utilities/file-utils.lua | 0
Rferron/table-utils.lua -> ferron/utilities/table-utils.lua | 0
Aferron/utilities/template-utils.lua | 20++++++++++++++++++++
Drhino | 5-----
20 files changed, 431 insertions(+), 447 deletions(-)

diff --git a/cornelius b/cornelius @@ -0,0 +1,6 @@ +#!/usr/bin/env lua + +-- Cornelius is the future CLI of Ferron SSG. +-- <https://en.wikipedia.org/wiki/Cornelius_the_First> + +print("Hi, I'm Cornelius!") diff --git a/ferron.lua b/ferron.lua @@ -9,14 +9,24 @@ local Ferron = Ferron -- Ferron submodules local config = require("config") -local archetypes = require("ferron.archetypes") -local getsites = require("ferron.get-sites") -local setsite = require("ferron.set-site") -local makepages = require("ferron.make-pages") -local makelistsofpages = require("ferron.make-lists-of-pages") -local makeatomfeed = require("ferron.make-atom-feed") +local site = require("ferron.site") +local archetype = require("ferron.archetype") +local page = require("ferron.page") +local list = require("ferron.list") +local feed = require("ferron.feed") local static = require("ferron.static") -local exec = require("ferron.exec") + +local exec = function(...) + for argkey, argval in pairs({...}) do + if type(argval) == "table" then + for funckey, funcval in ipairs(argval) do + if type(funcval) == "function" then + return funcval() + end + end + end + end +end inspect = require("inspect") @@ -28,16 +38,16 @@ Ferron.archetypes = {} Ferron.devmode = (arg[1] == "--dev" and true or false) Ferron.initfunctions = { - getsites, - setsite, - archetypes.get, - archetypes.createcontent + site.getsites, + site.setsite, + archetype.getarchetypes, + archetype.createcontent } Ferron.buildfunctions = { - makepages, - makelistsofpages, - makeatomfeed, + page.makepage, + list.makelist, + feed.makefeed, static.move } diff --git a/ferron/archetype.lua b/ferron/archetype.lua @@ -0,0 +1,72 @@ +-- +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 archetype = {} + +function archetype.getarchetypes() + local archetypesdir = Ferron.site.path .. Ferron.site.config.SITE.PATHS.ARCHETYPES + + if path.isdir(archetypesdir) then + for archetype, attr in fileutils.getdirtree(archetypesdir) do + if attr.mode ~= "directory" + and archetype:match("^.+(%..+)$") == ".json" + then + Ferron.archetypes[#Ferron.archetypes+1] = archetype:match("^.+/(.+)$"):match("(.+)%..*") + end + end + + return Ferron.archetypes + end +end + +function archetype.createcontent() + if tableutils.hasvalue(Ferron.archetypes, arg[1]) and type(arg[2]) == "string" then + local archetype = {} + local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT .. "/" .. arg[1] .. "/" .. os.date("%Y") .."/"..os.date("%m") .. "/" + local archetypepath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.ARCHETYPES .. "/" + + archetype.title = arg[2] + archetype.filename = string.lower(archetype.title:gsub('%p','-'):gsub('%s','-')) + archetype.date = os.date("%Y-%m-%d") + archetype.datetime = os.date("%H:%M:%S") + archetype.template = arg[1] + + if path.isdir(contentpath) == false then + fileutils.mkdir(contentpath) + end + + -- Build the markdown file + fileutils.pushfilecontent( + contentpath .. archetype.filename .. ".md", + templateutils.setmustache( + fileutils.pullfilecontent(archetypepath .. arg[1] .. ".md" ), + Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", + archetype + ) + ) + + -- Build the JSON file + fileutils.pushfilecontent( + contentpath .. archetype.filename .. ".json", + templateutils.setmustache( + fileutils.pullfilecontent(archetypepath .. arg[1] .. ".json"), + Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", + archetype + ) + ) + + if path.isfile(contentpath .. archetype.filename .. ".md") + and path.isfile(contentpath .. archetype.filename .. ".json") + then + print("¤¤ The files `" .. contentpath .. "{" .. archetype.filename .. ".md," .. archetype.filename .. ".json}` have been created. ¤¤") + else + print("! Error, something went wrong !") + end + + os.exit() + end +end + +return archetype diff --git a/ferron/archetypes.lua b/ferron/archetypes.lua @@ -1,72 +0,0 @@ --- -local path = require("path") -local fileutils = require("ferron.file-utils") -local tableutils = require("ferron.table-utils") -local templateutils = require("ferron.template-utils") -local archetypes = {} - -function archetypes.get() - local archetypesdir = Ferron.site.path .. Ferron.site.config.SITE.PATHS.ARCHETYPES - - if path.isdir(archetypesdir) then - for archetype, attr in fileutils.getdirtree(archetypesdir) do - if attr.mode ~= "directory" - and archetype:match("^.+(%..+)$") == ".json" - then - Ferron.archetypes[#Ferron.archetypes+1] = archetype:match("^.+/(.+)$"):match("(.+)%..*") - end - end - - return Ferron.archetypes - end -end - -function archetypes.createcontent() - if tableutils.hasvalue(Ferron.archetypes, arg[1]) and type(arg[2]) == "string" then - local archetype = {} - local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT .. "/" .. arg[1] .. "/" .. os.date("%Y") .."/"..os.date("%m") .. "/" - local archetypepath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.ARCHETYPES .. "/" - - archetype.title = arg[2] - archetype.filename = string.lower(archetype.title:gsub('%p','-'):gsub('%s','-')) - archetype.date = os.date("%Y-%m-%d") - archetype.datetime = os.date("%H:%M:%S") - archetype.template = arg[1] - - if path.isdir(contentpath) == false then - fileutils.mkdir(contentpath) - end - - -- Build the markdown file - fileutils.pushfilecontent( - contentpath .. archetype.filename .. ".md", - templateutils.setmustache( - fileutils.pullfilecontent(archetypepath .. arg[1] .. ".md" ), - Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", - archetype - ) - ) - - -- Build the JSON file - fileutils.pushfilecontent( - contentpath .. archetype.filename .. ".json", - templateutils.setmustache( - fileutils.pullfilecontent(archetypepath .. arg[1] .. ".json"), - Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", - archetype - ) - ) - - if path.isfile(contentpath .. archetype.filename .. ".md") - and path.isfile(contentpath .. archetype.filename .. ".json") - then - print("¤¤ The files `" .. contentpath .. "{" .. archetype.filename .. ".md," .. archetype.filename .. ".json}` have been created. ¤¤") - else - print("! Error, something went wrong !") - end - - os.exit() - end -end - -return archetypes diff --git a/ferron/exec.lua b/ferron/exec.lua @@ -1,14 +0,0 @@ --- -local function exec(...) - for argkey, argval in pairs({...}) do - if type(argval) == "table" then - for funckey, funcval in ipairs(argval) do - if type(funcval) == "function" then - funcval() - end - end - end - end -end - -return exec diff --git a/ferron/feed.lua b/ferron/feed.lua @@ -0,0 +1,54 @@ +-- +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 feed = {} + +function feed.makefeed() + local feed_tb = {} + local feedtemplate = path.isfile(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/feed.mustache") + local feedrsstemplate = path.isfile(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/rss2.mustache") + local feedpartials = path.isdir(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials") + local feedfile = Ferron.site.path .. Ferron.site.config.SITE.PATHS.HTML .. "/feed.atom.xml" + local feedrssfile = Ferron.site.path .. Ferron.site.config.SITE.PATHS.HTML .. "/feed.rss.xml" + + print("§ Make an ATOM feed with all the pages of the site ...") + + feed_tb["SITE"] = Ferron.site.config.SITE + feed_tb["entries"] = {} + + for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do + table.insert(feed_tb["entries"], v) + end + + feed_tb["lastupdate"] = feed_tb["entries"][1].updated + + -- Build the Atom XML file + fileutils.pushfilecontent( + feedfile, + templateutils.setmustache( + fileutils.pullfilecontent(feedtemplate), + feedpartials, + feed_tb + ) + ) + + -- Build the RSS 2 XML file + fileutils.pushfilecontent( + feedrssfile, + templateutils.setmustache( + fileutils.pullfilecontent(feedrsstemplate), + feedpartials, + feed_tb + ) + ) + + if path.isfile(feedfile) == false then + print(" !! /feed.atom.xml - error!") + end + + print("==========") +end + +return feed diff --git a/ferron/get-sites.lua b/ferron/get-sites.lua @@ -1,28 +0,0 @@ --- -local path = require("path") -local fileutils = require("ferron.file-utils") - -local function getsites() - -- Create a table with the content of `Ferron.config.paths.sites` - for site in path.each(Ferron.config.paths.sites .. "/*", "f", {delay = true; reverse = true;}) do - if path.isdir(site) then - -- Load site configuration - local siteconfig, siteconfig_err = fileutils.loadlocalconfig(site .. "/site.config.lua") - - -- Create a table with the basic infos of the site - Ferron.sites[site:match("^.+/(.+)$")] = { - path = site, - config = siteconfig, - pagestable = {} - } - - -- Create a simple array with the name of the sites - Ferron.sitesarray[#Ferron.sitesarray+1] = site:match("^.+/(.+)$") - - end - end - - return Ferron.sites, Ferron.sitesarray -end - -return getsites diff --git a/ferron/list.lua b/ferron/list.lua @@ -0,0 +1,126 @@ +-- +local json = require("dkjson") +local markdown = require("markdown") +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 static = require("ferron.static") +local list = {} + +local function sortentries(entries_tb, list_mt) + local list_length = list_mt.length ~= nil and list_mt.length or nil + local entries_count = list_length ~= nil and 0 or nil + + if entries_count ~= nil 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 contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT + local entries_tb = {} + + path.each( + directory .. "/*.md", + function(entry) + if path.basename(entry) ~= "index.md" or entry == contentpath .. "/index.md" then + local entry_noextension = entry:match("(.+)%..*") + local entry_relpath = fileutils.getrelpath(entry_noextension) + local entry_metadatas = json.decode(fileutils.pullfilecontent(entry_noextension .. ".json")) + local entry_key = entry_metadatas.date .. "|" .. entry_metadatas.datetime .. "|" .. entry_relpath + + entries_tb[entry_key] = Ferron.site.pagestable[entry_key] + end + end, + { + delay = true; -- use snapshot of directory + recurse = true; -- include subdirs + reverse = false; -- subdirs at first + } + ) + + sortentries(entries_tb, list_mt) + + return list_mt["entries"] +end + +function list.makelist() + local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT + + path.each( + contentpath .. "/*.md", + function(list) + if path.basename(list) == "index.md" and list ~= contentpath .. "/index.md" then + local list_noextension = list:match("(.+)%..*") + local list_plainname = list_noextension:match("^.+/(.+)$") + local list_dir = path.dirname(list) + local list_relpath = fileutils.getrelpath(list_noextension) + local list_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(path.dirname(list))) + local list_conf = json.decode(fileutils.pullfilecontent(list_noextension .. ".json")) + local list_conf_mt = setmetatable({}, { __index = list_conf }) + + print("§ Make a list page for the `" .. fileutils.getrelpath(list_dir) .. "` 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.SITE.BASEURL .. path.dirname(list_relpath) + -- Import site configuration in the metatable + list_conf_mt["SITE"] = Ferron.site.config.SITE + -- Set a table for the list of entries + list_conf_mt["entries"] = {} + + -- Set and get entries of the list + setentries(list_dir, list_conf_mt) + + if path.isdir(list_htmlpath) == false then + fileutils.mkdir(list_htmlpath) + end + + static.dispatch(list) + + -- Build the HTML file + fileutils.pushfilecontent( + list_htmlpath .. "/" .. list_plainname .. ".html", + templateutils.setmustache( + fileutils.pullfilecontent(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/" .. list_conf.template .. ".mustache"), + Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", + list_conf_mt + ) + ) + + if path.isfile(list_htmlpath .. "/" .. list_plainname .. ".html") == false then + print(" !! " .. list_relpath .. ".html - error!") + end + + print("==========") + + -- Update the JSON file data + list_conf["entries"] = nil + list_conf["SITE"] = nil + + fileutils.pushfilecontent(list_noextension .. ".json", json.encode(list_conf, {indent = true, keyorder = {"bridgy","cite","citeurl","content","date","datetime","description","id","keywords","permalink","section","template","title","updated"}})) + end + end, + { + delay = true; -- use snapshot of directory + recurse = true; -- include subdirs + reverse = false; -- subdirs at first + } + ) +end + +return list diff --git a/ferron/make-atom-feed.lua b/ferron/make-atom-feed.lua @@ -1,53 +0,0 @@ --- -local path = require("path") -local fileutils = require("ferron.file-utils") -local tableutils = require("ferron.table-utils") -local templateutils = require("ferron.template-utils") - -local function makeatomfeed() - local feed = {} - local feedtemplate = path.isfile(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/feed.mustache") - local feedrsstemplate = path.isfile(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/rss2.mustache") - local feedpartials = path.isdir(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials") - local feedfile = Ferron.site.path .. Ferron.site.config.SITE.PATHS.HTML .. "/feed.atom.xml" - local feedrssfile = Ferron.site.path .. Ferron.site.config.SITE.PATHS.HTML .. "/feed.rss.xml" - - print("§ Make an ATOM feed with all the pages of the site ...") - - feed["SITE"] = Ferron.site.config.SITE - feed["entries"] = {} - - for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do - table.insert(feed["entries"], v) - end - - feed["lastupdate"] = feed["entries"][1].updated - - -- Build the Atom XML file - fileutils.pushfilecontent( - feedfile, - templateutils.setmustache( - fileutils.pullfilecontent(feedtemplate), - feedpartials, - feed - ) - ) - - -- Build the RSS 2 XML file - fileutils.pushfilecontent( - feedrssfile, - templateutils.setmustache( - fileutils.pullfilecontent(feedrsstemplate), - feedpartials, - feed - ) - ) - - if path.isfile(feedfile) == false then - print(" !! /feed.atom.xml - error!") - end - - print("==========") -end - -return makeatomfeed diff --git a/ferron/make-lists-of-pages.lua b/ferron/make-lists-of-pages.lua @@ -1,123 +0,0 @@ --- -local json = require("dkjson") -local markdown = require("markdown") -local path = require("path") -local fileutils = require("ferron.file-utils") -local tableutils = require("ferron.table-utils") -local templateutils = require("ferron.template-utils") -local static = require("ferron.static") - -local function makelistsofpages() - local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT - - path.each( - contentpath .. "/*.md", - function(listindex) - if path.basename(listindex) == "index.md" and listindex ~= contentpath .. "/index.md" then - local listindex = listindex - local listindex_noextension = listindex:match("(.+)%..*") - local listindex_plainname = listindex_noextension:match("^.+/(.+)$") - local listindex_dir = path.dirname(listindex) - local listindex_relpath = fileutils.getrelpath(listindex_noextension) - local listindex_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(path.dirname(listindex))) - local listindex_section = path.dirname(listindex):match("^.+/(.+)$") - local listindex_metadatas = json.decode(fileutils.pullfilecontent(listindex_noextension .. ".json")) - local listindex_metadatas_mt = setmetatable({}, { __index = listindex_metadatas }) - local listindex_length = listindex_metadatas_mt.length ~= nil and listindex_metadatas_mt.length or nil - local pagecounter = listindex_length ~= nil and 0 or nil - - print("§ Make an index page for the `" .. fileutils.getrelpath(listindex_dir) .. "` section ...") - - -- Convert the markdown file to HTML - listindex_metadatas_mt.content = markdown(fileutils.pullfilecontent(listindex)) - -- Set a dynamic permalink - listindex_metadatas_mt.permalink = Ferron.site.config.SITE.BASEURL .. path.dirname(listindex_relpath) - -- Import site configuration in the metatable - listindex_metadatas_mt["SITE"] = Ferron.site.config.SITE - -- Set a table for the list of entries - listindex_metadatas_mt["entries"] = {} - - local function setlistindexentries() - local listindex_tmp_tab = {} - - path.each( - listindex_dir .. "/*.md", - function(md) - if path.basename(md) ~= "index.md" or md == contentpath .. "/index.md" then - local md_noextension = md:match("(.+)%..*") - local md_relpath = fileutils.getrelpath(md_noextension) - local md_section = md_relpath:match("/(%a-)/") - local md_metadatas = json.decode(fileutils.pullfilecontent(md_noextension .. ".json")) - local md_key = md_metadatas.date .. "|" .. md_metadatas.datetime .. "|" .. (md_section ~= nil and md_section or "root") .. "|" .. md_relpath - - listindex_tmp_tab[md_key] = Ferron.site.pagestable[md_key] - end - end, - { - delay = true; -- use snapshot of directory - recurse = true; -- include subdirs - reverse = false; -- subdirs at first - } - ) - - local function sortentries() - if pagecounter ~= nil then - for k, v in tableutils.sortdescendingpairs(listindex_tmp_tab) do - if pagecounter < listindex_length then - table.insert(listindex_metadatas_mt["entries"], v) - - pagecounter = pagecounter + 1 - end - end - else - for k, v in tableutils.sortdescendingpairs(listindex_tmp_tab) do - table.insert(listindex_metadatas_mt["entries"], v) - end - end - end - - sortentries() - - return listindex_metadatas_mt["entries"] - end - - setlistindexentries() - - if path.isdir(listindex_htmlpath) == false then - fileutils.mkdir(listindex_htmlpath) - end - - static.dispatch(listindex) - - -- Build the HTML file - fileutils.pushfilecontent( - listindex_htmlpath .. "/" .. listindex_plainname .. ".html", - templateutils.setmustache( - fileutils.pullfilecontent(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/" .. listindex_metadatas.template .. ".mustache"), - Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", - listindex_metadatas_mt - ) - ) - - if path.isfile(listindex_htmlpath .. "/" .. listindex_plainname .. ".html") == false then - print(" !! " .. listindex_relpath .. ".html - error!") - end - - print("==========") - - -- Update the JSON file data - listindex_metadatas["entries"] = nil - listindex_metadatas["SITE"] = nil - - fileutils.pushfilecontent(listindex_noextension .. ".json", json.encode(listindex_metadatas, {indent = true, keyorder = {"bridgy","cite","citeurl","content","date","datetime","description","id","keywords","permalink","section","template","title","updated"}})) - end - end, - { - delay = true; -- use snapshot of directory - recurse = true; -- include subdirs - reverse = false; -- subdirs at first - } - ) -end - -return makelistsofpages diff --git a/ferron/make-pages.lua b/ferron/make-pages.lua @@ -1,88 +0,0 @@ --- -local json = require("dkjson") -local markdown = require("markdown") -local path = require("path") -local fileutils = require("ferron.file-utils") -local tableutils = require("ferron.table-utils") -local templateutils = require("ferron.template-utils") - -local function makepages() - local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT - local datapath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.DATA - local pagesrelpath = {} - local shortlinks = {} - - -- Loop in the content directory - print("- Looking for markdown in " .. contentpath .. " ...") - print(" ¬ Then make the HTML pages of the site ...") - - path.each( - contentpath .. "/*.md", - function(md) - if path.basename(md) ~= "index.md" or md == contentpath .. "/index.md" then - local md = md - local md_noextension = md:match("(.+)%..*") - local md_plainname = md_noextension:match("^.+/(.+)$") - local md_relpath = fileutils.getrelpath(md_noextension) - local md_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(path.dirname(md))) - local md_section = md_relpath:match("/(%a-)/") - local md_metadatas = json.decode(fileutils.pullfilecontent(md_noextension .. ".json")) - local md_metadatas_mt = setmetatable({}, { __index = md_metadatas }) - local md_key = md_metadatas.date .. "|" .. md_metadatas.datetime .. "|" .. (md_section ~= nil and md_section or "root") .. "|" .. md_relpath - - md_metadatas.updated = os.date("%Y-%m-%dT%H:%M:%S", path.mtime(md)) - md_metadatas.id = "tag:" .. Ferron.site.config.SITE.DOMAINNAME .. "," .. md_metadatas.date .. ":" .. string.sub(md_metadatas.date, 0, 4) .. "/" .. string.sub(md_metadatas.date, 6, 7) .. "/" .. md_plainname - - -- Convert the markdown file to HTML - -- And put it in a metatable - md_metadatas_mt.content = markdown(fileutils.pullfilecontent(md)) - - md_metadatas_mt.permalink = Ferron.site.config.SITE.BASEURL .. (md_plainname ~= "index" and md_relpath .. ".html" or "") - md_metadatas_mt.section = md_section - md_metadatas_mt["SITE"] = Ferron.site.config.SITE - - Ferron.site.pagestable[md_key] = md_metadatas_mt - - if path.isdir(md_htmlpath) == false then - fileutils.mkdir(md_htmlpath) - end - - -- Build the HTML file - fileutils.pushfilecontent( - md_htmlpath .. "/" .. md_plainname .. ".html", - templateutils.setmustache( - fileutils.pullfilecontent(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/" .. md_metadatas.template .. ".mustache"), - Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", - md_metadatas_mt - ) - ) - - if path.isfile(md_htmlpath .. "/" .. md_plainname .. ".html") == false then - print(" !! " .. md_relpath .. ".html - error!") - end - - --pagesrelpath[#pagesrelpath +1] = md_relpath - - -- Update the JSON file data - fileutils.pushfilecontent(md_noextension .. ".json", json.encode(md_metadatas, {indent = true, keyorder = {"bridgy","cite","citeurl","content","date","datetime","description","id","keywords","permalink","section","template","title","updated"}})) - end - end, - { - delay = true; -- use snapshot of directory - recurse = true; -- include subdirs - reverse = true; -- subdirs at first - } - ) - - -- for k, v in pairs(pagesrelpath) do - -- shortlinks[v] = fileutils.shorturlencode(k) - -- end - - -- print(inspect(shortlinks)) - - fileutils.pushfilecontent(datapath .. "/shortlinks.json", json.encode(shortlinks, {indent = true})) - - print("==========") -end - -return makepages diff --git a/ferron/page.lua b/ferron/page.lua @@ -0,0 +1,74 @@ +-- +local json = require("dkjson") +local markdown = require("markdown") +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 page = {} + +function page.makepage() + local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT + + -- Loop in the content directory + print("- Looking for markdown in " .. contentpath .. " ...") + print(" ¬ Then make the HTML pages of the site ...") + + path.each( + contentpath .. "/*.md", + function(page) + if path.basename(page) ~= "index.md" or page == contentpath .. "/index.md" then + local page_noextension = page:match("(.+)%..*") + local page_plainname = page_noextension:match("^.+/(.+)$") + local page_relpath = fileutils.getrelpath(page_noextension) + local page_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(path.dirname(page))) + local page_conf = json.decode(fileutils.pullfilecontent(page_noextension .. ".json")) + local page_conf_mt = setmetatable({}, { __index = page_conf }) + local page_key = page_conf.date .. "|" .. page_conf.datetime .. "|" .. page_relpath + + page_conf.updated = os.date("%Y-%m-%dT%H:%M:%S", path.mtime(page)) + page_conf.id = "tag:" .. Ferron.site.config.SITE.DOMAINNAME .. "," .. page_conf.date .. ":" .. string.sub(page_conf.date, 0, 4) .. "/" .. string.sub(page_conf.date, 6, 7) .. "/" .. page_plainname + + -- Convert the markdown file to HTML + -- And put it in a metatable + page_conf_mt.content = markdown(fileutils.pullfilecontent(page)) + + page_conf_mt.permalink = Ferron.site.config.SITE.BASEURL .. (page_plainname ~= "index" and page_relpath .. ".html" or "") + page_conf_mt.section = nil + page_conf_mt["SITE"] = Ferron.site.config.SITE + + Ferron.site.pagestable[page_key] = page_conf_mt + + if path.isdir(page_htmlpath) == false then + fileutils.mkdir(page_htmlpath) + end + + -- Build the HTML file + fileutils.pushfilecontent( + page_htmlpath .. "/" .. page_plainname .. ".html", + templateutils.setmustache( + fileutils.pullfilecontent(Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/" .. page_conf.template .. ".mustache"), + Ferron.site.path .. Ferron.site.config.SITE.PATHS.TEMPLATES .. "/partials", + page_conf_mt + ) + ) + + if path.isfile(page_htmlpath .. "/" .. page_plainname .. ".html") == false then + print(" !! " .. page_relpath .. ".html - error!") + end + + -- Update the JSON file data + fileutils.pushfilecontent(page_noextension .. ".json", json.encode(page_conf, {indent = true, keyorder = {"bridgy","cite","citeurl","content","date","datetime","description","id","keywords","permalink","section","template","title","updated"}})) + end + end, + { + delay = true; -- use snapshot of directory + recurse = true; -- include subdirs + reverse = true; -- subdirs at first + } + ) + + print("==========") +end + +return page diff --git a/ferron/set-site.lua b/ferron/set-site.lua @@ -1,28 +0,0 @@ --- -local tableutils = require("ferron.table-utils") - -local function setsite() - -- Site chooser: Which site do you want to build? - local answer - - repeat - io.write("Welcome to Ferron! Which site do you want to build? \n") - - for k, v in ipairs(Ferron.sitesarray) do - io.write(k .. ") " .. v .. "\n") - end - - io.write("Please enter the number... \n") - io.flush() - - answer=io.read() - until (tableutils.haskey(Ferron.sitesarray, tonumber(answer))) == true - - Ferron.site = Ferron.sites[Ferron.sitesarray[tonumber(answer)]] - Ferron.site.config.SITE.BASEURL = (Ferron.devmode == true and Ferron.site.config.SITE.URLDEV or Ferron.site.config.SITE.URL) - Ferron.site.config.SITE.PREFETCHLIST = {} - - return Ferron.site -end - -return setsite diff --git a/ferron/site.lua b/ferron/site.lua @@ -0,0 +1,53 @@ +local path = require("path") +local tableutils = require("ferron.utilities.table-utils") +local fileutils = require("ferron.utilities.file-utils") +local site = {} + +function site.getsites() + -- Create a table with the content of `Ferron.config.paths.sites` + for site in path.each(Ferron.config.paths.sites .. "/*", "f", {delay = true; reverse = true;}) do + if path.isdir(site) then + -- Load site configuration + local siteconfig, siteconfig_err = fileutils.loadlocalconfig(site .. "/site.config.lua") + + -- Create a table with the basic infos of the site + Ferron.sites[site:match("^.+/(.+)$")] = { + path = site, + config = siteconfig, + pagestable = {} + } + + -- Create a simple array with the name of the sites + Ferron.sitesarray[#Ferron.sitesarray+1] = site:match("^.+/(.+)$") + + end + end + + return Ferron.sites, Ferron.sitesarray +end + +function site.setsite() + -- Site chooser: Which site do you want to build? + local answer + + repeat + io.write("Welcome to Ferron! Which site do you want to build? \n") + + for k, v in ipairs(Ferron.sitesarray) do + io.write(k .. ") " .. v .. "\n") + end + + io.write("Please enter the number... \n") + io.flush() + + answer=io.read() + until (tableutils.haskey(Ferron.sitesarray, tonumber(answer))) == true + + Ferron.site = Ferron.sites[Ferron.sitesarray[tonumber(answer)]] + Ferron.site.config.SITE.BASEURL = (Ferron.devmode == true and Ferron.site.config.SITE.URLDEV or Ferron.site.config.SITE.URL) + Ferron.site.config.SITE.PREFETCHLIST = {} + + return Ferron.site +end + +return site diff --git a/ferron/static.lua b/ferron/static.lua @@ -1,8 +1,8 @@ local lfs = require("lfs") local mimetypes = require('mimetypes') local path = require("path") -local fileutils = require("ferron.file-utils") -local tableutils = require("ferron.table-utils") +local fileutils = require("ferron.utilities.file-utils") +local tableutils = require("ferron.utilities.table-utils") local static = {} function static.dispatch(file) diff --git a/ferron/template-utils.lua b/ferron/template-utils.lua @@ -1,20 +0,0 @@ --- -local lustache = require("lustache") -local fileutils = require("ferron.file-utils") - -local templateutils = {} - --- setmustache -function templateutils.setmustache(tpl, partialspath, data) - local partials = {} - - for i,v in ipairs(lustache:parse(tpl)) do - if v.type == ">" then - partials[v.value] = fileutils.pullfilecontent(partialspath .. "/" .. v.value .. ".mustache") - end - end - - return lustache:render(tpl, data, partials) -end - -return templateutils diff --git a/ferron/file-utils.lua b/ferron/utilities/file-utils.lua diff --git a/ferron/table-utils.lua b/ferron/utilities/table-utils.lua diff --git a/ferron/utilities/template-utils.lua b/ferron/utilities/template-utils.lua @@ -0,0 +1,20 @@ +-- +local lustache = require("lustache") +local fileutils = require("ferron.utilities.file-utils") + +local templateutils = {} + +-- setmustache +function templateutils.setmustache(tpl, partialspath, data) + local partials = {} + + for i,v in ipairs(lustache:parse(tpl)) do + if v.type == ">" then + partials[v.value] = fileutils.pullfilecontent(partialspath .. "/" .. v.value .. ".mustache") + end + end + + return lustache:render(tpl, data, partials) +end + +return templateutils diff --git a/rhino b/rhino @@ -1,5 +0,0 @@ -#!/usr/bin/env lua - --- Rhino is the futur CLI of Ferron SSG. - -print("test")