ferron

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

commit 6c6f93ec094d06776446dc27d2adcd0455fba096
parent e048e7579035e87eda6e5eb9e86cf99014e5b32d
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Wed, 29 Nov 2017 23:25:15 -0500

Optimize vars

Diffstat:
Mfunctions/make-lists-of-pages.lua | 30+++++++++++++++++-------------
Mfunctions/make-pages.lua | 33++++++++++++++-------------------
2 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/functions/make-lists-of-pages.lua b/functions/make-lists-of-pages.lua @@ -2,6 +2,7 @@ local json = require("dkjson") local markdown = require("markdown") local path = require("path") +local ferronutils = require("functions.ferron-utils") local fileutils = require("functions.file-utils") local tableutils = require("functions.table-utils") local templateutils = require("functions.template-utils") @@ -14,21 +15,23 @@ local function makelistsofpages() and file ~= Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT .. "/index.md" then local listindex = file - local listindex_plainname = listindex:match("^.+/(.+)$"):match("(.+)%..*") + 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:match("(.+)%..*") .. ".json")) - local listindex_relpath = listindex:match("(.+)%..*"):sub((Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT):len() + 1) - local listindex_relpathfolder = (path.dirname(listindex):sub((Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT):len() + 1)) - local listindex_pathhtml = Ferron.site.path .. Ferron.site.config.SITE.PATHS.HTML .. listindex_relpathfolder - local listindex_html = listindex_pathhtml .. "/" .. listindex_plainname .. ".html" + local listindex_metadatas = json.decode(fileutils.pullfilecontent(listindex_noextension .. ".json")) + local listindex_metadatas_mt = setmetatable({}, { __index = listindex_metadatas }) local listindex_table = {} print("§ Make an index page for the `" .. listindex_section .. "` section:") -- Convert the markdown file to HTML - listindex_metadatas.content = markdown(fileutils.pullfilecontent(listindex)) + listindex_metadatas_mt.content = markdown(fileutils.pullfilecontent(listindex)) - listindex_table = listindex_metadatas + listindex_metadatas_mt.permalink = Ferron.site.config.SITE.BASEURL .. path.dirname(listindex_relpath) + + listindex_table = listindex_metadatas_mt listindex_table["SITE"] = Ferron.site.config.SITE listindex_table["entries"] = {} @@ -40,15 +43,15 @@ local function makelistsofpages() end end - if path.isdir(listindex_pathhtml) == false then - fileutils.mkdir(listindex_pathhtml) + if path.isdir(listindex_htmlpath) == false then + fileutils.mkdir(listindex_htmlpath) end dispatchnontextuals(listindex, listindex_section) -- Build the HTML file fileutils.pushfilecontent( - listindex_html, + 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", @@ -56,7 +59,7 @@ local function makelistsofpages() ) ) - if path.isfile(listindex_html) ~= false then + if path.isfile(listindex_htmlpath .. "/" .. listindex_plainname .. ".html") ~= false then print(" ¬ " .. listindex_relpath .. ".html - created") else print(" !! " .. listindex_relpath .. ".html - error!") @@ -67,7 +70,8 @@ local function makelistsofpages() -- Update the JSON file data listindex_metadatas["entries"] = nil listindex_metadatas["SITE"] = nil - fileutils.pushfilecontent(listindex:match("(.+)%..*") .. ".json", json.encode(listindex_metadatas, {indent = true, keyorder = {"bridgy","cite","citeurl","content","date","datetime","description","id","keywords","permalink","section","template","title","updated"}})) + + 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 end diff --git a/functions/make-pages.lua b/functions/make-pages.lua @@ -2,6 +2,7 @@ local json = require("dkjson") local markdown = require("markdown") local path = require("path") +local ferronutils = require("functions.ferron-utils") local fileutils = require("functions.file-utils") local tableutils = require("functions.table-utils") local templateutils = require("functions.template-utils") @@ -18,39 +19,34 @@ local function makepages() then local md = file local md_noextension = md:match("(.+)%..*") - local md_plainname = md:match("^.+/(.+)$"):match("(.+)%..*") - local md_folder = path.dirname(md) - local md_relpath = md_noextension:sub((Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT):len() + 1) - local md_relpathfolder = md_folder:sub((Ferron.site.path .. Ferron.site.config.SITE.PATHS.CONTENT):len() + 1) - local md_pathhtml = Ferron.site.path .. Ferron.site.config.SITE.PATHS.HTML .. md_relpathfolder - local md_html = md_pathhtml .. "/" .. md_plainname .. ".html" + 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_updated = os.date("%Y-%m-%dT%H:%M:%S", attr.modification) - local md_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 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 + -- Convert the markdown file to HTML -- And put it in a metatable md_metadatas_mt.content = markdown(fileutils.pullfilecontent(md)) - md_metadatas.section = md_section - md_metadatas.updated = md_updated - md_metadatas.id = md_id - md_metadatas.permalink = Ferron.site.config.SITE.BASEURL .. (md_plainname ~= "index" and md_relpath .. ".html" or "") + 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 - md_metadatas_mt["SITE"] = Ferron.site.config.SITE - - if path.isdir(md_pathhtml) == false then - fileutils.mkdir(md_pathhtml) + if path.isdir(md_htmlpath) == false then + fileutils.mkdir(md_htmlpath) end -- Build the HTML file fileutils.pushfilecontent( - md_html, + 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", @@ -58,14 +54,13 @@ local function makepages() ) ) - if path.isfile(md_html) ~= false then + 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 - md_metadatas.content = nil 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