summaryrefslogtreecommitdiff
path: root/src/lua-script/script.h
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-01-04 00:28:38 -0600
committersanine <sanine.not@pm.me>2022-01-04 00:28:38 -0600
commitcb4f641884ae6104f44967949f9b52a9a72f7baf (patch)
tree4819c0cc48f18b87d119d9b4bb393ef2bf4fe290 /src/lua-script/script.h
parent5830f684ba172664df43eee26636e99e5a228028 (diff)
write first-pass main lua script
Diffstat (limited to 'src/lua-script/script.h')
-rw-r--r--src/lua-script/script.h209
1 files changed, 209 insertions, 0 deletions
diff --git a/src/lua-script/script.h b/src/lua-script/script.h
new file mode 100644
index 0000000..e419923
--- /dev/null
+++ b/src/lua-script/script.h
@@ -0,0 +1,209 @@
+const char *argent_script =
+ "return function(config)\n"
+ "\n"
+ " --------------------------------\n"
+ " --\n"
+ " -- basics\n"
+ " --\n"
+ " --------------------------------\n"
+ "\n"
+ " function Set(array, transform)\n"
+ " if type(array) ~= 'table' then return nil end\n"
+ "\n"
+ " local transform = transform or function(x) return x end\n"
+ " \n"
+ " local set = {}\n"
+ " for _, element in ipairs(array) do\n"
+ " set[transform(element)] = true\n"
+ " end\n"
+ " return set\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function add_end_slash(str)\n"
+ " if type(str) ~= 'string' then return nil end\n"
+ " if string.match(str, '/$') then return str end\n"
+ " return str .. '/'\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function strip_end_slash(str)\n"
+ " if string.match(str, '/$') then return string.sub(str, 1, #str-1) end\n"
+ " return str\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function setup(config)\n"
+ " argent.config = {\n"
+ " 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 'layouts/',\n"
+ " plugin_directory = add_end_slash(config.layout_directory) or 'plugins/',\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"
+ " noprocess = Set(config.noprocess, strip_end_slash) or {},\n"
+ " rss_include = Set(config.rss_include, strip_end_slash) or {},\n"
+ " }\n"
+ "\n"
+ " package.path = argent.config.plugin_directory..'?.lua;'..package.path\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function is_dotfile(filename)\n"
+ " if string.match(filename, '^%.') then return true end\n"
+ " return false\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function directory_exists(directory, parent)\n"
+ " local directory = directory or ''\n"
+ " local parent = parent or ''\n"
+ " local dirs = argent.scanDirectory(argent.config.output_directory..parent)\n"
+ " for _, dir in ipairs(dirs) do\n"
+ " if strip_end_slash(directory) == dir then return true end\n"
+ " end\n"
+ " return false\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function output_directory_exists()\n"
+ " local dirs = argent.scanDirectory('./')\n"
+ " for _, dir in ipairs(dirs) do\n"
+ " if add_end_slash(dir) == argent.config.output_directory then\n"
+ " return true\n"
+ " end\n"
+ " end\n"
+ " return false\n"
+ " end\n"
+ "\n"
+ "\n"
+ " --------------------------------\n"
+ " --\n"
+ " -- obliterating\n"
+ " --\n"
+ " --------------------------------\n"
+ "\n"
+ " function obliterate_file(file, parent)\n"
+ " if argent.config.keep[file] then\n"
+ " return true\n"
+ " end\n"
+ "\n"
+ " os.remove(argent.config.output_directory..parent..file)\n"
+ " return false\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function obliterate_dir(dir, parent)\n"
+ " if argent.config.keep[strip_end_slash(dir)] then\n"
+ " return true\n"
+ " end\n"
+ "\n"
+ " return obliterate_directory(dir, parent)\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function obliterate_directory(dirname, parent)\n"
+ " local dirname = add_end_slash(dirname) or ''\n"
+ " local parent = parent or ''\n"
+ " \n"
+ " if argent.config.keep[dirname] then return true end\n"
+ "\n"
+ " local keep_self = (dirname == '')\n"
+ "\n"
+ " local dirs, files = argent.scanDirectory(argent.config.output_directory..parent..dirname)\n"
+ " for _, file in ipairs(files) do\n"
+ " keep_self = obliterate_file(file, parent..dirname) or keep_self\n"
+ " end\n"
+ " for _, dir in ipairs(dirs) do\n"
+ " if dir ~= '.' and dir ~= '..' then\n"
+ " keep_self = obliterate_dir(dir, parent..dirname) or keep_self\n"
+ " end\n"
+ " end\n"
+ "\n"
+ " if not keep_self then\n"
+ " os.remove(argent.config.output_directory..parent..dirname)\n"
+ " return false\n"
+ " else\n"
+ " return true\n"
+ " end\n"
+ " end\n"
+ "\n"
+ "\n"
+ " --------------------------------\n"
+ " --\n"
+ " -- processing\n"
+ " --\n"
+ " --------------------------------\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 return end\n"
+ "\n"
+ " if string.match(filename, '%.lua$') and not argent.config.noprocess[filename] then\n"
+ " process_lua_file(filename, parent)\n"
+ " else\n"
+ " argent.copyFile(\n"
+ " argent.config.site_directory..parent..filename,\n"
+ " argent.config.output_directory..parent..filename\n"
+ " )\n"
+ " end\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function process_lua_file(filename, parent)\n"
+ " local success, result = pcall(loadfile(argent.config.site_directory..parent..filename))\n"
+ " if not success then\n"
+ " print('WARNING: '..result)\n"
+ " return\n"
+ " end\n"
+ "\n"
+ " print(filename, result)\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 return end\n"
+ "\n"
+ " if not directory_exists(directory, parent) then\n"
+ " argent.createDirectory(argent.config.output_directory..parent..directory)\n"
+ " end\n"
+ " process_directory(directory, parent)\n"
+ " end\n"
+ "\n"
+ "\n"
+ " function process_directory(directory, parent)\n"
+ " local directory = add_end_slash(directory) or ''\n"
+ " local parent = add_end_slash(parent) or ''\n"
+ " local dirs, files = argent.scanDirectory(argent.config.site_directory..parent..directory)\n"
+ " for _, file in ipairs(files) do\n"
+ " process_file(file, parent..directory)\n"
+ " end\n"
+ "\n"
+ " for _, dir in ipairs(dirs) do\n"
+ " if dir ~= '.' and dir ~= '..' then\n"
+ " process_dir(dir, parent..directory)\n"
+ " end\n"
+ " end\n"
+ " end\n"
+ "\n"
+ "\n"
+ " --------------------------------\n"
+ " --\n"
+ " -- putting it all together\n"
+ " --\n"
+ " --------------------------------\n"
+ "\n"
+ " setup(config)\n"
+ " if not output_directory_exists() then\n"
+ " argent.createDirectory(argent.config.output_directory)\n"
+ " else\n"
+ " obliterate_directory()\n"
+ " end\n"
+ " process_directory()\n"
+ "end\n"
+ "\n"
+; \ No newline at end of file