ferron

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

commit 838c86addbe9b9bb5e56118928b6234e9a3130fd
parent f9c5cf627af0c2013f3ddd20525eb0a4e1156991
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Fri,  1 Dec 2017 08:11:22 -0500

Replace `getdirtree` with `path.each`.

Diffstat:
Mfunctions/make-lists-of-pages.lua | 110++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mfunctions/make-pages.lua | 96++++++++++++++++++++++++++++++++++++++++++-------------------------------------
2 files changed, 110 insertions(+), 96 deletions(-)

diff --git a/functions/make-lists-of-pages.lua b/functions/make-lists-of-pages.lua @@ -9,70 +9,78 @@ local templateutils = require("functions.template-utils") local dispatchnontextuals = require("functions.dispatch-nontextuals") local function makelistsofpages() - for file, attr in fileutils.getdirtree(Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT) do - if attr.mode ~= "directory" - and file:match("^.+/(.+)$") == "index.md" - and file ~= Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT .. "/index.md" - then - local listindex = file - local listindex_noextension = listindex:match("(.+)%..*") - local listindex_plainname = listindex_noextension:match("^.+/(.+)$") - local listindex_relpath = ferronutils.getrelpath(listindex_noextension) - local listindex_htmlpath = ferronutils.sethtmlpath(ferronutils.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 contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT - print("§ Make an index page for the `" .. listindex_section .. "` section:") + 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_relpath = ferronutils.getrelpath(listindex_noextension) + local listindex_htmlpath = ferronutils.sethtmlpath(ferronutils.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 }) - -- 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"] = {} + print("§ Make an index page for the `" .. listindex_section .. "` section:") - for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do - local date, datetime, section, relpath = k:match("(.-)|(.-)|(.-)|(.-)$") + -- 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"] = {} - if section == listindex_section or relpath:match("%/"..listindex_section.."%/") then - table.insert(listindex_metadatas_mt["entries"], v) + for k, v in tableutils.sortdescendingpairs(Ferron.site.pagestable) do + local date, datetime, section, relpath = k:match("(.-)|(.-)|(.-)|(.-)$") + + if section == listindex_section or relpath:match("%/"..listindex_section.."%/") then + table.insert(listindex_metadatas_mt["entries"], v) + end end - end - if path.isdir(listindex_htmlpath) == false then - fileutils.mkdir(listindex_htmlpath) - end + if path.isdir(listindex_htmlpath) == false then + fileutils.mkdir(listindex_htmlpath) + end - dispatchnontextuals(listindex, listindex_section) + dispatchnontextuals(listindex, listindex_section) - -- 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 + -- 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 - created") - else - print(" !! " .. listindex_relpath .. ".html - error!") - end + if path.isfile(listindex_htmlpath .. "/" .. listindex_plainname .. ".html") ~= false then + print(" ¬ " .. listindex_relpath .. ".html - created") + else + print(" !! " .. listindex_relpath .. ".html - error!") + end - print("==========") + print("==========") - -- Update the JSON file data - listindex_metadatas["entries"] = nil - listindex_metadatas["SITE"] = nil + -- 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 + 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 + } + ) + print("==========") end return makelistsofpages diff --git a/functions/make-pages.lua b/functions/make-pages.lua @@ -8,62 +8,68 @@ local tableutils = require("functions.table-utils") local templateutils = require("functions.template-utils") local function makepages() + local contentpath = Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT + -- Loop in the content directory - print("- Looking for markdown in " .. Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT) + print("- Looking for markdown in " .. contentpath) - for file, attr in fileutils.getdirtree(Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT) do - if attr.mode ~= "directory" - and file:match("^.+(%..+)$") == ".md" - and file:match("^.+/(.+)$") ~= "index.md" - or file == Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT .. "/index.md" - then - local md = file - local md_noextension = md:match("(.+)%..*") - local md_plainname = md_noextension:match("^.+/(.+)$") - local md_relpath = ferronutils.getrelpath(md_noextension) - local md_htmlpath = ferronutils.sethtmlpath(ferronutils.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 + 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 = ferronutils.getrelpath(md_noextension) + local md_htmlpath = ferronutils.sethtmlpath(ferronutils.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", attr.modification) - 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 + 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)) + -- 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 + 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 + Ferron.site.pagestable[md_key] = md_metadatas_mt - if path.isdir(md_htmlpath) == false then - fileutils.mkdir(md_htmlpath) - end + 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 + -- 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 - created") - else - print(" !! " .. md_relpath .. ".html - error!") - end + if path.isfile(md_htmlpath .. "/" .. md_plainname .. ".html") ~= false then + print(" ¬ " .. md_relpath .. ".html - created") + else + print(" !! " .. md_relpath .. ".html - error!") + end - -- 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 + -- 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 = false; -- subdirs at first + } + ) print("==========") end