ferron

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

commit 2c5fa9680a101ce38884c026942192e85f9ffdf0
parent 4c0e172a6577afd89ab019dd2b7912226db62bbd
Author: Hugo Soucy <hsoucy@kronostechnologies.com>
Date:   Tue, 16 Oct 2018 15:10:04 -0400

Add a function to select templates between etlua or mustache

Diffstat:
Mferron/config.lua | 1+
Mferron/list.lua | 5++---
Mferron/page.lua | 5++---
Mferron/utilities/template-utils.lua | 27++++++++++++++++++++++++++-
4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/ferron/config.lua b/ferron/config.lua @@ -24,6 +24,7 @@ local config = { "luaposix", "lua-discount", "dkjson", + "etlua", "inspect", "mimetypes" } diff --git a/ferron/list.lua b/ferron/list.lua @@ -67,7 +67,6 @@ function list.makelist() local list_htmlpath = fileutils.sethtmlpath(fileutils.getrelpath(path.dirname(list))) local list_conf = json.decode(fileutils.pullfilecontent(fileutils.removeextension(list) .. ".json")) local list_conf_mt = setmetatable({}, { __index = list_conf }) - local list_template = templatespath .. "/" .. (list_conf.template ~= nil and list_conf.template or "default-index") .. ".mustache" print("ยง Make a list page for the `" .. fileutils.getrelpath(path.dirname(list)) .. "` subdirectory ...") @@ -107,14 +106,14 @@ function list.makelist() fileutils.pushfilecontent( list_htmlpath .. "/" .. fileutils.getplainname(list) .. ".html", templateutils.processmustache( - fileutils.pullfilecontent(list_template), + fileutils.pullfilecontent(templateutils.settemplate(true, list_conf.template)), templatespath .. "/partials", list_conf_mt ) ) end, { - delay = true; -- use snapshot of directory + delay = false; -- use snapshot of directory recurse = true; -- include subdirs reverse = false; -- subdirs at first } diff --git a/ferron/page.lua b/ferron/page.lua @@ -25,7 +25,6 @@ function page.makepage() 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 .. "|" .. 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.config.domainname .. "," .. page_conf.date .. ":" .. string.sub(page_conf.date, 0, 4) .. "/" .. string.sub(page_conf.date, 6, 7) .. "/" .. fileutils.getplainname(page) @@ -60,7 +59,7 @@ function page.makepage() fileutils.pushfilecontent( page_htmlpath .. "/" .. fileutils.getplainname(page) .. ".html", templateutils.processmustache( - fileutils.pullfilecontent(page_template), + fileutils.pullfilecontent(templateutils.settemplate(false, page_conf.template)), templatespath .. "/partials", page_conf_mt ) @@ -68,7 +67,7 @@ function page.makepage() end end, { - delay = true; -- use snapshot of directory + delay = false; -- use snapshot of directory recurse = true; -- include subdirs reverse = true; -- subdirs at first } diff --git a/ferron/utilities/template-utils.lua b/ferron/utilities/template-utils.lua @@ -1,10 +1,30 @@ -- local lustache = require "lustache" +local path = require "path" local fileutils = require "ferron.utilities.file-utils" local templateutils = {} --- setmustache +-- set template +function templateutils.settemplate(index, templatename) + local templatepath = Ferron.site.path .. Ferron.site.config.paths.templates .. "/" + local template = nil + + if templatename ~= nil and type(templatename) == "string" then + template = templatepath .. templatename + else + if type(index) == "boolean" and index == true then + template = templatepath .. "default-index" + else + template = templatepath .. "default" + end + end + -- etlua or mustache + -- etlua templates will outrank the mustache ones + return assert(path.isfile(template .. ".etlua") and template .. ".etlua" or template .. ".mustache", "You need to add some templates to your site.") +end + +-- process mustache function templateutils.processmustache(tpl, partialspath, data) local partials = {} @@ -17,4 +37,9 @@ function templateutils.processmustache(tpl, partialspath, data) return lustache:render(tpl, data, partials) end +-- process template +function templateutils.processtemplate(template, data) + print(template, data) +end + return templateutils