From 9beef6bdace1e70b5f3097896a955892e324800a Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 4 Jan 2022 15:57:34 -0600 Subject: implement plugins directory --- src/argent.c | 1 + src/bindings.c | 11 +++++++ src/bindings.h | 2 ++ src/lua-script/script.h | 82 +++++++++++++++++++++++++++++++++++++++++------ src/lua-script/script.lua | 82 +++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 160 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/argent.c b/src/argent.c index 2c10472..f057380 100644 --- a/src/argent.c +++ b/src/argent.c @@ -45,6 +45,7 @@ int main(int argc, char **argv) hs_create_table (L, hs_str_cfunc("markdown", markdown), + hs_str_cfunc("currentWorkingDirectory", current_working_directory), hs_str_cfunc("scanDirectory", scan_directory), hs_str_cfunc("createDirectory", create_directory), hs_str_cfunc("copyFile", copy_file), diff --git a/src/bindings.c b/src/bindings.c index 7a94020..45abf6a 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -84,6 +84,17 @@ int markdown(lua_State *L) } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int current_working_directory(lua_State *L) +{ + char *cwd = getcwd(NULL, 0); + lua_pushstring(L, cwd); + free(cwd); + return 1; +} + + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ static void throw_directory_error(lua_State *L, char *dir_name); diff --git a/src/bindings.h b/src/bindings.h index 02f33c6..2e4d26c 100644 --- a/src/bindings.h +++ b/src/bindings.h @@ -5,6 +5,8 @@ int markdown(lua_State *L); +int current_working_directory(lua_State *L); + // return two arrays, one containing filenames for all directories in the dir // and the other containing filenames for all regular files int scan_directory(lua_State *L); diff --git a/src/lua-script/script.h b/src/lua-script/script.h index 0bb6451..e29003f 100644 --- a/src/lua-script/script.h +++ b/src/lua-script/script.h @@ -88,7 +88,7 @@ const char *argent_script = " site_directory = add_end_slash(config.site_directory) or 'site/',\n" " output_directory = add_end_slash(config.output_directory) or 'public/',\n" " layout_directory = add_end_slash(config.layout_directory) or nil,\n" - " plugin_directory = add_end_slash(config.layout_directory) or nil,\n" + " plugin_directory = add_end_slash(config.plugin_directory) or nil,\n" " exclude = Set(config.exclude, strip_end_slash) or {},\n" " include = Set(config.include, strip_end_slash) or {},\n" " keep = Set(config.keep, strip_end_slash) or {},\n" @@ -115,7 +115,9 @@ const char *argent_script = " 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" + " package.path =\n" + " add_end_slash(argent.currentWorkingDirectory())\n" + " ..argent.config.plugin_directory..'?.lua;'..package.path\n" " end\n" " argent.log('debug', 'end setup')\n" " end\n" @@ -211,10 +213,32 @@ const char *argent_script = " --\n" " --------------------------------\n" "\n" + " function should_include(filename, parent)\n" + " for pattern in pairs(argent.config.include) do\n" + " if string.match(filename, pattern) or string.match(parent..filename, pattern) then\n" + " return true\n" + " end\n" + " end\n" + " return false\n" + " end\n" + "\n" + " function should_ignore(filename, parent)\n" + " if is_dotfile(filename) and not should_include(filename, parent) then\n" + " return false\n" + " end\n" + "\n" + " for pattern in pairs(argent.config.exclude) do\n" + " if string.match(filename, pattern)\n" + " or string.match(parent..filename, pattern) then\n" + " return true\n" + " end\n" + " end\n" + "\n" + " return false\n" + " end\n" + "\n" " function process_file(filename, parent)\n" - " if (is_dotfile(filename) and not argent.config.include[filename])\n" - " or argent.config.exclude[filename]\n" - " then\n" + " if should_ignore(filename, parent) then\n" " argent.log('debug', fmt('will not process file %q', parent..filename))\n" " return\n" " end\n" @@ -239,14 +263,54 @@ const char *argent_script = " return\n" " end\n" "\n" - " print(filename, result)\n" + " if not type(result) == 'table' then\n" + " argent.log('error', fmt('%q returned %q instead of %q', filename, type(result), 'table'))\n" + " argent.log('warn', fmt('%q will not result in file output!', filename))\n" + " return\n" + " end\n" + "\n" + " local html\n" + " if result.html then\n" + " html = result.html\n" + " elseif result.markdown then\n" + " html = argent.markdown(result.markdown)\n" + " else\n" + " argent.log('error', fmt('%q did not specify any content!', filename))\n" + " argent.log('warn', fmt('%q will not result in file output!', filename))\n" + " return\n" + " end\n" + "\n" + " if not result.title then\n" + " result.title = string.gsub(filename, '%.lua$', '')\n" + " argent.log('warn', fmt('%q did not specify a title; using default title of %q',\n" + " filename, result.title))\n" + " end\n" + "\n" + " local layout = function(html, tbl) return html end\n" + " if result.layout then\n" + " if not argent.layouts[result.layout] then\n" + " argent.log('error', fmt('%q requested nonexistent layout %q', filename, result.layout))\n" + " argent.log('warn', fmt('%q will not result in file output!', filename))\n" + " return\n" + " else\n" + " layout = argent.layouts[result.layout]\n" + " end\n" + " else -- no layout specified\n" + " argent.log('warn', fmt(\n" + " '%q did not specify a layout; output will be naked content!',\n" + " filename))\n" + " end\n" + "\n" + " local output_data = layout(html, result)\n" + " local output_name = string.gsub(filename, '%.lua$', '.html')\n" + " local output_file = io.open(argent.config.output_directory..parent..output_name, 'w')\n" + " output_file:write(output_data)\n" + " output_file:close()\n" " end\n" "\n" "\n" " 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\n" + " if should_ignore(directory, parent) then\n" " argent.log('debug', fmt('will not process directory %q', parent..directory))\n" " return\n" " end\n" diff --git a/src/lua-script/script.lua b/src/lua-script/script.lua index e5da91f..c6c2ecb 100644 --- a/src/lua-script/script.lua +++ b/src/lua-script/script.lua @@ -87,7 +87,7 @@ return function(config) site_directory = add_end_slash(config.site_directory) or 'site/', output_directory = add_end_slash(config.output_directory) or 'public/', layout_directory = add_end_slash(config.layout_directory) or nil, - plugin_directory = add_end_slash(config.layout_directory) or nil, + plugin_directory = add_end_slash(config.plugin_directory) or nil, exclude = Set(config.exclude, strip_end_slash) or {}, include = Set(config.include, strip_end_slash) or {}, keep = Set(config.keep, strip_end_slash) or {}, @@ -114,7 +114,9 @@ return function(config) 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 + package.path = + add_end_slash(argent.currentWorkingDirectory()) + ..argent.config.plugin_directory..'?.lua;'..package.path end argent.log('debug', 'end setup') end @@ -210,10 +212,32 @@ return function(config) -- -------------------------------- + function should_include(filename, parent) + for pattern in pairs(argent.config.include) do + if string.match(filename, pattern) or string.match(parent..filename, pattern) then + return true + end + end + return false + end + + function should_ignore(filename, parent) + if is_dotfile(filename) and not should_include(filename, parent) then + return false + end + + for pattern in pairs(argent.config.exclude) do + if string.match(filename, pattern) + or string.match(parent..filename, pattern) then + return true + end + end + + return false + end + function process_file(filename, parent) - if (is_dotfile(filename) and not argent.config.include[filename]) - or argent.config.exclude[filename] - then + if should_ignore(filename, parent) then argent.log('debug', fmt('will not process file %q', parent..filename)) return end @@ -238,14 +262,54 @@ return function(config) return end - print(filename, result) + if not type(result) == 'table' then + argent.log('error', fmt('%q returned %q instead of %q', filename, type(result), 'table')) + argent.log('warn', fmt('%q will not result in file output!', filename)) + return + end + + local html + if result.html then + html = result.html + elseif result.markdown then + html = argent.markdown(result.markdown) + else + argent.log('error', fmt('%q did not specify any content!', filename)) + argent.log('warn', fmt('%q will not result in file output!', filename)) + return + end + + if not result.title then + result.title = string.gsub(filename, '%.lua$', '') + argent.log('warn', fmt('%q did not specify a title; using default title of %q', + filename, result.title)) + end + + local layout = function(html, tbl) return html end + if result.layout then + if not argent.layouts[result.layout] then + argent.log('error', fmt('%q requested nonexistent layout %q', filename, result.layout)) + argent.log('warn', fmt('%q will not result in file output!', filename)) + return + else + layout = argent.layouts[result.layout] + end + else -- no layout specified + argent.log('warn', fmt( + '%q did not specify a layout; output will be naked content!', + filename)) + end + + local output_data = layout(html, result) + local output_name = string.gsub(filename, '%.lua$', '.html') + local output_file = io.open(argent.config.output_directory..parent..output_name, 'w') + output_file:write(output_data) + output_file:close() end 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 + if should_ignore(directory, parent) then argent.log('debug', fmt('will not process directory %q', parent..directory)) return end -- cgit v1.2.1