satelito

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

site.lua (4522B)


      1 -- @module site
      2 local site = {}
      3 --
      4 local inspect = require 'inspect'
      5 local lfs = require 'lfs'
      6 --
      7 local assets = require 'satelito.assets'
      8 local feed = require 'satelito.feed'
      9 local file = require 'satelito.file'
     10 local list = require 'satelito.list'
     11 local lume = require 'satelito.lib.lume.lume'
     12 local page = require 'satelito.page'
     13 local sitemapxml = require 'satelito.sitemapxml'
     14 
     15 --- From a filepath get the closest 'config.lua' by climbing the
     16 -- directory tree
     17 -- Recursive function
     18 function site.get_config(filepath)
     19   assert(filepath and filepath ~= '', 'The filepath parameter is missing or empty.')
     20 
     21   local dir = filepath:match("(.*/)")
     22   local dir_parent = string.sub(dir, 1, -2):match("(.*/)")
     23 
     24   for entry in lfs.dir(dir) do
     25     if entry and entry == 'config.lua' then
     26       return dir .. entry
     27     end
     28   end
     29 
     30   return site.get_config(dir_parent)
     31 end
     32 
     33 -- Make the site (from the sitedata)
     34 function site.make(sitedata)
     35   local duration
     36   local config = _G.Satelito.config
     37   local contentdir = _G.Satelito.contentdir
     38   local export = _G.Satelito.args['export']
     39   local make = _G.Satelito.args['make']
     40   local publicdir = _G.Satelito.publicdir
     41   local templates = _G.Satelito.templates
     42   local timestart = _G.Satelito.timestart
     43 
     44   for i = 1, #sitedata do
     45     local html, html_path
     46     local feed_xml, feed_xml_path
     47     local paginated
     48 
     49     sitedata[i].index = i
     50 
     51     -- Lists
     52     -- Subpages from the actual index page
     53     if sitedata[i].list ~= false and file.is_index(sitedata[i].path) then
     54       sitedata[i].children = list.get_children(sitedata[i].list, sitedata, sitedata[i].asc)
     55     end
     56 
     57     -- Collections
     58     -- Group of pages from specific relpaths
     59     if sitedata[i].collection and sitedata[i].collection_list then
     60       sitedata[i].collection = list.get_children(sitedata[i].collection_list, sitedata, sitedata[i].asc)
     61     end
     62 
     63     -- Archives
     64     if sitedata[i].archives then
     65       sitedata[i].archives_children = list.get_archives(contentdir)
     66     end
     67 
     68     -- Tags
     69     if sitedata[i].tags then
     70       sitedata[i].tags_children = list.get_tags(contentdir)
     71     end
     72 
     73     if i > 1 then
     74       sitedata[i].relprev = sitedata[i-1]
     75     end
     76 
     77     if i < #sitedata then
     78       sitedata[i].relnext = sitedata[i+1]
     79     end
     80 
     81     -- If a pagination is requested
     82     if sitedata[i].pagination and file.is_index(sitedata[i].relpath) then
     83       print('=> A pagination is requested ...')
     84 
     85       sitedata[i].pagination.limit = sitedata[i].pagination.limit or 6
     86       sitedata[i].pagination.prefix = sitedata[i].pagination.prefix or 'pg-'
     87 
     88       -- Split page children by pagination limit
     89       paginated = list.set_pagination(sitedata[i].collection or sitedata[i].children, sitedata[i].pagination.limit)
     90 
     91       -- Set the pagination length
     92       sitedata[i].pagination.length = #paginated
     93 
     94       print('=> Making the pagination pages ...')
     95 
     96       for p = 1, #paginated do
     97         sitedata[i].children = paginated[p]
     98         sitedata[i].pagination.current = p
     99         -- Make the pagination pages
    100         if p == 1 then
    101           html, html_path = page.make(sitedata[i])
    102         else
    103           html, html_path = page.make(
    104             sitedata[i],
    105             sitedata[i].exportlink:match("(.*/)")..sitedata[i].pagination.prefix..p..'.html'
    106           )
    107         end
    108         -- Export the pagination pages
    109         if export then
    110           file.export(html_path, html)
    111         else
    112           print(html)
    113         end
    114       end
    115 
    116     else
    117       -- Make the page
    118       html, html_path = page.make(sitedata[i])
    119 
    120       -- Export the page
    121       if export then
    122         file.export(html_path, html)
    123       else
    124         print(html)
    125       end
    126     end
    127 
    128     -- Feed
    129     if file.is_index(sitedata[i].relpath) and export then
    130       feed_xml, feed_xml_path = feed.make(sitedata[i])
    131       file.export(feed_xml_path, feed_xml)
    132     end
    133 
    134     -- Copy assets to the public_html/ folder
    135     if export then
    136       assets.export(sitedata[i])
    137     end
    138   end
    139 
    140   -- Make and export the sitemap.xml
    141   if config.sitemapxml and make and export then
    142     local sitemapxml_xml, sitemapxml_xml_path = sitemapxml.make(
    143       sitedata, templates, publicdir
    144     )
    145 
    146     file.export(sitemapxml_xml_path, sitemapxml_xml)
    147   end
    148 
    149   duration = os.difftime(os.time(), timestart) > 0
    150     and 'in approximately '..os.difftime(os.time(), timestart)..' second(s).'
    151     or 'in less than 1 second.'
    152 
    153   print('--------------------------------------------------------------------------')
    154   print('Satelito built '..lume.count(sitedata)..' HTML page(s) '..duration)
    155 end
    156 
    157 return site