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.
123 lines
3.3 KiB
123 lines
3.3 KiB
-- @module site
|
|
local site = {}
|
|
--
|
|
local lfs = require 'lfs'
|
|
--
|
|
local assets = require 'satelito.assets'
|
|
local feed = require 'satelito.feed'
|
|
local file = require 'satelito.file'
|
|
local list = require 'satelito.list'
|
|
local lume = require 'satelito.lib.lume.lume'
|
|
local page = require 'satelito.page'
|
|
|
|
--- From a filepath get the closest 'config.lua' by climbing the
|
|
-- directory tree
|
|
-- Recursive function
|
|
function site.get_config(filepath)
|
|
assert(filepath and filepath ~= '', 'The filepath parameter is missing or empty.')
|
|
|
|
local dir = filepath:match("(.*/)")
|
|
local dir_parent = string.sub(dir, 1, -2):match("(.*/)")
|
|
|
|
for entry in lfs.dir(dir) do
|
|
if entry and entry == 'config.lua' then
|
|
return dir .. entry
|
|
end
|
|
end
|
|
|
|
return site.get_config(dir_parent)
|
|
end
|
|
|
|
-- Make the site
|
|
function site.make(sitemap, export, timestart)
|
|
for i = 1, #sitemap do
|
|
local html, html_path
|
|
local feed_xml, feed_xml_path
|
|
local paginated
|
|
|
|
sitemap[i].index = i
|
|
|
|
if sitemap[i].list ~= false and file.is_index(sitemap[i].path) then
|
|
sitemap[i].children = list.get_children(sitemap[i].list, sitemap, sitemap[i].asc)
|
|
end
|
|
|
|
if sitemap[i].collection then
|
|
sitemap[i].collection = list.get_collection(sitemap[i].collection, sitemap, sitemap[i].asc)
|
|
end
|
|
|
|
if i > 1 then
|
|
sitemap[i].relprev = sitemap[i-1]
|
|
end
|
|
|
|
if i < #sitemap then
|
|
sitemap[i].relnext = sitemap[i+1]
|
|
end
|
|
|
|
-- If a pagination is requested
|
|
if sitemap[i].pagination and file.is_index(sitemap[i].relpath) then
|
|
print('=> A pagination is requested ...')
|
|
|
|
sitemap[i].pagination.limit = sitemap[i].pagination.limit or 6
|
|
sitemap[i].pagination.prefix = sitemap[i].pagination.prefix or 'pg-'
|
|
|
|
-- Split page children by pagination limit
|
|
paginated = list.set_pagination(sitemap[i].collection or sitemap[i].children, sitemap[i].pagination.limit)
|
|
|
|
-- Set the pagination length
|
|
sitemap[i].pagination.length = #paginated
|
|
|
|
print('=> Making the pagination pages ...')
|
|
|
|
for p = 1, #paginated do
|
|
sitemap[i].children = paginated[p]
|
|
sitemap[i].pagination.current = p
|
|
-- Make the pagination pages
|
|
if p == 1 then
|
|
html, html_path = page.make(sitemap[i])
|
|
else
|
|
html, html_path = page.make(
|
|
sitemap[i],
|
|
sitemap[i].exportlink:match("(.*/)")..sitemap[i].pagination.prefix..p..'.html'
|
|
)
|
|
end
|
|
-- Export the pagination pages
|
|
if export then
|
|
file.export(html_path, html)
|
|
else
|
|
print(html)
|
|
end
|
|
end
|
|
|
|
else
|
|
-- Make the page
|
|
html, html_path = page.make(sitemap[i])
|
|
|
|
-- Export the page
|
|
if export then
|
|
file.export(html_path, html)
|
|
else
|
|
print(html)
|
|
end
|
|
end
|
|
|
|
-- Feed
|
|
if file.is_index(sitemap[i].relpath) and export then
|
|
feed_xml, feed_xml_path = feed.make(sitemap[i])
|
|
file.export(feed_xml_path, feed_xml)
|
|
end
|
|
|
|
-- Copy assets to the public_html/ folder
|
|
if export then
|
|
assets.export(sitemap[i])
|
|
end
|
|
end
|
|
|
|
print('--------------------------------------------------------------------------')
|
|
print(
|
|
'Satelito built '
|
|
..lume.count(sitemap)..' HTML page(s) in approximately '
|
|
..os.difftime(os.time(), timestart)..' second(s).'
|
|
)
|
|
end
|
|
|
|
return site
|
|
|