ferron

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

commit 252c8d5e73e1d56aa73c3847e29d3bdfc2c18432
parent cc6d41367d764482e8d6d95bf8be8bf059b5df85
Author: Hugo Soucy <hsoucy@kronostechnologies.com>
Date:   Mon,  4 Feb 2019 15:31:55 -0500

Simplify isFile method

Diffstat:
Mferron/utilities/file-utils.lua | 156++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 78 insertions(+), 78 deletions(-)

diff --git a/ferron/utilities/file-utils.lua b/ferron/utilities/file-utils.lua @@ -1,9 +1,9 @@ -- fileutils.lua local fileutils = {} local fileutilsmeta = { - __call = function(self, key, vars) - print(key) - end + __call = function(self, key, vars) + print(key) + end } local json = require "dkjson" @@ -14,106 +14,106 @@ local tableutils = require "ferron.utilities.table-utils" function fileutils.getbasename(filepath) - return string.gsub(filepath, "(.*/)(.*)", "%2") + return string.gsub(filepath, "(.*/)(.*)", "%2") end function fileutils.isFile(filepath) - if lfs.attributes(filepath) == nil then - return false - elseif lfs.attributes(filepath).mode == "file" then - return filepath - end + if lfs.attributes(filepath) + and lfs.attributes(filepath).mode == "file" + then + return filepath + end - return false + return false end function fileutils.isDirectory(filepath) - if lfs.attributes(filepath) == nil then - return false - elseif lfs.attributes(filepath).mode == "directory" then - return filepath - end + if lfs.attributes(filepath) == nil then + return false + elseif lfs.attributes(filepath).mode == "directory" then + return filepath + end - return false + return false end function fileutils.isMarkdown(filepath) - if fileutils.isFile(filepath) - and (mimetypes.guess(filepath) == "text/x-markdown" - or fileutils.getextension(filepath) == ".md") then - return filepath - end + if fileutils.isFile(filepath) + and (mimetypes.guess(filepath) == "text/x-markdown" + or fileutils.getextension(filepath) == ".md") then + return filepath + end - return + return end function fileutils.isIndex(filepath) - if fileutils.isMarkdown(filepath) == false then - return false - elseif fileutils.getbasename(filepath) == "index.md" then - return filepath - end + if fileutils.isMarkdown(filepath) == false then + return false + elseif fileutils.getbasename(filepath) == "index.md" then + return filepath + end - return false + return false end function fileutils.isNotIndex(filepath) - if fileutils.isMarkdown(filepath) == false then - return false - elseif fileutils.isMarkdown(filepath) and not fileutils.isIndex(filepath) then - return filepath - end + if fileutils.isMarkdown(filepath) == false then + return false + elseif fileutils.isMarkdown(filepath) and not fileutils.isIndex(filepath) then + return filepath + end - return false + return false end function fileutils.isNonTextual(file) - local mimestable = config.mimetypes + local mimestable = config.mimetypes - return (tableutils.hasvalue(mimestable, mimetypes.guess(file)) and true or false) + return (tableutils.hasvalue(mimestable, mimetypes.guess(file)) and true or false) end -- getdirtree function fileutils.getdirtree(dir) - assert(dir and dir ~= "", "directory parameter is missing or empty") + assert(dir and dir ~= "", "directory parameter is missing or empty") - if string.sub(dir, -1) == "/" then - dir = string.sub(dir, 1, -2) - end + if string.sub(dir, -1) == "/" then + dir = string.sub(dir, 1, -2) + end - local function yieldtree(dir) - for entry in lfs.dir(dir) do - if entry ~= "." and entry ~= ".." then - entry = dir.."/"..entry + local function yieldtree(dir) + for entry in lfs.dir(dir) do + if entry ~= "." and entry ~= ".." then + entry = dir.."/"..entry - local attr = lfs.attributes(entry) + local attr = lfs.attributes(entry) - coroutine.yield(entry,attr) + coroutine.yield(entry,attr) - if attr.mode == "directory" then - yieldtree(entry) + if attr.mode == "directory" then + yieldtree(entry) + end + end end - end end - end - return coroutine.wrap(function() yieldtree(dir) end) + return coroutine.wrap(function() yieldtree(dir) end) end -- mkdir function fileutils.mkdir(path) - local sep, pStr = package.config:sub(1, 1), "" + local sep, pStr = package.config:sub(1, 1), "" - for dir in path:gmatch("[^" .. sep .. "]+") do - pStr = pStr .. sep .. dir + for dir in path:gmatch("[^" .. sep .. "]+") do + pStr = pStr .. sep .. dir - lfs.mkdir(pStr) - end + lfs.mkdir(pStr) + end end -- pullfilecontent function fileutils.pullfilecontent(pathtofile) - local file = assert(io.open(pathtofile, "r")) + local file = assert(io.open(pathtofile, "r")) local content = file:read "*a" file:close() @@ -123,55 +123,55 @@ end -- pushfilecontent function fileutils.pushfilecontent(pathtofile, data) - local file = io.open(pathtofile, "w+") + local file = io.open(pathtofile, "w+") - file:write(data) + file:write(data) file:close() - return assert(fileutils.isFile(pathtofile)) + return assert(fileutils.isFile(pathtofile)) end function fileutils.getrelpath(file) - return file:sub((Ferron.site.path .. Ferron.site.config.paths.content):len() + 1) + return file:sub((Ferron.site.path .. Ferron.site.config.paths.content):len() + 1) end function fileutils.sethtmlpath(folder) - return Ferron.site.path .. Ferron.site.config.paths.html .. folder + return Ferron.site.path .. Ferron.site.config.paths.html .. folder end function fileutils.removeextension(file) - return file:match("(.+)%..*") + return file:match("(.+)%..*") end function fileutils.getextension(file) - return file:match("^.+(%..+)$") + return file:match("^.+(%..+)$") end function fileutils.getplainname(file) - return fileutils.removeextension(file):match("^.+/(.+)$") + return fileutils.removeextension(file):match("^.+/(.+)$") end function fileutils.getdirname(file) - return file:match("(.*/)") + return file:match("(.*/)") end function fileutils.getpageconf(pagepath) - local pageconf = nil - local conflua = fileutils.isFile(fileutils.removeextension(pagepath) .. ".lua") - local confjson = fileutils.isFile(fileutils.removeextension(pagepath) .. ".json") - - if conflua then - package.path = package.path .. ";" .. fileutils.getdirname(conflua) .. "/?.lua" - pageconf = require(fileutils.getplainname(conflua)) - elseif confjson then - pageconf = json.decode(fileutils.pullfilecontent(confjson)) - end + local pageconf = nil + local conflua = fileutils.isFile(fileutils.removeextension(pagepath) .. ".lua") + local confjson = fileutils.isFile(fileutils.removeextension(pagepath) .. ".json") + + if conflua then + package.path = package.path .. ";" .. fileutils.getdirname(conflua) .. "/?.lua" + pageconf = require(fileutils.getplainname(conflua)) + elseif confjson then + pageconf = json.decode(fileutils.pullfilecontent(confjson)) + end - return pageconf + return pageconf end function fileutils.shorturlencode(num) - local alphabet = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_" + local alphabet = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_" local base = alphabet:len() local str = ""