From b8b1228720717a6879ed6277d484e6e83a3893f0 Mon Sep 17 00:00:00 2001 From: luca0N! Date: Mon, 21 Mar 2022 23:08:23 -0300 Subject: [PATCH] Copy website files to output SWG now copies non-special files (files that aren't used by SWG and Markdown files) to the output directory (such as CSS files). --- src/WebsiteBuilder.cxx | 46 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/WebsiteBuilder.cxx b/src/WebsiteBuilder.cxx index 3e3856c..d9a060e 100644 --- a/src/WebsiteBuilder.cxx +++ b/src/WebsiteBuilder.cxx @@ -167,11 +167,53 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st } } +/** + * Used to determine whether a file should be copied onto the output directory + * or not. Certain files, like the config file shouldn't be copied. + */ +bool is_special_file(std::string const &filename) { + if (filename == "swg.cfg" || filename.find("__swg_") == 0) + return true; + return false; +} + void build_website(SwgContext &ctx, std::string const &path) { build_dir_structure(path); - // Blog lookup + // Build regular webpages (index, privacy notice, etc) + std::filesystem::path ws = path; + // Copy all non-MD files to output (to include assets, for instance). + try { + for (auto const &ws_entry : std::filesystem::recursive_directory_iterator(ws)) { + // Skip all files inside the "output" directory. + if (ws_entry.path().string().find(path + "output/") == 0) continue; + std::string currentFile = getFilename(ws_entry.path()); + // Assuming this is a non-MD file, copy it to the output. + if (ws_entry.is_regular_file() && currentFile.find(".md") == std::string::npos + && !is_special_file(getFilename(ws_entry.path()))) { + // Subdirectories should be respected! If this + // file is in a subdirectory, it should also be + // created in the output. + std::filesystem::path copied_subdir = path; + copied_subdir /= "output"; + copied_subdir /= ws_entry.path().string() + .substr(path.length(), + ws_entry.path().string().length() - path.length() - + getFilename(ws_entry.path()).length()); + if (!std::filesystem::exists(copied_subdir)) { + std::filesystem::create_directories(copied_subdir); + std::cout << "--> Creating: " << copied_subdir << "\n"; + } + std::filesystem::copy(ws_entry, copied_subdir); + std::cout << "\t!: " << ws_entry.path().string().substr(path.length()) << "\n"; + } + } + } catch (std::filesystem::filesystem_error const &e) { + std::cerr << "error: could not copy website assets to output: " << e.what() << std::endl; + exit(RETURN_FAILED_UNKNOWN_ERROR); + } + // Blog lookup bool failed = false; for (Blog *b : ctx.blogs) { @@ -185,6 +227,8 @@ void build_website(SwgContext &ctx, std::string const &path) { const std::filesystem::path blogPath(relativePath); try { for (auto const &dir_entry : std::filesystem::recursive_directory_iterator(blogPath)) { + // Skip all files inside the "output" directory. + if (dir_entry.path().string().find(path + "output/") == 0) continue; // Directory item iteration std::cout << "\t" << dir_entry << std::endl;