commit 2aa1d1c960e6ce92d5d15e86f2d49491f51c268b
parent cb04583d21ab0503eff100cc19edc8d6d73f5890
Author: Hugo Soucy <hugo@soucy.cc>
Date: Sun, 26 Sep 2021 10:34:08 -0400
Simplify the API to get the children of an index page
Diffstat:
3 files changed, 53 insertions(+), 6 deletions(-)
diff --git a/satelito-dev-2.rockspec b/satelito-dev-2.rockspec
@@ -27,6 +27,7 @@ build = {
["satelito.dirtree"] = "satelito/dirtree.lua",
["satelito.feed"] = "satelito/feed.lua",
["satelito.file"] = "satelito/file.lua",
+ ["satelito.list"] = "satelito/list.lua",
["satelito.model"] = "satelito/model.lua",
["satelito.page"] = "satelito/page.lua",
["satelito.site"] = "satelito/site.lua",
diff --git a/satelito/init.lua b/satelito/init.lua
@@ -10,6 +10,7 @@ local assets = require 'satelito.assets'
local dirtree = require 'satelito.dirtree'
local feed = require 'satelito.feed'
local file = require 'satelito.file'
+local list = require 'satelito.list'
local site = require 'satelito.site'
local page = require 'satelito.page'
--
@@ -48,7 +49,7 @@ if args['init'] then
return
end
--- Example: '$ find site/content/ -name "*.md" | satelito pipe'
+-- Example: '$ find site/content/ -name "*.md" | satelito pipe'
if args['pipe'] then
local timestart = os.time()
local config
@@ -57,11 +58,13 @@ if args['pipe'] then
for filepath in (io.lines()) do
if file.is_markdown(filepath) or file.is_html(filepath) then
+ -- Get the site configuration's path
local configpath = site.get_config(filepath)
-- Change the current directory to the config's directory
lfs.chdir(file.get_dirname(configpath))
+ -- If the site configuration file exists
if file.exists(configpath) then
-- Add the currentdir to the package.path
package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua'
@@ -72,8 +75,10 @@ if args['pipe'] then
-- Absolute path to the 'content/' directory
local contentdir = lfs.currentdir() .. '/' .. config.paths.content
+ -- Get the meta of the file
local meta = model.set(filepath, config, contentdir)
+ -- Add the meta of the file into the sitemap table
sitemap[meta.relpath] = lume.extend({}, meta)
end
end
@@ -82,11 +87,17 @@ if args['pipe'] then
-- Array of templates
templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates))
+ -- Loop through the sitemap to create and export the HTML page
for k, v in pairs(sitemap) do
- v.site = sitemap
- local html, html_path = page.make(v, templates)
+ local html, html_path
local feed_xml, feed_xml_path
+ v.root = sitemap
+ v.children = list.get_children(v.list, sitemap, v.asc)
+
+ -- Make the page
+ html, html_path = page.make(v, templates)
+
if args['export'] then
file.export(html_path, html)
else
@@ -140,10 +151,16 @@ if args['make'] then
--
for k, v in pairs(sitemap) do
- v.site = sitemap
- local html, html_path = page.make(v, templates)
+ local html, html_path
local feed_xml, feed_xml_path
+ v.root = sitemap
+ v.children = list.get_children(v.list, sitemap, v.asc)
+
+ -- Make the page
+ html, html_path = page.make(v, templates)
+
+ -- Export the page
if args['export'] then
file.export(html_path, html)
else
@@ -163,7 +180,6 @@ if args['make'] then
end
print('Satelito built ' .. lume.count(sitemap) .. ' HTML page(s) in approximately ' .. os.difftime(os.time(), timestart) .. ' second(s).')
- --print('Satelito built that site in approximately ' .. os.difftime(os.time(), timestart) .. ' second(s).')
else
print('There is no "config.lua" here.')
os.exit()
diff --git a/satelito/list.lua b/satelito/list.lua
@@ -0,0 +1,30 @@
+-- @module list
+local list = {}
+--
+local lume = require 'satelito.lib.lume.lume'
+
+function list.get_children(children_list, sitemap, asc)
+ local children
+
+ if children_list then
+ -- Sorting ASC if list have asc set to true
+ if asc then
+ table.sort(
+ children_list,
+ function(a , b) return tonumber(sitemap[a].time_created) < tonumber(sitemap[b].time_created) end
+ )
+ -- Otherwise it's DESC
+ else
+ table.sort(
+ children_list,
+ function(a , b) return tonumber(sitemap[a].time_created) > tonumber(sitemap[b].time_created) end
+ )
+ end
+
+ children = lume.map(children_list, function(item) return sitemap[item] end)
+ end
+
+ return children
+end
+
+return list