satelito

Static site generator made with Lua script.
git clone git://soucy.cc/satelito.git
Log | Files | Refs | README

site.lua (4796B)


      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 verbose = _G.Satelito.args['verbose']
     40   local make = _G.Satelito.args['make']
     41   local publicdir = _G.Satelito.publicdir
     42   local templates = _G.Satelito.templates
     43   local timestart = _G.Satelito.timestart
     44 
     45   for i = 1, #sitedata do
     46     local html, html_path
     47     local feed_xml, feed_xml_path
     48     local paginated
     49 
     50     if verbose then
     51       io.write('   ¬ ./' .. config.paths.content..sitedata[i].relpath)
     52     end
     53 
     54     sitedata[i].index = i
     55 
     56     -- Lists
     57     -- Subpages from the actual index page
     58     if sitedata[i].list ~= false and file.is_index(sitedata[i].path) then
     59       sitedata[i].children = list.get_children(sitedata[i].list, sitedata, sitedata[i].asc)
     60     end
     61 
     62     -- Collections
     63     -- Group of pages from specific relpaths
     64     if sitedata[i].collection and sitedata[i].collection_list then
     65       sitedata[i].collection = list.get_children(sitedata[i].collection_list, sitedata, sitedata[i].asc)
     66     end
     67 
     68     -- Archives
     69     if sitedata[i].archives then
     70       sitedata[i].archives_children = list.get_archives(contentdir)
     71     end
     72 
     73     -- Tags
     74     if sitedata[i].tags then
     75       sitedata[i].tags_children = list.get_tags(contentdir)
     76     end
     77 
     78     if i > 1 then
     79       sitedata[i].relprev = sitedata[i-1]
     80     end
     81 
     82     if i < #sitedata then
     83       sitedata[i].relnext = sitedata[i+1]
     84     end
     85 
     86     -- If a pagination is requested
     87     if sitedata[i].pagination and file.is_index(sitedata[i].relpath) then
     88       print('=> A pagination is requested ...')
     89 
     90       sitedata[i].pagination.limit = sitedata[i].pagination.limit or 6
     91       sitedata[i].pagination.prefix = sitedata[i].pagination.prefix or 'pg-'
     92 
     93       -- Split page children by pagination limit
     94       paginated = list.set_pagination(sitedata[i].collection or sitedata[i].children, sitedata[i].pagination.limit)
     95 
     96       -- Set the pagination length
     97       sitedata[i].pagination.length = #paginated
     98 
     99       print('=> Making the pagination pages ...')
    100 
    101       for p = 1, #paginated do
    102         sitedata[i].children = paginated[p]
    103         sitedata[i].pagination.current = p
    104         -- Make the pagination pages
    105         if p == 1 then
    106           html, html_path = page.make(sitedata[i])
    107         else
    108           html, html_path = page.make(
    109             sitedata[i],
    110             sitedata[i].exportlink:match("(.*/)")..sitedata[i].pagination.prefix..p..'.html'
    111           )
    112         end
    113         -- Export the pagination pages
    114         if export then
    115           file.export(html_path, html)
    116         else
    117           print(html)
    118         end
    119       end
    120 
    121     else
    122       -- Make the page
    123       html, html_path = page.make(sitedata[i])
    124 
    125       -- Export the page
    126       if export then
    127         file.export(html_path, html)
    128       else
    129         print(html)
    130       end
    131     end
    132 
    133     -- Feed
    134     if file.is_index(sitedata[i].relpath) and export then
    135       feed_xml, feed_xml_path = feed.make(sitedata[i])
    136       file.export(feed_xml_path, feed_xml)
    137     end
    138 
    139     -- Copy assets to the public_html/ folder
    140     if export then
    141       assets.export(sitedata[i])
    142     end
    143 
    144     if verbose then
    145       io.write(' > ./'.. config.paths.public_html:sub(1, -2) .. sitedata[i].rellink .. ' ✔ \n')
    146     end
    147   end
    148 
    149   -- Make and export the sitemap.xml
    150   if config.sitemapxml and make and export then
    151     local sitemapxml_xml, sitemapxml_xml_path = sitemapxml.make(
    152       sitedata, templates, publicdir
    153     )
    154 
    155     file.export(sitemapxml_xml_path, sitemapxml_xml)
    156   end
    157 
    158   duration = os.difftime(os.time(), timestart) > 0
    159     and 'in approximately '..os.difftime(os.time(), timestart)..' second(s).'
    160     or 'in less than 1 second.'
    161 
    162   print('--------------------------------------------------------------------------')
    163   print('Satelito built '..lume.count(sitedata)..' HTML page(s) '..duration)
    164 end
    165 
    166 return site