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 | |
parent | a026db7389c15eb5a8905e6f04eb3a92bf31f9bc (diff) |
add layout loading
-rw-r--r-- | src/argent.c | 3 | ||||
-rw-r--r-- | src/lua-script/script.h | 59 | ||||
-rw-r--r-- | src/lua-script/script.lua | 59 | ||||
-rw-r--r-- | src/options.c | 18 |
4 files changed, 125 insertions, 14 deletions
diff --git a/src/argent.c b/src/argent.c index 97d6862..2c10472 100644 --- a/src/argent.c +++ b/src/argent.c @@ -31,7 +31,8 @@ int main(int argc, char **argv) struct argent_options opts; int error = parse_options(&opts, argc, argv); if (error) - return 1; + // error of '2' indicates -h was passed + return error == 2 ? 0 : 1; argent_log(INFO, "log level: %s", level_string(argent_log_level)); argent_log(INFO, "configuration file: %s", opts.conf_filename); 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) diff --git a/src/options.c b/src/options.c index d221828..e694f99 100644 --- a/src/options.c +++ b/src/options.c @@ -6,18 +6,26 @@ static void print_usage(const char *progname) { - printf("Usage: %s [-c config_file] [-i input_directory] [-o output_directory]\n", + printf("Usage: %s [-c config_file] [-v[v[v]]] [-q[q[q]]] [-h]\n" + " -v Increase output verbosity\n" + " -q Decrease output verbosity\n" + " -c Specifiy configuration file to read (default 'config.lua')\n" + " -h Print this help message and exit\n", progname); } int parse_options(struct argent_options *opts, int argc, char **argv) { - opts->log_level = 0; + opts->log_level = WARN; opts->conf_filename = "config.lua"; int opt; - while ((opt = getopt(argc, argv, "vc:")) != -1) { + while ((opt = getopt(argc, argv, "hqvc:")) != -1) { switch (opt) { + case 'q': + opts->log_level -= 1; + break; + case 'v': opts->log_level += 1; break; @@ -26,6 +34,10 @@ int parse_options(struct argent_options *opts, int argc, char **argv) opts->conf_filename = optarg; break; + case 'h': + print_usage(argv[0]); + return 2; + default: print_usage(argv[0]); return 1; |