Static site generator made with Lua script.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.3 KiB

-- @module list
local list = {}
--
local dirtree = require 'satelito.dirtree'
local file = require 'satelito.file'
local lfs = require 'lfs' -- luafilesystem
local lume = require 'satelito.lib.lume.lume'
-- Pagination
function list.set_pagination(pagelist, len)
local slicedlist = {}
local i = 1
while(i < #pagelist) do
local j
if i == 1 then
j = 1
i = i + (len-1)
else
j = i + 1
i = i + len
end
slicedlist[#slicedlist+1] = lume.slice(pagelist, j, i)
end
return slicedlist
end
-- Children
function list.get_children(children_list, sitemap, asc)
local children = {}
if children_list then
for i = 1, #sitemap do
local is_child = lume.find(children_list, sitemap[i].relpath)
if is_child then
children[#children+1] = sitemap[i]
end
end
-- Sorting ASC if list have asc set to true
if asc then
table.sort(children, function(a, b) return tonumber(a.time_created) < tonumber(b.time_created) end)
else
table.sort(children, function(a, b) return tonumber(a.time_created) > tonumber(b.time_created) end)
end
end
return children
end
function list.get_collection(collection, sitemap, asc)
if collection and type(collection) == 'table' then
local collection_list = {}
local contentdir = lfs.currentdir()..'/'..sitemap[1].paths.content
for i = 1, #collection do
if lfs.attributes(contentdir..collection[i]).mode == 'directory' then
collection_list[#collection_list+1] = file.get_collection(contentdir..collection[i], contentdir)
else
collection_list[#collection_list+1] = { collection[i] }
end
end
return list.get_children(lume.concat(table.unpack(collection_list)), sitemap, asc)
end
return
end
-- Archives
function list.get_archives(contentdir)
local archives_table = {}
for filepath in dirtree.get(contentdir) do
if file.is_markdown(filepath)
or file.is_html(filepath)
and file.get_metafile(filepath)
then
local metafile = file.get_metafile(filepath)
if metafile.date ~= '0000-00-00' then
local year = string.sub(metafile.date, 1, 4)
local month = string.sub(metafile.date, 6, 7)
if archives_table[year] then
archives_table[year][month] = {}
else
archives_table[year] = {
[month] = {}
}
end
end
end
end
for filepath in dirtree.get(contentdir) do
if file.is_markdown(filepath)
or file.is_html(filepath)
and file.get_metafile(filepath)
then
local metafile = file.get_metafile(filepath)
if metafile.date ~= '0000-00-00' then
local year = string.sub(metafile.date, 1, 4)
local month = string.sub(metafile.date, 6, 7)
local day = string.sub(metafile.date, 9, 10)
local archive_info = {
day = day,
rellink = file.get_rellink(filepath, contentdir),
title = metafile.title
}
archives_table[year][month][#archives_table[year][month]+1] = archive_info
end
end
end
return archives_table
end
-- Tags
function list.get_tags(contentdir)
local tags_table = {}
-- Insert all the keywords as key of an empty subtable
for filepath in dirtree.get(contentdir) do
if file.is_markdown(filepath)
or file.is_html(filepath)
and file.get_metafile(filepath)
then
local metafile = file.get_metafile(filepath)
if metafile.keywords ~= nil then
for _, keyword in pairs(file.get_metafile(filepath).keywords) do
if keyword ~= '' then
tags_table[keyword] = {}
end
end
end
end
end
-- After insert relative links in each keyword's table
for filepath in dirtree.get(contentdir) do
if file.is_markdown(filepath)
or file.is_html(filepath)
and file.get_metafile(filepath)
then
local metafile = file.get_metafile(filepath)
if metafile.keywords ~= nil then
for _, keyword in pairs(file.get_metafile(filepath).keywords) do
if tags_table[keyword] then
local tags_info = {
title = metafile.title,
rellink = file.get_rellink(filepath, contentdir)
}
tags_table[keyword][#tags_table[keyword]+1] = tags_info
end
end
end
end
end
return tags_table
end
return list