satelito

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

init.lua (5354B)


      1 #!/usr/bin/env lua
      2 --
      3 local argparse = require 'argparse'
      4 local lfs = require 'lfs'
      5 local inspect = require 'inspect'
      6 --
      7 local lume = require 'satelito.lib.lume.lume'
      8 local model = require 'satelito.model'
      9 local assets = require 'satelito.assets'
     10 local dirtree = require 'satelito.dirtree'
     11 local feed = require 'satelito.feed'
     12 local file = require 'satelito.file'
     13 local site = require 'satelito.site'
     14 local page = require 'satelito.page'
     15 --
     16 local init
     17 local pipe
     18 local make
     19 
     20 local parser = argparse()
     21   :name 'satelito'
     22   :description 'Satelito is a static site generator in lua script.'
     23   :epilog 'For more info, see https://soucy.cc/git/satelito/file/README.md.html'
     24 local args
     25 
     26 -- Set 'init' command
     27 init = parser:command('init', 'Init the sample website in your $HOME folder.')
     28 -- Set 'pipe' command
     29 pipe = parser:command('pipe', 'Build the site by inputing one or more markdown files through the pipeline.')
     30 pipe:flag('-e --export', 'Export the outputed HTML in the *config.paths.public_html* folder.')
     31 -- Set 'make' command
     32 make = parser:command('make', 'Build the site from his directory.')
     33 make:flag('-e --export', 'Export the outputed HTML in the *config.paths.public_html* folder.')
     34 
     35 args = parser:parse()
     36 
     37 -- Example '$ satelito init'
     38 if args['init'] then
     39   os.execute('mkdir ~/satelito-sample')
     40   os.execute('git clone git://soucy.cc/satelito-sample.git ~/satelito-sample')
     41   print('--------------------------------------------------------------------------------------------')
     42   os.execute('rm -Rf ~/satelito-sample/.git')
     43   os.execute('ls -la ~/satelito-sample')
     44   print('--------------------------------------------------------------------------------------------')
     45   print('You should rename `~/satelito-sample` and edit `~/satelito-sample/config.lua` according to your needs.')
     46   print('--------------------------------------------------------------------------------------------')
     47 
     48   return
     49 end
     50 
     51 -- Example: '$ find site/content/  -name "*.md" | satelito pipe'
     52 if args['pipe'] then
     53   local timestart = os.time()
     54   local config
     55   local templates
     56   local sitemap = {}
     57 
     58   for filepath in (io.lines()) do
     59     if file.is_markdown(filepath) or file.is_html(filepath) then
     60       local configpath = site.get_config(filepath)
     61 
     62       -- Change the current directory to the config's directory
     63       lfs.chdir(file.get_dirname(configpath))
     64 
     65       if file.exists(configpath) then
     66         -- Add the currentdir to the package.path
     67         package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua'
     68 
     69         -- Set config.lua in a table
     70         config = require 'config'
     71 
     72         -- Absolute path to the 'content/' directory
     73         local contentdir = lfs.currentdir() .. '/' .. config.paths.content
     74 
     75         local meta = model.set(filepath, config, contentdir)
     76 
     77         sitemap[meta.relpath] = lume.extend({}, meta)
     78       end
     79     end
     80   end
     81 
     82   -- Array of templates
     83   templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates))
     84 
     85   for k, v in pairs(sitemap) do
     86     v.site = sitemap
     87     local html, html_path = page.make(v, templates)
     88     local feed_xml, feed_xml_path
     89 
     90     if args['export'] then
     91       file.export(html_path, html)
     92     else
     93       print(html)
     94     end
     95 
     96     if file.is_index(k) and args['export'] then
     97       feed_xml, feed_xml_path = feed.make(v, templates)
     98 
     99       file.export(feed_xml_path, feed_xml)
    100     end
    101 
    102     -- Copy assets to the public_html/ folder
    103     if args['export'] then
    104       assets.export(v)
    105     end
    106   end
    107 
    108   print('Satelito built ' .. lume.count(sitemap) .. ' HTML page(s) in approximately ' .. os.difftime(os.time(), timestart) .. ' second(s).')
    109   return
    110 end
    111 
    112 -- Example: '$ satelito make --export'
    113 if args['make'] then
    114   local config
    115 
    116   if file.exists('config.lua') then
    117     -- Add the currentdir to the package.path
    118     package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua'
    119 
    120     local timestart = os.time()
    121     local sitemap = {}
    122 
    123     -- Set config.lua in a table
    124     config = require 'config'
    125 
    126     -- Absolute path to the 'content/' directory
    127     local contentdir = lfs.currentdir() .. '/' .. config.paths.content
    128 
    129     -- Array of templates
    130     local templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. config.paths.templates))
    131 
    132     --
    133     for filepath in dirtree.get(contentdir) do
    134       if file.is_markdown(filepath) or file.is_html(filepath) then
    135         local meta = model.set(filepath, config, contentdir)
    136 
    137         sitemap[meta.relpath] = lume.extend({}, meta)
    138       end
    139     end
    140 
    141     --
    142     for k, v in pairs(sitemap) do
    143       v.site = sitemap
    144       local html, html_path = page.make(v, templates)
    145       local feed_xml, feed_xml_path
    146 
    147       if args['export'] then
    148         file.export(html_path, html)
    149       else
    150         print(html)
    151       end
    152 
    153       if file.is_index(k) and args['export'] then
    154         feed_xml, feed_xml_path = feed.make(v, templates)
    155 
    156         file.export(feed_xml_path, feed_xml)
    157       end
    158 
    159       -- Copy assets to the public_html/ folder
    160       if args['export'] then
    161         assets.export(v)
    162       end
    163     end
    164 
    165     print('Satelito built ' .. lume.count(sitemap) .. ' HTML page(s) in approximately ' .. os.difftime(os.time(), timestart) .. ' second(s).')
    166     --print('Satelito built that site in approximately ' .. os.difftime(os.time(), timestart) .. ' second(s).')
    167   else
    168     print('There is no "config.lua" here.')
    169     os.exit()
    170   end
    171 end