ferron

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

commit 0eb51d91d2e4b3cbcb1ec43b901a61b9bb00e67a
parent 766aa524671d55054e160b6de2b3abf540c0347e
Author: Hugo Soucy <hsoucy@kronostechnologies.com>
Date:   Wed, 30 Jan 2019 13:24:53 -0500

Return to the Ferron.site global

Diffstat:
Mferron.lua | 29+++++++++++++++++------------
Mferron/content.lua | 14+++++++-------
Mferron/feed.lua | 7+++----
Mferron/link.lua | 11+++++------
Mferron/list.lua | 22++++++++++------------
Mferron/page.lua | 27+++++++++++++--------------
Mferron/plugin.lua | 2+-
Mferron/site.lua | 49+++++++++++++++++++++----------------------------
Mferron/static.lua | 10++++------
Mferron/tag.lua | 14+++++++-------
Mferron/utilities/file-utils.lua | 8++++----
Mferron/utilities/template-utils.lua | 4++--
12 files changed, 94 insertions(+), 103 deletions(-)

diff --git a/ferron.lua b/ferron.lua @@ -2,19 +2,10 @@ -- Ferron SSG -- --- App's Globals --- Init the main namespace -Ferron = {} --- Set if the build need to be in devmode -Ferron.devmode = (arg[1] == "--dev" and true or false) - -if Ferron.devmode == true then - inspect = require "inspect" -end - +-- Required Modules inspect = require "inspect" - --- Ferron's Modules +local lfs = require "lfs" +local config = require "ferron.config" local site = require "ferron.site" local content = require "ferron.content" local page = require "ferron.page" @@ -24,6 +15,20 @@ local static = require "ferron.static" local plugin = require "ferron.plugin" local exec = require "ferron.utilities.exec" +-- App's Globals +-- Init the main namespace +Ferron = {} +-- Set if the build need to be in devmode +Ferron.devmode = (arg[1] == "--dev" and true or false) +-- Chosen site to build must be global +Ferron.site = { + location = lfs.currentdir() .. "/" .. config.sites, + config = nil, + path = nil, + pagestable = {}, + navigation = {}, +} + Ferron.init = { site.setsite, site.sitereset, diff --git a/ferron/content.lua b/ferron/content.lua @@ -7,14 +7,14 @@ local fileutils = require "ferron.utilities.file-utils" local slugify = require "ferron.utilities.lua-slugify.slugify" local tableutils = require "ferron.utilities.table-utils" local templateutils = require "ferron.utilities.template-utils" -local site = require "ferron.site" + -- Module Declaration local content = {} function content.getarchetypes(dir) local archetypes = {} - local archetypesdir = (dir ~= nil and dir or site.archetypes) + local archetypesdir = (dir ~= nil and dir or Ferron.site.archetypes) if fileutils.isDirectory(archetypesdir) then for archetype, attr in fileutils.getdirtree(archetypesdir) do @@ -30,7 +30,7 @@ function content.getarchetypes(dir) end function content.setcontent() - local archetypes = content.getarchetypes(site.archetypes) + local archetypes = content.getarchetypes(Ferron.site.archetypes) local whicharchetype = nil local whichtitle = nil @@ -63,8 +63,8 @@ function content.makecontent(contenttype, contenttitle) if tableutils.hasvalue(content.getarchetypes(), archetypetype) and type(title) == "string" then local archetype = {} - local contentpath = site.content .. "/" .. archetypetype .. "/" .. os.date("%Y") .."/"..os.date("%m") .. "/" - local archetypepath = site.archetypes .. "/" + local contentpath = Ferron.site.content .. "/" .. archetypetype .. "/" .. os.date("%Y") .."/"..os.date("%m") .. "/" + local archetypepath = Ferron.site.archetypes .. "/" archetype.title = title archetype.filename = slugify(archetype.title) @@ -81,7 +81,7 @@ function content.makecontent(contenttype, contenttitle) contentpath .. archetype.filename .. ".md", templateutils.rendermustache( fileutils.pullfilecontent(archetypepath .. archetypetype .. ".md" ), - site.templates .. "/partials", + Ferron.site.templates .. "/partials", archetype ) ) @@ -91,7 +91,7 @@ function content.makecontent(contenttype, contenttitle) contentpath .. archetype.filename .. ".json", templateutils.rendermustache( fileutils.pullfilecontent(archetypepath .. archetypetype .. ".json"), - site.templates .. "/partials", + Ferron.site.templates .. "/partials", archetype ) ) diff --git a/ferron/feed.lua b/ferron/feed.lua @@ -2,15 +2,14 @@ local fileutils = require "ferron.utilities.file-utils" local tableutils = require "ferron.utilities.table-utils" local templateutils = require "ferron.utilities.template-utils" -local site = require "ferron.site" local feed = {} function feed.makefeed(entries, destination) - local feedtypes = site.config.feedtypes - local feedpartials = fileutils.isDirectory(site.templates .. "/partials") + local feedtypes = Ferron.site.config.feedtypes + local feedpartials = fileutils.isDirectory(Ferron.site.templates .. "/partials") for k, v in pairs(feedtypes) do - local feedtemplate = site.templates .. "/feed/" .. k .. ".mustache" + local feedtemplate = Ferron.site.templates .. "/feed/" .. k .. ".mustache" if fileutils.isFile(feedtemplate) then fileutils.pushfilecontent( diff --git a/ferron/link.lua b/ferron/link.lua @@ -3,13 +3,12 @@ local json = require "dkjson" local fileutils = require "ferron.utilities.file-utils" local tableutils = require "ferron.utilities.table-utils" local templateutils = require "ferron.utilities.template-utils" -local site = require "ferron.site" local link = {} function link.makeshorts() local links = {} local links_keyorder = {} - local links_db = site.data .. "/shortlinks.json" + local links_db = Ferron.site.data .. "/shortlinks.json" -- If `links_db` dosen't exists create it if fileutils.isFile(links_db) == false then @@ -17,7 +16,7 @@ function link.makeshorts() fileutils.pushfilecontent(links_db, "") - for k, v in tableutils.sortdescendingpairs(site.pagestable) do + for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do if v.rellink ~= "" then counter = counter + 1 @@ -31,7 +30,7 @@ function link.makeshorts() else links = json.decode(fileutils.pullfilecontent(links_db)) - for k, v in tableutils.sortdescendingpairs(site.pagestable) do + for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do if v.rellink ~= "" and links[v.rellink] == nil then table.insert(links_keyorder, v.rellink) @@ -44,8 +43,8 @@ function link.makeshorts() end function link.makerewritemap() - local links_db = site.data .. "/shortlinks.json" - local links_map = site.html .. "/shortlinksmap.txt" + local links_db = Ferron.site.data .. "/shortlinks.json" + local links_map = Ferron.site.html .. "/shortlinksmap.txt" if fileutils.isFile(links_db) then links = json.decode(fileutils.pullfilecontent(links_db)) diff --git a/ferron/list.lua b/ferron/list.lua @@ -6,8 +6,6 @@ local tableutils = require "ferron.utilities.table-utils" local templateutils = require "ferron.utilities.template-utils" local feed = require "ferron.feed" local static = require "ferron.static" -local site = require "ferron.site" - local list = {} local function sortentries(entries_tb, list_mt) @@ -37,9 +35,9 @@ local function setentries(directory, list_mt) tableutils.each( function(entry) local entry_metadatas = fileutils.getpageconf(entry) - local entry_key = entry_metadatas.date .. "|" .. entry_metadatas.datetime .. "|" .. fileutils.getrelpath(fileutils.removeextension(entry), site) + local entry_key = entry_metadatas.date .. "|" .. entry_metadatas.datetime .. "|" .. fileutils.getrelpath(fileutils.removeextension(entry)) - entries_tb[entry_key] = site.pagestable[entry_key] + entries_tb[entry_key] = Ferron.site.pagestable[entry_key] end, tableutils.filter(fileutils.isNotIndex, tableutils.settable(fileutils.getdirtree(directory))) ) @@ -51,20 +49,20 @@ end function list.render(listsource) local list = listsource - local list_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(fileutils.getdirname(list), site), site) + 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), site) .. "` subdirectory ...") + 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 = site.config.baseurl .. fileutils.getdirname(fileutils.getrelpath(fileutils.removeextension(list), site)) + 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"] = site.config + list_conf_mt["site"] = Ferron.site.config -- Import site navigation in the metatable - list_conf_mt["navigation"] = site.navigation + list_conf_mt["navigation"] = Ferron.site.navigation -- Set a table for the list of entries list_conf_mt["entries"] = {} @@ -89,15 +87,15 @@ function list.render(listsource) fileutils.pushfilecontent( list_htmlpath .. "/" .. fileutils.getplainname(list) .. ".html", templateutils.rendertemplate( - templateutils.selecttemplate(false, list_conf.template, site), + templateutils.selecttemplate(false, list_conf.template), list_conf_mt, - site.templates .. "/partials" + Ferron.site.templates .. "/partials" ) ) end function list.init() - local contentpath = assert(fileutils.isDirectory(site.content)) + local contentpath = assert(fileutils.isDirectory(Ferron.site.content)) tableutils.each(list.render, tableutils.filter(fileutils.isIndex, tableutils.settable(fileutils.getdirtree(contentpath)))) diff --git a/ferron/page.lua b/ferron/page.lua @@ -4,29 +4,28 @@ 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 site = require "ferron.site" local page = {} function page.render(pagesource) local page = pagesource - local page_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(fileutils.getdirname(page), site), site) + local page_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(fileutils.getdirname(page))) local page_conf = fileutils.getpageconf(page) local page_conf_mt = setmetatable({}, { __index = page_conf }) - local page_key = page_conf.date .. "|" .. page_conf.datetime .. "|" .. fileutils.getrelpath(fileutils.removeextension(page), site) + local page_key = page_conf.date .. "|" .. page_conf.datetime .. "|" .. fileutils.getrelpath(fileutils.removeextension(page)) -- Set some dynamic configuration properties page_conf_mt.content = markdown(fileutils.pullfilecontent(page)) page_conf_mt.updated = os.date("%Y-%m-%dT%H:%M:%S", lfs.attributes(page).modification) - page_conf_mt.permalink = site.config.baseurl .. (fileutils.getplainname(page) ~= "index" and fileutils.getrelpath(fileutils.removeextension(page), site) .. ".html" or "") - page_conf_mt.rellink = (fileutils.getplainname(page) ~= "index" and fileutils.getrelpath(fileutils.removeextension(page), site) .. ".html" or "") - page_conf_mt["site"] = site.config - page_conf_mt["navigation"] = site.navigation + page_conf_mt.permalink = Ferron.site.config.baseurl .. (fileutils.getplainname(page) ~= "index" and fileutils.getrelpath(fileutils.removeextension(page)) .. ".html" or "") + page_conf_mt.rellink = (fileutils.getplainname(page) ~= "index" and fileutils.getrelpath(fileutils.removeextension(page)) .. ".html" or "") + page_conf_mt["site"] = Ferron.site.config + page_conf_mt["navigation"] = Ferron.site.navigation if page_conf.id == nil then - page_conf_mt.id = "tag:" .. site.config.domainname .. "," .. page_conf.date .. ":" .. string.sub(page_conf.date, 0, 4) .. "/" .. string.sub(page_conf.date, 6, 7) .. "/" .. fileutils.getplainname(page) + page_conf_mt.id = "tag:" .. Ferron.site.config.domainname .. "," .. page_conf.date .. ":" .. string.sub(page_conf.date, 0, 4) .. "/" .. string.sub(page_conf.date, 6, 7) .. "/" .. fileutils.getplainname(page) end - site.pagestable[page_key] = page_conf_mt + Ferron.site.pagestable[page_key] = page_conf_mt -- If the ancestors of the page dosen't exists make it if not fileutils.isDirectory(page_htmlpath) then @@ -37,9 +36,9 @@ function page.render(pagesource) fileutils.pushfilecontent( page_htmlpath .. "/" .. fileutils.getplainname(page) .. ".html", templateutils.rendertemplate( - templateutils.selecttemplate(false, page_conf.template, site), + templateutils.selecttemplate(false, page_conf.template), page_conf_mt, - assert(fileutils.isDirectory(site.templates)) .. "/partials" + assert(fileutils.isDirectory(Ferron.site.templates)) .. "/partials" ) ) @@ -49,15 +48,15 @@ function page.render(pagesource) end function page.init() - local contentpath = assert(fileutils.isDirectory(site.content)) + local contentpath = assert(fileutils.isDirectory(Ferron.site.content)) tableutils.each(page.render, tableutils.filter(fileutils.isNotIndex, tableutils.settable(fileutils.getdirtree(contentpath)))) print("==========") - print("§ " .. tableutils.length(site.pagestable) .. " HTML pages have been created.") + print("§ " .. tableutils.length(Ferron.site.pagestable) .. " HTML pages have been created.") print("==========") - return --print(inspect(site.pagestable)) + return --print(inspect(Ferron.site.pagestable)) end return page diff --git a/ferron/plugin.lua b/ferron/plugin.lua @@ -5,7 +5,7 @@ local fileutils = require "ferron.utilities.file-utils" local plugin = {} function plugin.run(site, plugname) - local plugin = require(site.config.sites .. fileutils.getbasename(site.path) .. site.config.paths.plugins .. "/" .. plugname) + local plugin = require(Ferron.site.config.sites .. fileutils.getbasename(Ferron.site.path) .. Ferron.site.config.paths.plugins .. "/" .. plugname) return plugin.init(site) end diff --git a/ferron/site.lua b/ferron/site.lua @@ -2,17 +2,10 @@ local lfs = require "lfs" local config = require "ferron.config" local tableutils = require "ferron.utilities.table-utils" local fileutils = require "ferron.utilities.file-utils" - -local site = { - location = lfs.currentdir() .. "/" .. config.sites, - config = nil, - path = nil, - pagestable = {}, - navigation = {}, -} +local site = {} function site.getsitelist(siteslocation) - local location = siteslocation or site.location + local location = siteslocation or Ferron.site.location -- Create a simple array with the directory name of the sites local siteslist = tableutils.map( function(site, i) @@ -59,34 +52,34 @@ function site.setsite(sitename) whichsite = 1 end - -- Assign values to site.path and site.config - 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)]))) + -- Assign values to Ferron.site.path and Ferron.site.config + Ferron.site.path = Ferron.site.location .. (sitename ~= nil and sitename or siteslist[tonumber(whichsite)]) + Ferron.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) + -- 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) - -- Create absolute path properties for site's folders - site.archetypes = site.path .. site.config.paths.archetypes - site.content = site.path .. site.config.paths.content - site.data = site.path .. site.config.paths.data - site.html = site.path .. site.config.paths.html - site.plugins = site.path .. site.config.paths.plugins - site.static = site.path .. site.config.paths.static - site.templates = site.path .. site.config.paths.templates + -- 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.templates = Ferron.site.path .. Ferron.site.config.paths.templates return true end function site.setnavigation(contentlocation) - local location = assert(fileutils.isDirectory(contentlocation or site.content)) + local location = assert(fileutils.isDirectory(contentlocation or Ferron.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 = site.config.baseurl .. fileutils.removeextension(fileutils.getrelpath(f, site)) .. ".html" + local location = Ferron.site.config.baseurl .. fileutils.removeextension(fileutils.getrelpath(f)) .. ".html" local attributes = meta.navigation.attributes local order = meta.navigation.order @@ -123,7 +116,7 @@ function site.setnavigation(contentlocation) return _tbl end - return tableutils.extend(site.navigation, sortnavigation(navigation)) + return tableutils.extend(Ferron.site.navigation, sortnavigation(navigation)) end function site.startsite(samplelocation) @@ -132,7 +125,7 @@ function site.startsite(samplelocation) name = nil, location = nil, config = nil, - sample = site.location .. "ferron-ssg.tld", + sample = Ferron.site.location .. "ferron-ssg.tld", } local location = samplelocation or newsite.sample @@ -141,7 +134,7 @@ function site.startsite(samplelocation) io.flush() newsite.name = io.read() - newsite.location = site.location .. newsite.name + newsite.location = Ferron.site.location .. newsite.name fileutils.mkdir(newsite.location) @@ -156,7 +149,7 @@ end -- Reset the `public_html/` folder of the selected site function site.sitereset(htmllocation) - local location = htmllocation or site.html + local location = htmllocation or Ferron.site.html local function removefiles(dir) local ok, errormsg -- remove files from directory diff --git a/ferron/static.lua b/ferron/static.lua @@ -5,22 +5,20 @@ -- Required Packages local fileutils = require "ferron.utilities.file-utils" local tableutils = require "ferron.utilities.table-utils" -local site = require "ferron.site" - local static = {} local function dispatchnontextual(file) - return os.execute("cp -p " .. file .. " " .. site.html) + return os.execute("cp -p " .. file .. " " .. Ferron.site.html) end local function movestaticfolder() - return os.execute("cp -Rp " .. site.static .. "/*" .. " " .. site.html) + return os.execute("cp -Rp " .. Ferron.site.static .. "/*" .. " " .. Ferron.site.html) end function static.init() - local contentpath = assert(fileutils.isDirectory(site.content)) + local contentpath = assert(fileutils.isDirectory(Ferron.site.content)) - print("¤¤ Your site is ready at `" .. site.html .. "` ¤¤") + print("¤¤ Your site is ready at `" .. Ferron.site.html .. "` ¤¤") return movestaticfolder(), tableutils.each(dispatchnontextual, tableutils.filter(fileutils.isNonTextual, tableutils.settable(fileutils.getdirtree(contentpath)))) diff --git a/ferron/tag.lua b/ferron/tag.lua @@ -3,12 +3,12 @@ local json = require "dkjson" local fileutils = require "ferron.utilities.file-utils" local tableutils = require "ferron.utilities.table-utils" local templateutils = require "ferron.utilities.template-utils" -local site = require "ferron.site" + local tag = {} local function maketagstable() local tagstable = {} - local pages = site.pagestable + local pages = Ferron.site.pagestable for k, page in pairs(pages) do if page.keywords ~= nil then @@ -42,16 +42,16 @@ function tag.maketagpage() tagurls = urls } - if fileutils.isDirectory(site.html .. "/tag") == false then - fileutils.mkdir(site.html .. "/tag") + if fileutils.isDirectory(Ferron.site.html .. "/tag") == false then + fileutils.mkdir(Ferron.site.html .. "/tag") end -- Build the HTML file fileutils.pushfilecontent( - site.html .. "/tag/" .. tag .. ".html", + Ferron.site.html .. "/tag/" .. tag .. ".html", templateutils.rendermustache( - fileutils.pullfilecontent(site.templates .. "/tag.mustache"), - site.templates .. "/partials", + fileutils.pullfilecontent(Ferron.site.templates .. "/tag.mustache"), + Ferron.site.templates .. "/partials", tag_tb ) ) diff --git a/ferron/utilities/file-utils.lua b/ferron/utilities/file-utils.lua @@ -125,12 +125,12 @@ function fileutils.pushfilecontent(pathtofile, data) return assert(fileutils.isFile(pathtofile)) end -function fileutils.getrelpath(file, site) - return file:sub((site.path .. site.config.paths.content):len() + 1) +function fileutils.getrelpath(file) + return file:sub((Ferron.site.path .. Ferron.site.config.paths.content):len() + 1) end -function fileutils.sethtmlpath(folder, site) - return site.path .. site.config.paths.html .. folder +function fileutils.sethtmlpath(folder) + return Ferron.site.path .. Ferron.site.config.paths.html .. folder end function fileutils.removeextension(file) diff --git a/ferron/utilities/template-utils.lua b/ferron/utilities/template-utils.lua @@ -6,8 +6,8 @@ local fileutils = require "ferron.utilities.file-utils" local templateutils = {} -- set template -function templateutils.selecttemplate(index, templatename, site) - local templatepath = site.path .. site.config.paths.templates .. "/" +function templateutils.selecttemplate(index, templatename) + local templatepath = Ferron.site.path .. Ferron.site.config.paths.templates .. "/" local template = nil if templatename ~= nil and type(templatename) == "string" then