init.lua (5772B)
1 #!/usr/bin/env lua 2 -- 3 local argparse = require 'argparse' 4 local inspect = require 'inspect' 5 local lfs = require 'lfs' 6 -- 7 local lume = require 'satelito.lib.lume.lume' 8 local model = require 'satelito.model' 9 local dirtree = require 'satelito.dirtree' 10 local file = require 'satelito.file' 11 local site = require 'satelito.site' 12 -- 13 local init 14 local pipe 15 local make 16 local exec 17 local reset 18 19 local parser = argparse() 20 :name 'satelito' 21 :description 'Satelito is a static site generator in lua script.' 22 :epilog 'For more info, see https://soucy.cc/git/satelito/file/README.md.html' 23 local args 24 25 -- Set 'init' command 26 init = parser:command('init', 'Init the sample website in your $HOME folder.') 27 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 32 -- Set 'make' command 33 make = parser:command('make', 'Build the site from his directory.') 34 make:flag('-e --export', 'Export the outputed HTML in the *config.paths.public_html* folder.') 35 make:flag('-v --verbose', 'Displays in the terminal the source and destination of each content.') 36 37 -- Set the exec command 38 exec = parser:command('exec', 'Execute a script from the site\'s bin directory.') 39 exec:argument 'bin name' 40 -- Set the reset command 41 reset = parser:command('reset', 'Delete HTML and XML files in the public_html directory.') 42 43 args = parser:parse() 44 45 -- Started 46 print('=> ☾ Satelito is here ☽ ...') 47 48 ------------- 49 -- GLOBALS -- 50 ------------- 51 52 -- Set the Satelito global table 53 _G.Satelito = {} 54 _G.Satelito.timestart = os.time() 55 _G.Satelito.args = args 56 57 -- Put config.lua in a table 58 print('=> Fetching the configuration file content ...') 59 _G.Satelito.config = dofile(site.get_config(lfs.currentdir()..'/')) 60 61 -- Change current directory for the config.lua's directory 62 print('=> Moving where the site configuration file is located ...') 63 lfs.chdir(file.get_dirname(site.get_config(lfs.currentdir()..'/'))) 64 65 -- Then add the current directory to the package.path 66 package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua' 67 68 -- Get the absolute path for the 'content' directory 69 _G.Satelito.contentdir = lfs.currentdir()..'/'.._G.Satelito.config.paths.content 70 71 -- Get the absolute path for the 'public_html' directory 72 _G.Satelito.publicdir = lfs.currentdir()..'/'.._G.Satelito.config.paths.public_html 73 74 -- Get the list of templates 75 if args['make'] or args['pipe'] then 76 print('=> Fetching the templates ...') 77 _G.Satelito.templates = lume.array(dirtree.get(lfs.currentdir() .. '/' .. _G.Satelito.config.paths.templates)) 78 end 79 80 --print(inspect(_G.Satelito)) 81 82 ---------- 83 -- EXEC -- 84 ---------- 85 86 if args['exec'] then 87 package.path = package.path .. ';'.. lfs.currentdir() ..'/?.lua' 88 89 print(args['bin name'], inspect(lfs.currentdir())) 90 91 if args['bin name'] then 92 dofile(lfs.currentdir() .. '/bin/'..args['bin name']) 93 end 94 95 return 96 end 97 98 ---------- 99 -- INIT -- 100 ---------- 101 102 -- Initialize the satelito sample site in $HOME 103 -- Example '$ satelito init' 104 if args['init'] then 105 os.execute('mkdir ~/satelito-sample') 106 os.execute('git clone git://soucy.cc/satelito-sample.git ~/satelito-sample') 107 print('------------------------------------------------------------------------------------------------------') 108 os.execute('rm -Rf ~/satelito-sample/.git') 109 os.execute('ls -la ~/satelito-sample') 110 print('------------------------------------------------------------------------------------------------------') 111 print('You should rename `~/satelito-sample` and edit `~/satelito-sample/config.lua` according to your needs.') 112 print('------------------------------------------------------------------------------------------------------') 113 114 return 115 end 116 117 ---------- 118 -- PIPE -- 119 ---------- 120 121 -- Pipe stdout into satelito 122 -- Example: '$ find site/content/ | satelito pipe' 123 if args['pipe'] then 124 local sitedata = {} 125 126 for filepath in (io.lines()) do 127 if file.is_content(filepath) then 128 -- Get the pagedata of the file 129 local pagedata = model.set(filepath) 130 131 -- Add the pagedata of the file into the sitedata table 132 if lume.count(sitedata) == 0 then 133 print('=> Fetching the markdown and HTML content ...') 134 end 135 136 sitedata[#sitedata+1] = pagedata 137 end 138 end 139 140 print('=> '..lume.count(sitedata)..' content found') 141 print('=> Making the web site ...') 142 143 -- Sorting by alphanum 144 table.sort(lume.unique(sitedata), function(a, b) return a.idorder > b.idorder end) 145 146 return site.make(sitedata) 147 end 148 149 ---------- 150 -- MAKE -- 151 ---------- 152 153 -- Make command 154 -- Example: '$ satelito make --export' 155 if args['make'] then 156 local contentdir = _G.Satelito.contentdir 157 local sitedata = {} 158 159 print('=> Fetching the markdown and HTML content ...') 160 161 for filepath in dirtree.get(contentdir) do 162 if file.is_content(filepath) then 163 local pagedata = model.set(filepath) 164 165 sitedata[#sitedata+1] = pagedata 166 end 167 end 168 169 print('=> '..lume.count(sitedata)..' content found') 170 print('=> Making the web site ...') 171 172 -- Sort before make the website 173 table.sort(lume.unique(sitedata), function(a, b) return a.idorder > b.idorder end) 174 175 return site.make(sitedata) 176 end 177 178 ----------- 179 -- RESET -- 180 ----------- 181 182 if args['reset'] then 183 local publicdir = _G.Satelito.publicdir 184 local yes_or_no 185 186 repeat 187 print('=> Reset ...') 188 print('=> Are you sure you want to delete HTML and XML files in the "'..publicdir..'" folder?') 189 print('=> Please answer with "Yes" or "No" ...') 190 io.flush() 191 192 yes_or_no = string.lower(tostring(io.read())) 193 until yes_or_no == 'yes' or yes_or_no == 'no' 194 195 if yes_or_no == 'yes' then 196 dirtree.reset(publicdir) 197 198 print('=> Done') 199 else 200 print('=> Aborted') 201 end 202 203 return 204 end