hscc

Soure code of <https://hugo.soucy.cc>.
git clone git://soucy.cc/hscc.git
Log | Files | Refs

file.lua (3879B)


      1 -- @module file
      2 local file = {}
      3 --
      4 local lfs = require 'lfs'
      5 local mimetypes = require 'mimetypes'
      6 local lume = require 'utils.lume.lume'
      7 local dirtree = require 'utils.dirtree'
      8 
      9 -- Check if a file exists
     10 function file.exists(filepath)
     11   local _file = io.open(filepath, 'r')
     12 
     13   if _file ~= nil then
     14     _file:close()
     15 
     16     return filepath
     17   else
     18     return false
     19   end
     20 end
     21 
     22 -- Open & read the content of a file
     23 function file.read(filepath)
     24   if not filepath then
     25     print('A file is missing ...')
     26   end
     27 
     28   local _file = assert(io.open(filepath, 'r'))
     29 	local content = _file:read '*a'
     30 
     31 	_file:close()
     32 
     33 	return content
     34 end
     35 
     36 -- Create a file and add data in it
     37 function file.write(filepath, data)
     38   local _file = io.open(filepath, 'w+')
     39 
     40   _file:write(data)
     41   _file:close()
     42 
     43   return assert(lfs.attributes(filepath).mode == 'file')
     44 end
     45 
     46 -- Get basename from a file path
     47 function file.get_basename(filepath)
     48   return filepath:gsub('(.*/)(.*)', '%2')
     49 end
     50 
     51 -- Get the directory of a file
     52 function file.get_dirname(filepath)
     53   return filepath:match("(.*/)") and filepath:match("(.*/)") or ''
     54 end
     55 
     56 -- Get directory basename
     57 function file.get_basedir(dirname)
     58   return dirname:match('^.+/(.+)$')
     59 end
     60 
     61 function file.get_relpath(filepath, dirname)
     62   return filepath:sub((dirname):len() + 1)
     63 end
     64 
     65 function file.get_rellink(filepath, dirname)
     66   if  file.is_index(filepath) then
     67     return '/' .. file.get_dirname(file.get_relpath(filepath, dirname))
     68   else
     69     return '/' .. file.get_relpath(filepath, dirname):match('(.+)%..*') .. '.html'
     70   end
     71 end
     72 
     73 function file.get_permalink(filepath, dirname, url)
     74   return url .. '/' .. file.get_relpath(filepath, dirname):match('(.+)%..*') .. '.html'
     75 end
     76 
     77 function file.get_exportlink(filepath, dirname, htmlpath)
     78   return htmlpath .. file.get_relpath(filepath, dirname):match('(.+)%..*') .. '.html'
     79 end
     80 
     81 function file.get_metafile(filepath)
     82   if lfs.attributes(filepath:match('(.+)%..*') .. '.lua') then
     83     return dofile(filepath:match('(.+)%..*') .. '.lua')
     84   else
     85     return nil
     86   end
     87 end
     88 
     89 -- Get the modified file from a list of paths
     90 function file.get_lastmodified(filelist)
     91   return math.max(
     92     table.unpack(
     93       lume.map(filelist,
     94                function(fl)
     95                  return lfs.attributes(fl).modification
     96                end
     97   )))
     98 end
     99 
    100 -- Check if the file is markdown
    101 function file.is_markdown(filepath)
    102   return (mimetypes.guess(filepath) == 'text/x-markdown')
    103 end
    104 
    105 -- Check if the file is HTML
    106 function file.is_html(filepath)
    107   return (mimetypes.guess(filepath) == 'text/html')
    108 end
    109 
    110 -- Check if the file is index.md
    111 function file.is_index(filepath)
    112   return (file.get_basename(filepath) == 'index.md') or (file.get_basename(filepath) == 'index.html')
    113 end
    114 
    115 --
    116 function file.get_collection(filepath, dirname)
    117   local collection = {}
    118 
    119   for subfilepath in dirtree.get(file.get_dirname(filepath)) do
    120     if subfilepath
    121       and (file.is_markdown(subfilepath) or file.is_html(subfilepath))
    122       and not file.is_index(subfilepath)
    123     then
    124       collection[#collection+1] = file.get_relpath(subfilepath, dirname)
    125     end
    126   end
    127 
    128   return collection
    129 end
    130 
    131 -- Create a dirtree
    132 function file.mkdir(filepath)
    133   local sep, pStr = package.config:sub(1, 1), ''
    134   -- Check if the filepath is absolute or relative
    135   local _filepath = (string.find(filepath, lfs.currentdir())
    136                      and filepath
    137                      or lfs.currentdir() .. '/' .. filepath)
    138 
    139   --
    140   for dir in _filepath:gmatch('[^' .. sep .. ']+') do
    141     pStr = pStr .. sep .. dir
    142     lfs.mkdir(pStr)
    143   end
    144 end
    145 --
    146 
    147 --- Export a file to a specific location
    148 -- @name file.export
    149 -- @param filepath the location path of the file
    150 -- @param filecontent a string that is a code block
    151 -- @return execute a file.write function
    152 function file.export(filepath, filecontent)
    153   file.mkdir(file.get_dirname(filepath))
    154   return file.write(filepath, filecontent)
    155 end
    156 
    157 return file