ferron

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

commit 0236724c83b0405c951737a396ba65b4738c2d62
parent 11813f83a1d7841448f6214ee0ba7d8688afdf05
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Tue, 28 Aug 2018 16:09:48 -0400

Replace vars by first class functions

Diffstat:
Mferron/list.lua | 29+++++++++++++----------------
Mferron/page.lua | 21+++++++++------------
Mferron/static.lua | 7+++----
Mferron/utilities/file-utils.lua | 8++++++++
4 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/ferron/list.lua b/ferron/list.lua @@ -38,10 +38,8 @@ local function setentries(directory, list_mt) 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 + local entry_metadatas = json.decode(fileutils.pullfilecontent(fileutils.removeextension(entry) .. ".json")) + local entry_key = entry_metadatas.date .. "|" .. entry_metadatas.datetime .. "|" .. fileutils.getrelpath(fileutils.removeextension(entry)) entries_tb[entry_key] = Ferron.site.pagestable[entry_key] end @@ -66,27 +64,23 @@ function list.makelist() 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 = json.decode(fileutils.pullfilecontent(fileutils.removeextension(list) .. ".json")) local list_conf_mt = setmetatable({}, { __index = list_conf }) - print("§ Make a list page for the `" .. fileutils.getrelpath(list_dir) .. "` subdirectory ...") + print("§ Make a list page for the `" .. fileutils.getrelpath(path.dirname(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.siteconfig.baseurl .. path.dirname(list_relpath) + list_conf_mt.permalink = Ferron.site.siteconfig.baseurl .. path.dirname(fileutils.getrelpath(fileutils.removeextension(list))) -- Import site configuration in the metatable list_conf_mt["site"] = Ferron.site.siteconfig -- 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) + setentries(path.dirname(list), list_conf_mt) if not path.isdir(list_htmlpath) then fileutils.mkdir(list_htmlpath) @@ -96,7 +90,7 @@ function list.makelist() -- Build the HTML file fileutils.pushfilecontent( - list_htmlpath .. "/" .. list_plainname .. ".html", + list_htmlpath .. "/" .. fileutils.getplainname(list) .. ".html", templateutils.setmustache( fileutils.pullfilecontent(templatespath .. "/" .. (list_conf.template ~= nil and list_conf.template or "default-index") .. ".mustache"), templatespath .. "/partials", @@ -104,8 +98,8 @@ function list.makelist() ) ) - if not path.isfile(list_htmlpath .. "/" .. list_plainname .. ".html") then - print(" !! " .. list_relpath .. ".html - error!") + if not path.isfile(list_htmlpath .. "/" .. fileutils.getplainname(list) .. ".html") then + print(" !! " .. fileutils.getrelpath(fileutils.removeextension(list)) .. ".html - error!") end print("==========") @@ -114,7 +108,10 @@ function list.makelist() 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","template","title","updated"}})) + fileutils.pushfilecontent( + fileutils.removeextension(list) .. ".json", + json.encode(list_conf, {indent = true, keyorder = {"bridgy","cite","citeurl","content","date","datetime","description","id","keywords","permalink","template","title","updated"}}) + ) end end, { diff --git a/ferron/page.lua b/ferron/page.lua @@ -21,24 +21,21 @@ function page.makepage() 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 = json.decode(fileutils.pullfilecontent(fileutils.removeextension(page) .. ".json")) local page_conf_mt = setmetatable({}, { __index = page_conf }) - local page_key = page_conf.date .. "|" .. page_conf.datetime .. "|" .. page_relpath + local page_key = page_conf.date .. "|" .. page_conf.datetime .. "|" .. fileutils.getrelpath(fileutils.removeextension(page)) local page_template = templatespath .. "/" .. (page_conf.template ~= nil and page_conf.template or "default") .. ".mustache" - page_conf.id = "tag:" .. Ferron.site.siteconfig.domainname .. "," .. page_conf.date .. ":" .. string.sub(page_conf.date, 0, 4) .. "/" .. string.sub(page_conf.date, 6, 7) .. "/" .. page_plainname + page_conf.id = "tag:" .. Ferron.site.siteconfig.domainname .. "," .. page_conf.date .. ":" .. string.sub(page_conf.date, 0, 4) .. "/" .. string.sub(page_conf.date, 6, 7) .. "/" .. fileutils.getplainname(page) -- Convert the markdown file to HTML -- And put it in a metatable page_conf_mt.content = markdown(fileutils.pullfilecontent(page)) page_conf_mt.updated = os.date("%Y-%m-%dT%H:%M:%S", path.mtime(page)) - page_conf_mt.permalink = Ferron.site.siteconfig.baseurl .. (page_plainname ~= "index" and page_relpath .. ".html" or "") - page_conf_mt.rellink = (page_plainname ~= "index" and page_relpath .. ".html" or "") + page_conf_mt.permalink = Ferron.site.siteconfig.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.siteconfig -- If exists set the shortlink @@ -55,7 +52,7 @@ function page.makepage() -- Build the HTML file fileutils.pushfilecontent( - page_htmlpath .. "/" .. page_plainname .. ".html", + page_htmlpath .. "/" .. fileutils.getplainname(page) .. ".html", templateutils.setmustache( fileutils.pullfilecontent(page_template), templatespath .. "/partials", @@ -63,13 +60,13 @@ function page.makepage() ) ) - if not path.isfile(page_htmlpath .. "/" .. page_plainname .. ".html") then - print(" !! " .. page_relpath .. ".html - error!") + if not path.isfile(page_htmlpath .. "/" .. fileutils.getplainname(page) .. ".html") then + print(" !! " .. fileutils.getrelpath(page_noextension) .. ".html - error!") end -- Update the JSON file data fileutils.pushfilecontent( - page_noextension .. ".json", + fileutils.removeextension(page) .. ".json", json.encode( page_conf, { indent = true, diff --git a/ferron/static.lua b/ferron/static.lua @@ -20,17 +20,16 @@ function static.dispatch(file) if fileutils.isNonTextual(f) then local listindex_dirname = path.dirname(file) local img = f - local img_name = img:match("^.+/(.+)$") - local img_relpath = fileutils.getrelpath(img) + local img_name = img:match("^.+/(.+)$") local htmlfolder = Ferron.site.path .. Ferron.site.siteconfig.paths.html if not posix.readlink(img) then -- Copy content's images in the `public_html/` directory - path.copy(img, htmlfolder .. img_relpath) + path.copy(img, htmlfolder .. fileutils.getrelpath(img)) -- Then with those copies create symlinks from those -- non-textuals for list index pages - if path.isfile(htmlfolder .. img_relpath) then + if path.isfile(htmlfolder .. fileutils.getrelpath(img)) then lfs.link("." .. img:sub((listindex_dirname):len() + 1), htmlfolder .. fileutils.getrelpath(path.dirname(file)) .. "/" .. img_name, true) end diff --git a/ferron/utilities/file-utils.lua b/ferron/utilities/file-utils.lua @@ -88,6 +88,14 @@ function fileutils.sethtmlpath(folder) return Ferron.site.path .. Ferron.site.siteconfig.paths.html .. folder end +function fileutils.removeextension(file) + return file:match("(.+)%..*") +end + +function fileutils.getplainname(file) + return fileutils.removeextension(file):match("^.+/(.+)$") +end + function fileutils.shorturlencode(num) local alphabet = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_" local base = alphabet:len()