commit 0fc7fe8ec22e64adb8c5ff3bde828f25b0cc98f0
parent b9985ba12ef40515c008e654b7b3984b83ec3b28
Author: Hugo Soucy <hsoucy@kronostechnologies.com>
Date: Tue, 5 Feb 2019 08:03:24 -0500
Refactor some fileutils methods
Diffstat:
1 file changed, 79 insertions(+), 81 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"
@@ -12,108 +12,106 @@ local mimetypes = require "mimetypes"
local config = require "ferron.config"
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)
- and 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
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 false
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)
+ and 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)
+ and not fileutils.isIndex(filepath)
+ then
+ return filepath
+ end
- return false
+ return false
end
-function fileutils.isNonTextual(file)
- local mimestable = config.mimetypes
-
- return (tableutils.hasvalue(mimestable, mimetypes.guess(file)) and true or false)
+function fileutils.isNonTextual(filepath)
+ return (tableutils.hasvalue(config.mimetypes, mimetypes.guess(filepath)) 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)
- end
- end
+ if attr.mode == "directory" then
+ yieldtree(entry)
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 +121,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 = ""