commit daf2ea4ac2e21dc28319309ae11c8c783fb65c1b
parent 8f3c6f53944acf619288d601978cfb2271cfc201
Author: Hugo Soucy <hugo@soucy.cc>
Date: Sun, 26 Sep 2021 10:36:48 -0400
Update file.lua
Diffstat:
M | bin/utils/file.lua | | | 103 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- |
1 file changed, 97 insertions(+), 6 deletions(-)
diff --git a/bin/utils/file.lua b/bin/utils/file.lua
@@ -1,10 +1,30 @@
-- @module file
local file = {}
+--
local lfs = require 'lfs'
local mimetypes = require 'mimetypes'
---
+local lume = require 'utils.lume.lume'
+local dirtree = require 'utils.dirtree'
+
+-- Check if a file exists
+function file.exists(filepath)
+ local _file = io.open(filepath, 'r')
+
+ if _file ~= nil then
+ _file:close()
+
+ return filepath
+ else
+ return false
+ end
+end
+
-- Open & read the content of a file
function file.read(filepath)
+ if not filepath then
+ print('A file is missing ...')
+ end
+
local _file = assert(io.open(filepath, 'r'))
local content = _file:read '*a'
@@ -30,7 +50,7 @@ end
-- Get the directory of a file
function file.get_dirname(filepath)
- return filepath:match("(.*/)")
+ return filepath:match("(.*/)") and filepath:match("(.*/)") or ''
end
-- Get directory basename
@@ -42,25 +62,96 @@ function file.get_relpath(filepath, dirname)
return filepath:sub((dirname):len() + 1)
end
+function file.get_rellink(filepath, dirname)
+ if file.is_index(filepath) then
+ return '/' .. file.get_dirname(file.get_relpath(filepath, dirname))
+ else
+ return '/' .. file.get_relpath(filepath, dirname):match('(.+)%..*') .. '.html'
+ end
+end
+
+function file.get_permalink(filepath, dirname, url)
+ return url .. '/' .. file.get_relpath(filepath, dirname):match('(.+)%..*') .. '.html'
+end
+
+function file.get_exportlink(filepath, dirname, htmlpath)
+ return htmlpath .. file.get_relpath(filepath, dirname):match('(.+)%..*') .. '.html'
+end
+
+function file.get_metafile(filepath)
+ if lfs.attributes(filepath:match('(.+)%..*') .. '.lua') then
+ return dofile(filepath:match('(.+)%..*') .. '.lua')
+ else
+ return nil
+ end
+end
+
+-- Get the modified file from a list of paths
+function file.get_lastmodified(filelist)
+ return math.max(
+ table.unpack(
+ lume.map(filelist,
+ function(fl)
+ return lfs.attributes(fl).modification
+ end
+ )))
+end
+
-- Check if the file is markdown
function file.is_markdown(filepath)
return (mimetypes.guess(filepath) == 'text/x-markdown')
end
+-- Check if the file is HTML
+function file.is_html(filepath)
+ return (mimetypes.guess(filepath) == 'text/html')
+end
+
-- Check if the file is index.md
function file.is_index(filepath)
- return (file.get_basename(filepath) == 'index.md')
+ return (file.get_basename(filepath) == 'index.md') or (file.get_basename(filepath) == 'index.html')
+end
+
+--
+function file.get_collection(filepath, dirname)
+ local collection = {}
+
+ for subfilepath in dirtree.get(file.get_dirname(filepath)) do
+ if subfilepath
+ and file.is_markdown(subfilepath)
+ and not file.is_index(subfilepath)
+ then
+ collection[#collection+1] = file.get_relpath(subfilepath, dirname)
+ end
+ end
+
+ return collection
end
-- Create a dirtree
function file.mkdir(filepath)
local sep, pStr = package.config:sub(1, 1), ''
+ -- Check if the filepath is absolute or relative
+ local _filepath = (string.find(filepath, lfs.currentdir())
+ and filepath
+ or lfs.currentdir() .. '/' .. filepath)
- for dir in filepath:gmatch('[^' .. sep .. ']+') do
+ --
+ for dir in _filepath:gmatch('[^' .. sep .. ']+') do
pStr = pStr .. sep .. dir
-
- lfs.mkdir(lfs.currentdir() .. pStr)
+ lfs.mkdir(pStr)
end
end
+--
+
+--- Export a file to a specific location
+-- @name file.export
+-- @param filepath the location path of the file
+-- @param filecontent a string that is a code block
+-- @return execute a file.write function
+function file.export(filepath, filecontent)
+ file.mkdir(file.get_dirname(filepath))
+ return file.write(filepath, filecontent)
+end
return file