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).
This commit is contained in:
luca0N! 2022-03-21 23:08:23 -03:00
parent c4d83a9eef
commit b8b1228720
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 45 additions and 1 deletions

View File

@ -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;