commit fd57721a9147c7f5178bd3fbc4eba8b31b056502
parent 8a22e2f3c27a9a0992f11ae629b8dfef3815a6ce
Author: Hugo Soucy <hugo.soucy@equisoft.com>
Date: Sun, 13 Oct 2019 08:17:07 -0400
Move some functions in file-utils
Diffstat:
2 files changed, 46 insertions(+), 11 deletions(-)
diff --git a/ferron/page.lua b/ferron/page.lua
@@ -60,28 +60,27 @@ local function setpagetable(meta, relpath)
return
end
-local function setmetaprops(pagesource, relpath, sitedata)
+local function setpagemodel(page, sitedata)
return {
- content = markdown(fl.pullfilecontent(pagesource)),
+ content = markdown(fl.pullfilecontent(page)),
navigation = sitedata.navigation,
- permalink = sitedata.config.baseurl .. (fl.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''),
- rellink = (fl.getplainname(pagesource) ~= 'index' and relpath .. '.html' or ''),
+ permalink = fl.getpermalink(page, sitedata),
+ rellink = fl.getrellink(page, sitedata.content),
site = sitedata.config,
- updated = os.date('%Y-%m-%dT%H:%M:%S', lfs.attributes(pagesource).modification),
- id = 'tag:' .. sitedata.config.domainname .. ',' .. fl.getpageconf(pagesource).date .. ':' .. relpath,
+ updated = os.date('%Y-%m-%dT%H:%M:%S', lfs.attributes(page).modification),
+ id = fl.setpageid(page, sitedata),
entries = {},
}
end
-function page.render(pagesource, sitedata)
- local page = pagesource
+function page.render(page, sitedata)
local pathRel = fl.getrelpath(fl.removeextension(page), sitedata.content)
local pathToHtml = fl.sethtmlpath(fl.getrelpath(fl.getdirname(page), sitedata.content), sitedata.html)
local metaTable = setmetatable(
- setmetaprops(page, pathRel, sitedata), {__index = fl.getpageconf(page)}
+ setpagemodel(page, sitedata), {__index = fl.getpageconf(page)}
)
- local shortlinks_db = fl.isFile(sitedata.data .. '/shortlinks.json')
- local shortlinks_tb = (shortlinks_db ~= false and json.decode(fl.pullfilecontent(shortlinks_db)) or nil)
+ local shortlinks_db
+ local shortlinks_tb
-- Populate Ferron.site.pagestable{}
if fl.isNotIndex(page) then
@@ -90,6 +89,9 @@ function page.render(pagesource, sitedata)
-- If exists set the shortlink
if fl.isNotIndex(page) then
+ shortlinks_db = fl.isFile(sitedata.data .. '/shortlinks.json')
+ shortlinks_db = fl.isFile(sitedata.data .. '/shortlinks.json')
+
if shortlinks_tb and shortlinks_db then
metaTable.shortlink = (shortlinks_tb[metaTable.rellink] and shortlinks_tb[metaTable.rellink] or nil)
end
diff --git a/ferron/utilities/file-utils.lua b/ferron/utilities/file-utils.lua
@@ -74,6 +74,21 @@ function fileutils.isMarkdown(filepath)
return false
end
+--- Check if the pathname target is a JavaScript file
+-- @name isJS
+-- @param filepath a pathname to a file
+-- @return if true the filepath param, else false
+function fileutils.isJS(filepath)
+ if fileutils.isFile(filepath)
+ and (mimetypes.guess(filepath) == 'text/javascript'
+ or fileutils.getextension(filepath) == '.js')
+ then
+ return filepath
+ end
+
+ return false
+end
+
--- Check if the pathname target is a markdown file named index
-- @name isIndex
-- @param filepath a pathname to a file
@@ -188,6 +203,24 @@ function fileutils.getdirname(file)
return file:match('(.*/)')
end
+function fileutils.getrellink(file, contentdir)
+ local relpath = fileutils.getrelpath(file, contentdir)
+
+ return (fileutils.getplainname(file) ~= 'index' and relpath .. '.html' or '')
+end
+
+function fileutils.getpermalink(file, sitedata)
+ return sitedata.config.baseurl .. (fileutils.getrellink(file, sitedata.content))
+end
+
+function fileutils.setpageid(file, sitedata)
+ local domainname = sitedata.config.domainname
+ local date = fileutils.getpageconf(file).date
+ local relpath = fileutils.getrelpath(file, sitedata.content)
+
+ return 'tag:' .. domainname .. ',' .. date .. ':' .. relpath
+end
+
function fileutils.getpageconf(pagepath)
local pageconf = nil
local conflua = fileutils.isFile(fileutils.removeextension(pagepath) .. '.lua')