diff options
author | sanine <sanine.not@pm.me> | 2022-01-04 15:05:41 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-01-04 15:05:41 -0600 |
commit | cc82d13a4f80a5a2483581dd679f06772a9e3a19 (patch) | |
tree | 6cb096ade3e7db59cc91cd836d6e7de95a01cec4 /src/lua-script | |
parent | a026db7389c15eb5a8905e6f04eb3a92bf31f9bc (diff) |
add layout loading
Diffstat (limited to 'src/lua-script')
-rw-r--r-- | src/lua-script/script.h | 59 | ||||
-rw-r--r-- | src/lua-script/script.lua | 59 |
2 files changed, 108 insertions, 10 deletions
diff --git a/src/lua-script/script.h b/src/lua-script/script.h index 6bd07bb..0bb6451 100644 --- a/src/lua-script/script.h +++ b/src/lua-script/script.h @@ -47,6 +47,40 @@ const char *argent_script = " end\n" "\n" "\n" + " function load_layouts(directory, parent, layouts)\n" + " local directory = add_end_slash(directory) or ''\n" + " local parent = parent or ''\n" + " local layouts = layouts or {}\n" + "\n" + " local fullpath = parent..directory\n" + " local dirs, files = argent.scanDirectory(argent.config.layout_directory..fullpath)\n" + "\n" + " for _, file in ipairs(files) do\n" + " if string.match(file, '%.lua$') then\n" + " local basename = string.gsub(file, '%.lua$', '')\n" + " local id = string.gsub(fullpath..basename, '/', '.')\n" + " argent.log('debug', fmt('loading layout %q', id))\n" + " success, result = pcall(loadfile(argent.config.layout_directory..fullpath..file), 0, 1)\n" + " if not success then\n" + " argent.log('error', result)\n" + " argent.log('warn', fmt('layout %q will not be available', id))\n" + " else\n" + " layouts[id] = result\n" + " end\n" + " end\n" + " end\n" + "\n" + " for _, dir in ipairs(dirs) do\n" + " if dir ~= '.' and dir ~= '..' then\n" + " argent.log('debug', fmt('loading layouts from %q', parent..directory))\n" + " load_layouts(dir, parent..directory, layouts)\n" + " end\n" + " end\n" + "\n" + " return layouts\n" + " end\n" + "\n" + "\n" " function setup(config)\n" " argent.log('debug', 'begin setup')\n" " \n" @@ -73,6 +107,13 @@ const char *argent_script = " argent.log('info', 'noprocess: '..set_tostring(argent.config.noprocess))\n" " argent.log('info', 'rss files: '..set_tostring(argent.config.rss_include))\n" "\n" + " if argent.config.layout_directory then\n" + " argent.layouts = load_layouts()\n" + " else\n" + " argent.layouts = {}\n" + " end\n" + " argent.log('info', fmt('available layouts: %s', set_tostring(argent.layouts)))\n" + " \n" " if argent.config.plugin_directory then\n" " package.path = argent.config.plugin_directory..'?.lua;'..package.path\n" " end\n" @@ -116,11 +157,11 @@ const char *argent_script = "\n" " function obliterate_file(file, parent)\n" " if argent.config.keep[file] then\n" - " argent.log('debug', fmt('retaining file %q', file))\n" + " argent.log('debug', fmt('retaining file %q', parent..file))\n" " return true\n" " end\n" "\n" - " argent.log('debug', fmt('removing file %q', file))\n" + " argent.log('debug', fmt('removing file %q', parent..file))\n" " os.remove(argent.config.output_directory..parent..file)\n" " return false\n" " end\n" @@ -128,7 +169,7 @@ const char *argent_script = "\n" " function obliterate_dir(dir, parent)\n" " if argent.config.keep[strip_end_slash(dir)] then\n" - " argent.log('debug', fmt('retaining directory %q', dir))\n" + " argent.log('debug', fmt('retaining directory %q', parent..dir))\n" " return true\n" " end\n" "\n" @@ -173,11 +214,16 @@ const char *argent_script = " function process_file(filename, parent)\n" " if (is_dotfile(filename) and not argent.config.include[filename])\n" " or argent.config.exclude[filename]\n" - " then return end\n" + " then\n" + " argent.log('debug', fmt('will not process file %q', parent..filename))\n" + " return\n" + " end\n" "\n" " if string.match(filename, '%.lua$') and not argent.config.noprocess[filename] then\n" + " argent.log('debug', fmt('processing %q as lua file', parent..filename))\n" " process_lua_file(filename, parent)\n" " else\n" + " argent.log('debug', fmt('processing %q as regular file', parent..filename))\n" " argent.copyFile(\n" " argent.config.site_directory..parent..filename,\n" " argent.config.output_directory..parent..filename\n" @@ -200,7 +246,10 @@ const char *argent_script = " function process_dir(directory, parent)\n" " if (is_dotfile(directory) and not argent.config.include[strip_end_slash(directory)])\n" " or argent.config.exclude[strip_end_slash(directory)]\n" - " then return end\n" + " then\n" + " argent.log('debug', fmt('will not process directory %q', parent..directory))\n" + " return\n" + " end\n" "\n" " if not directory_exists(directory, parent) then\n" " argent.createDirectory(argent.config.output_directory..parent..directory)\n" diff --git a/src/lua-script/script.lua b/src/lua-script/script.lua index ccfe902..e5da91f 100644 --- a/src/lua-script/script.lua +++ b/src/lua-script/script.lua @@ -46,6 +46,40 @@ return function(config) end + function load_layouts(directory, parent, layouts) + local directory = add_end_slash(directory) or '' + local parent = parent or '' + local layouts = layouts or {} + + local fullpath = parent..directory + local dirs, files = argent.scanDirectory(argent.config.layout_directory..fullpath) + + for _, file in ipairs(files) do + if string.match(file, '%.lua$') then + local basename = string.gsub(file, '%.lua$', '') + local id = string.gsub(fullpath..basename, '/', '.') + argent.log('debug', fmt('loading layout %q', id)) + success, result = pcall(loadfile(argent.config.layout_directory..fullpath..file), 0, 1) + if not success then + argent.log('error', result) + argent.log('warn', fmt('layout %q will not be available', id)) + else + layouts[id] = result + end + end + end + + for _, dir in ipairs(dirs) do + if dir ~= '.' and dir ~= '..' then + argent.log('debug', fmt('loading layouts from %q', parent..directory)) + load_layouts(dir, parent..directory, layouts) + end + end + + return layouts + end + + function setup(config) argent.log('debug', 'begin setup') @@ -72,6 +106,13 @@ return function(config) argent.log('info', 'noprocess: '..set_tostring(argent.config.noprocess)) argent.log('info', 'rss files: '..set_tostring(argent.config.rss_include)) + if argent.config.layout_directory then + argent.layouts = load_layouts() + else + argent.layouts = {} + end + argent.log('info', fmt('available layouts: %s', set_tostring(argent.layouts))) + if argent.config.plugin_directory then package.path = argent.config.plugin_directory..'?.lua;'..package.path end @@ -115,11 +156,11 @@ return function(config) function obliterate_file(file, parent) if argent.config.keep[file] then - argent.log('debug', fmt('retaining file %q', file)) + argent.log('debug', fmt('retaining file %q', parent..file)) return true end - argent.log('debug', fmt('removing file %q', file)) + argent.log('debug', fmt('removing file %q', parent..file)) os.remove(argent.config.output_directory..parent..file) return false end @@ -127,7 +168,7 @@ return function(config) function obliterate_dir(dir, parent) if argent.config.keep[strip_end_slash(dir)] then - argent.log('debug', fmt('retaining directory %q', dir)) + argent.log('debug', fmt('retaining directory %q', parent..dir)) return true end @@ -172,11 +213,16 @@ return function(config) function process_file(filename, parent) if (is_dotfile(filename) and not argent.config.include[filename]) or argent.config.exclude[filename] - then return end + then + argent.log('debug', fmt('will not process file %q', parent..filename)) + return + end if string.match(filename, '%.lua$') and not argent.config.noprocess[filename] then + argent.log('debug', fmt('processing %q as lua file', parent..filename)) process_lua_file(filename, parent) else + argent.log('debug', fmt('processing %q as regular file', parent..filename)) argent.copyFile( argent.config.site_directory..parent..filename, argent.config.output_directory..parent..filename @@ -199,7 +245,10 @@ return function(config) function process_dir(directory, parent) if (is_dotfile(directory) and not argent.config.include[strip_end_slash(directory)]) or argent.config.exclude[strip_end_slash(directory)] - then return end + then + argent.log('debug', fmt('will not process directory %q', parent..directory)) + return + end if not directory_exists(directory, parent) then argent.createDirectory(argent.config.output_directory..parent..directory) |