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.

189 lines
6.0 KiB

#!/usr/bin/env lua
--
local argparse = require 'argparse'
local inspect = require 'inspect'
local lfs = require 'lfs'
--
local lume = require 'satelito.lib.lume.lume'
local model = require 'satelito.model'
local dirtree = require 'satelito.dirtree'
local file = require 'satelito.file'
local sitemapxml = require 'satelito.sitemapxml'
local site = require 'satelito.site'
--
local init
local pipe
local make
local exec
local parser = argparse()
:name 'satelito'
:description 'Satelito is a static site generator in lua script.'
:epilog 'For more info, see https://soucy.cc/git/satelito/file/README.md.html'
local args
-- Set 'init' command
init = parser:command('init', 'Init the sample website in your $HOME folder.')
-- Set 'pipe' command
pipe = parser:command('pipe', 'Build the site by inputing one or more markdown files through the pipeline.')
pipe:flag('-e --export', 'Export the outputed HTML in the *config.paths.public_html* folder.')
-- Set 'make' command
make = parser:command('make', 'Build the site from his directory.')
make:flag('-e --export', 'Export the outputed HTML in the *config.paths.public_html* folder.')
-- Set the exec command
exec = parser:command('exec', 'Execute a script frome the bin directory')
exec:argument 'bin name'
args = parser:parse()
if args['exec'] then
package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua'
print(args['bin name'], inspect(lfs.currentdir()))
if args['bin name'] then
dofile(lfs.currentdir() .. '/bin/'..args['bin name'])
end
end
-- Example '$ satelito init'
if args['init'] then
os.execute('mkdir ~/satelito-sample')
os.execute('git clone git://soucy.cc/satelito-sample.git ~/satelito-sample')
print('------------------------------------------------------------------------------------------------------')
os.execute('rm -Rf ~/satelito-sample/.git')
os.execute('ls -la ~/satelito-sample')
print('------------------------------------------------------------------------------------------------------')
print('You should rename `~/satelito-sample` and edit `~/satelito-sample/config.lua` according to your needs.')
print('------------------------------------------------------------------------------------------------------')
return
end
-- Example: '$ find site/content/ -name "*.md" | satelito pipe'
if args['pipe'] then
local configpath = false
local config = false
local contentdir = false
local sitemap = {}
local templates
local timestart = os.time()
print('=> Satelito is on ...')
for filepath in (io.lines()) do
if file.is_markdown(filepath) or file.is_html(filepath) then
-- Get the site configuration's path
if not configpath then
print('=> Searching for the nearest config.lua ...')
configpath = site.get_config(filepath)
end
-- Change the current directory to the config's directory
if lfs.currentdir()..'/' ~= file.get_dirname(configpath) then
print('=> Moving where the configuration file is located ...')
lfs.chdir(file.get_dirname(configpath))
end
-- 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'
-- Set config.lua in a table
if not config then
print('=> Fetching the configuration file content ...')
config = require 'config'
end
-- Absolute path to the 'content/' directory
if not contentdir then
contentdir = lfs.currentdir() .. '/' .. config.paths.content
end
-- Get the list of templates
if not templates then
print('=> Fetching the templates ...')
templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates))
end
-- Get the meta of the file
local meta = model.set(filepath, lume.extend(config, {templates = templates}), contentdir)
-- Add the meta of the file into the sitemap table
if lume.count(sitemap) == 0 then
print('=> Fetching the markdown and HTML content ...')
end
sitemap[#sitemap+1] = meta
end
end
end
print('=> '..lume.count(sitemap)..' content found')
print('=> Making the web site ...')
-- Sorting by alphanum
table.sort(sitemap, function(a, b) return a.idorder > b.idorder end)
return site.make(sitemap, args['export'], timestart)
end
-- Make command
-- Example: '$ satelito make --export'
if args['make'] then
local config
local templates
local timestart = os.time()
local sitemap = {}
print('=> Satelito is on ...')
if file.exists('config.lua') then
-- Add the currentdir to the package.path
package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua'
-- Set config.lua in a table
print('=> Fetching the configuration file content ...')
config = require 'config'
-- Absolute path to the 'content/' directory
local contentdir = lfs.currentdir() .. '/' .. config.paths.content
print('=> Fetching the templates ...')
templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates))
print('=> Fetching the markdown and HTML content ...')
for filepath in dirtree.get(contentdir) do
if file.is_markdown(filepath) or file.is_html(filepath) then
local meta = model.set(filepath, lume.extend(config, {templates = templates}), contentdir)
sitemap[#sitemap+1] = meta
end
end
print('=> '..lume.count(sitemap)..' content found')
print('=> Making the web site ...')
-- Sort before make the website
table.sort(sitemap, function(a, b) return a.idorder > b.idorder end)
site.make(sitemap, args['export'], timestart)
-- Make and export the sitemap.xml
if config.sitemapxml and args['export'] then
local sitemapxml_xml, sitemapxml_xml_path = sitemapxml.make(
sitemap, templates, config.paths.public_html
)
file.export(sitemapxml_xml_path, sitemapxml_xml)
end
return
else
print('There is no "config.lua" here.')
os.exit()
end
end