const char *argent_script = "return function(config)\n" " local fmt = string.format\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" " function set_tostring(set)\n" " local str = '[empty]'\n" " for element in pairs(set) do\n" " if str == '[empty]' then\n" " str = tostring(element)\n" " else\n" " str = str .. '; '.. tostring(element)\n" " end\n" " end\n" "\n" " return str\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 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" " 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 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" " noprocess = Set(config.noprocess, strip_end_slash) or {},\n" " rss_include = Set(config.rss_include, strip_end_slash) or {},\n" " }\n" "\n" " argent.log('info', fmt('site directory: %s', argent.config.site_directory))\n" " argent.log('info', fmt('output directory: %s', argent.config.output_directory))\n" " argent.log('info', fmt('layout directory: %s', tostring(argent.config.layout_directory)))\n" " argent.log('info', fmt('plugin directory: %s', tostring(argent.config.plugin_directory)))\n" "\n" " argent.log('info', 'exclusions: '..set_tostring(argent.config.exclude))\n" " argent.log('info', 'inclusions: '..set_tostring(argent.config.include))\n" " argent.log('info', 'files to keep: '..set_tostring(argent.config.keep))\n" " 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 =\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" "\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" " argent.log('debug', fmt('retaining file %q', parent..file))\n" " return true\n" " end\n" "\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" "\n" "\n" " function obliterate_dir(dir, parent)\n" " if argent.config.keep[strip_end_slash(dir)] then\n" " argent.log('debug', fmt('retaining directory %q', parent..dir))\n" " return true\n" " end\n" "\n" " argent.log('debug', fmt('obliterating files in %q', parent..dir))\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 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 should_ignore(filename, parent) 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" " )\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" " 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 should_ignore(directory, parent) 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" " 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" ;