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 (4445B)


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