satelito

Static [web] site (or page) generator (ssg) made with Lua script.
git clone git://soucy.cc/satelito.git
Log | Files | Refs | README

list.lua (4453B)


      1 -- @module list
      2 local list = {}
      3 --
      4 local dirtree = require 'satelito.dirtree'
      5 local file = require 'satelito.file'
      6 local inspect = require 'inspect'
      7 local lfs = require 'lfs' -- luafilesystem
      8 local lume = require 'satelito.lib.lume.lume'
      9 
     10 -- Pagination
     11 function list.set_pagination(pagelist, len)
     12   local slicedlist = {}
     13   local i = 1
     14 
     15   while(i < #pagelist) do
     16     local j
     17 
     18     if i == 1 then
     19       j = 1
     20       i = i + (len-1)
     21     else
     22       j = i + 1
     23       i = i + len
     24     end
     25 
     26     slicedlist[#slicedlist+1] = lume.slice(pagelist, j, i)
     27   end
     28 
     29   return slicedlist
     30 end
     31 
     32 -- Children
     33 function list.get_children(children_list, sitemap, asc)
     34   local children = {}
     35 
     36   if children_list then
     37     for i = 1, #sitemap do
     38       local is_child = lume.find(children_list, sitemap[i].relpath)
     39 
     40       if is_child then
     41         children[#children+1] = sitemap[i]
     42       end
     43     end
     44 
     45     -- Sorting ASC if list have asc set to true
     46     if asc then
     47       table.sort(children, function(a, b) return tonumber(a.time_created) < tonumber(b.time_created) end)
     48     else
     49       table.sort(children, function(a, b) return tonumber(a.time_created) > tonumber(b.time_created) end)
     50     end
     51   end
     52 
     53   return children
     54 end
     55 
     56 function list.get_collection(collection, sitemap, asc)
     57   if collection and type(collection) == 'table' then
     58     local collection_list = {}
     59     local contentdir = lfs.currentdir()..'/'..sitemap[1].paths.content
     60 
     61     for i = 1, #collection do
     62       if lfs.attributes(contentdir..collection[i]).mode == 'directory' then
     63         collection_list[#collection_list+1] = file.get_collection(contentdir..collection[i], contentdir)
     64       else
     65         collection_list[#collection_list+1] = { collection[i] }
     66       end
     67     end
     68 
     69     return list.get_children(lume.concat(table.unpack(collection_list)), sitemap, asc)
     70   end
     71 
     72   return
     73 end
     74 
     75 -- Archives
     76 function list.get_archives(contentdir)
     77   local archives_table = {}
     78 
     79   for filepath in dirtree.get(contentdir) do
     80     if file.is_markdown(filepath)
     81       or file.is_html(filepath)
     82       and file.get_metafile(filepath)
     83     then
     84       local metafile = file.get_metafile(filepath)
     85 
     86       if metafile.date ~= '0000-00-00' then
     87         local year = string.sub(metafile.date, 1, 4)
     88         local month = string.sub(metafile.date, 6, 7)
     89 
     90         if archives_table[year] then
     91           archives_table[year][month] = {}
     92         else
     93           archives_table[year] = {
     94             [month] = {}
     95           }
     96         end
     97       end
     98     end
     99   end
    100 
    101   for filepath in dirtree.get(contentdir) do
    102     if file.is_markdown(filepath)
    103       or file.is_html(filepath)
    104       and file.get_metafile(filepath)
    105     then
    106       local metafile = file.get_metafile(filepath)
    107 
    108       if metafile.date ~= '0000-00-00' then
    109         local year = string.sub(metafile.date, 1, 4)
    110         local month = string.sub(metafile.date, 6, 7)
    111         local day = string.sub(metafile.date, 9, 10)
    112         local archive_info = {
    113           day = day,
    114           rellink = file.get_rellink(filepath, contentdir),
    115           title = metafile.title
    116         }
    117 
    118         archives_table[year][month][#archives_table[year][month]+1] = archive_info
    119       end
    120     end
    121   end
    122 
    123   return archives_table
    124 end
    125 
    126 -- Tags
    127 function list.get_tags(contentdir)
    128   local tags_table = {}
    129 
    130   -- Insert all the keywords as key of an empty subtable
    131   for filepath in dirtree.get(contentdir) do
    132     if file.is_markdown(filepath)
    133       or file.is_html(filepath)
    134       and file.get_metafile(filepath)
    135     then
    136       local metafile = file.get_metafile(filepath)
    137 
    138       if metafile.keywords ~= nil then
    139         for _, keyword in pairs(file.get_metafile(filepath).keywords) do
    140           if keyword ~= '' then
    141             tags_table[keyword] = {}
    142           end
    143         end
    144       end
    145     end
    146   end
    147 
    148   -- After insert relative links in each keyword's table
    149   for filepath in dirtree.get(contentdir) do
    150     if file.is_markdown(filepath)
    151       or file.is_html(filepath)
    152       and file.get_metafile(filepath)
    153     then
    154       local metafile = file.get_metafile(filepath)
    155 
    156       if metafile.keywords ~= nil then
    157         for _, keyword in pairs(file.get_metafile(filepath).keywords) do
    158           if tags_table[keyword] then
    159             local tags_info = {
    160               title = metafile.title,
    161               rellink = file.get_rellink(filepath, contentdir)
    162             }
    163 
    164             tags_table[keyword][#tags_table[keyword]+1] = tags_info
    165           end
    166         end
    167       end
    168     end
    169   end
    170 
    171   return tags_table
    172 end
    173 
    174 return list