ferron

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

commit 8ecff3811073d54300fa579863914aa5b789ec37
parent 4b1ab808292d6d36d62aa3a93e846340951d2ffb
Author: Hugo Soucy <hugo.soucy@equisoft.com>
Date:   Tue, 22 Oct 2019 20:47:22 -0400

Clean template functions

Diffstat:
Mferron/page.lua | 7++-----
Mferron/utilities/template-utils.lua | 52++++++++++++++++++++++++++--------------------------
2 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/ferron/page.lua b/ferron/page.lua @@ -114,13 +114,10 @@ function page.render(page, sitedata) -- Build and push the HTML page fl.pushfilecontent( - -- Create that page pathToHtml .. '/' .. fl.getplainname(page) .. '.html', - -- With that mustache template tp.rendertemplate( - tp.selecttemplate(false, metaTable.template, sitedata.templates), - metaTable, - assert(fl.isDirectory(sitedata.templates)) .. '/partials' + tp.selecttemplate(fl.isIndex(page), metaTable.template, sitedata.templates), + metaTable ) ) diff --git a/ferron/utilities/template-utils.lua b/ferron/utilities/template-utils.lua @@ -7,50 +7,50 @@ local templateutilsmeta = { } local lustache = require 'lustache' +local site = require 'ferron.site' local fl = require 'ferron.utilities.file-utils' - -- set template function templateutils.selecttemplate(index, templatename, templatesdir) - local templatepath = templatesdir .. '/' - local template = nil - - if templatename and type(templatename) == 'string' then - template = templatepath .. templatename + local templatepath = templatesdir .. '/' + local template + + if templatename and type(templatename) == 'string' then + template = templatepath .. templatename + else + if type(index) == 'boolean' and index == true then + template = templatepath .. 'default-index' else - if type(index) == 'boolean' and index == true then - template = templatepath .. 'default-index' - else - template = templatepath .. 'default' - end + template = templatepath .. 'default' end + end - return assert(template .. '.mustache', 'You need to add some templates to your site.') + return assert(template .. '.mustache', 'You need to add some templates to your site.') end -- process mustache -function templateutils.rendermustache(tpl, partialspath, data) - local partials = {} +function templateutils.rendermustache(tpl, data) + local partials = {} + local partialspath = fl.isDirectory(site.templates) .. '/partials' - for i,v in ipairs(lustache:parse(tpl)) do - if v.type == '>' then - partials[v.value] = fl.pullfilecontent(partialspath .. '/' .. v.value .. '.mustache') - end + for _, v in ipairs(lustache:parse(tpl)) do + if v.type == '>' then + partials[v.value] = fl.pullfilecontent(partialspath .. '/' .. v.value .. '.mustache') end + end - return lustache:render(tpl, data, partials) + return lustache:render(tpl, data, partials) end -- process template -function templateutils.rendertemplate(template, data, partials) - local extension = fl.getextension(template) - local render = nil +function templateutils.rendertemplate(template, data) + local render - if extension == '.mustache' then - render = templateutils.rendermustache(fl.pullfilecontent(template), partials, data) - end + if fl.getextension(template) == '.mustache' then + render = templateutils.rendermustache(fl.pullfilecontent(template), data) + end - return render + return render end return setmetatable(templateutils, templateutilsmeta)