satelito

Static [web] site (or page) generator (ssg) made with Lua script.
git clone git://soucy.cc/satelito.git
Log | Files | Refs | README

commit 051bc31e847c9824750283efe86fa9279c387264
parent be089e5e0efc4a6ce9fad8bc35ca67a1500ad779
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Fri,  9 Apr 2021 14:37:47 -0400

Refactor the init script

Diffstat:
Msatelito/init.lua | 177++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 84 insertions(+), 93 deletions(-)

diff --git a/satelito/init.lua b/satelito/init.lua @@ -1,34 +1,40 @@ #!/usr/bin/env lua -- -local model = require 'satelito.model' local argparse = require 'argparse' -local inspect = require 'inspect' local lfs = require 'lfs' +local inspect = require 'inspect' +-- local lume = require 'satelito.lib.lume.lume' +local model = require 'satelito.model' local assets = require 'satelito.assets' local dirtree = require 'satelito.dirtree' local feed = require 'satelito.feed' local file = require 'satelito.file' +local site = require 'satelito.site' local page = require 'satelito.page' +-- +local init +local pipe +local make + local parser = argparse() - :require_command(false) :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 -parser:mutex( - parser:option('-f --file', 'Input a markdown filepath.'), - parser:flag('-p --pipeline', 'Input one or more markdown files through the pipeline.') -) -parser:flag('-e --export', 'Export the outputed HTML in the *paths.public_html* folder.') - -local init = parser:command('init', 'Init the sample website in your $HOME folder.') -local make = parser:command('make', 'Explicitly process the content in site mode instead of the default page mode.') -local test = parser:command('test', 'Test some features') +-- 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.') args = parser:parse() +-- Example '$ satelito init' if args['init'] then os.execute('mkdir ~/satelito-sample') os.execute('git clone git://soucy.cc/satelito-sample.git ~/satelito-sample') @@ -36,106 +42,75 @@ if args['init'] then os.execute('rm -Rf ~/satelito-sample/.git') os.execute('ls -la ~/satelito-sample') print('--------------------------------------------------------------------------------------------') - print('You shoul rename `~/satelito-sample` and edit `~/sample/config.lua` according to your needs.') + print('You shoul rename `~/satelito-sample` and edit `~/satelito-sample/config.lua` according to your needs.') print('--------------------------------------------------------------------------------------------') return end -if args['file'] and file.is_markdown(args['file']) then - local html, html_path = page.build(args['file'], model.get(args['file'])) - - if args['export'] then - page.export(html_path, html) - else - print(html) - end - - return -end +-- Example: '$ find site/content/ -name "*.md" | satelito pipe' +if args['pipe'] then + local timestart = os.time() + local config + local templates + local sitemap = {} -if args['pipeline'] then - -- print('Enter a filepath:') - -- for filepath in (io.lines()) do if file.is_markdown(filepath) then - local filemeta = model.get(filepath) - local html, html_path = page.build(filepath, filemeta) - local feed_xml, feed_xml_path + local configpath = site.get_config(filepath) - if args['export'] then - page.export(html_path, html) + -- Change the current directory to the config's directory + lfs.chdir(file.get_dirname(configpath)) - -- If filepath is an index - -- Then build and export his feed - if file.is_index(filepath) then - feed_xml, feed_xml_path = feed.build(filepath, filemeta) - feed.export(feed_xml_path, feed_xml) - end + if file.exists(configpath) then + -- Add the currentdir to the package.path + package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua' - -- Copy assets to the public_html/ folder - assets.export(filepath) - else - print(html) - end - else - print("Wrong data type!") - os.exit() - end - end - - return -end - -if args['make'] then - local config - local contentdir + -- Set config.lua in a table + config = require 'config' - if file.exists('config.lua') then - -- Add the currentdir to the package.path - package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua' + -- Absolute path to the 'content/' directory + local contentdir = lfs.currentdir() .. '/' .. config.paths.content - local counter = 0 - local timestart = os.time() - - -- Set config.lua in a table - config = require 'config' + local meta = model.set(filepath, config, contentdir) - -- Absolute path to the content directory - contentdir = lfs.currentdir() .. '/' .. config.paths.content + sitemap[meta.relpath] = lume.extend({}, meta) + end + end + end - for filepath in dirtree.get(contentdir) do - if file.is_markdown(filepath) then - local filemeta = model.get(filepath, config) - local html, html_path = page.build(filepath, filemeta) - local feed_xml, feed_xml_path + -- Array of templates + templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates)) - counter = counter+1 + for k, v in pairs(sitemap) do + v.site = sitemap + local html, html_path = page.make(v, templates) + local feed_xml, feed_xml_path - if args['export'] then - page.export(html_path, html) + if args['export'] then + file.export(html_path, html) + else + print(html) + end - -- If filepath is an index - -- Then build and export his feed - if file.is_index(filepath) then - feed_xml, feed_xml_path = feed.build(filepath, filemeta) - feed.export(feed_xml_path, feed_xml) - end + if file.is_index(k) and args['export'] then + feed_xml, feed_xml_path = feed.make(v, templates) - -- Copy assets to the public_html/ folder - assets.export(filepath) - end - end + file.export(feed_xml_path, feed_xml) end - print('Satelito builded ' .. counter .. ' HTML pages in ' .. os.difftime(os.time(), timestart) .. ' seconds.') - else - print('There is no "config.lua" here.') - os.exit() + -- Copy assets to the public_html/ folder + if args['export'] then + assets.export(v) + end end + + print('Satelito built ' .. lume.count(sitemap) .. ' HTML page(s) in approximately ' .. os.difftime(os.time(), timestart) .. ' second(s).') + return end -if args['test'] then +-- Example: '$ satelito make --export' +if args['make'] then local config if file.exists('config.lua') then @@ -154,6 +129,7 @@ if args['test'] then -- Array of templates local templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates)) + -- for filepath in dirtree.get(contentdir) do if file.is_markdown(filepath) then local meta = model.set(filepath, config, contentdir) @@ -162,16 +138,31 @@ if args['test'] then end end - for _, v in pairs(sitemap) do + -- + for k, v in pairs(sitemap) do v.site = sitemap local html, html_path = page.make(v, templates) + local feed_xml, feed_xml_path - page.export(html_path, html) - end + if args['export'] then + file.export(html_path, html) + else + print(html) + end + + if file.is_index(k) and args['export'] then + feed_xml, feed_xml_path = feed.make(v, templates) + + file.export(feed_xml_path, feed_xml) + end - --print(inspect(sitemap)) + -- Copy assets to the public_html/ folder + if args['export'] then + assets.export(v) + end + end - print('Satelito builded in ' .. 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()