From 99e2f412cd9a02bfa5ca6edbea2757b3a1ad1019 Mon Sep 17 00:00:00 2001 From: luca0N! Date: Fri, 4 Mar 2022 18:20:08 -0300 Subject: [PATCH] Fix generated blog page contents SWG now creates a proper HTML file with the .html extension set correctly and puts the article contents inside of the main HTML template. --- src/WebsiteBuilder.cxx | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/WebsiteBuilder.cxx b/src/WebsiteBuilder.cxx index d617e5b..3e3856c 100644 --- a/src/WebsiteBuilder.cxx +++ b/src/WebsiteBuilder.cxx @@ -38,11 +38,16 @@ std::string blog_relative_path(std::string const &pathPrefix, std::string const return path.substr(pathPrefix.length() - 1); } -std::string getFilename(std::string const &path) { +std::string getFilename(std::string const &path, bool const ext = true) { + int extSeparator = -1; // Backwards search for directory separator. for (int x = path.length(); x > 0; --x) { - if (path[x] == '/') - return path.substr(x + 1); + if (path[x] == '/') return ext ? path.substr(x + 1) : path.substr(x + 1, extSeparator - x - 1); + // If this function was called with "ext" set to false, + // generate a substring of the path containing the filename + // only. + else if (!ext && path[x] == '.' && extSeparator == -1) + extSeparator = x; } return path; } @@ -126,9 +131,6 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st std::string year = match->str(), month = (++match)->str(); - /*std::cout << "Year for \"" << articlePath << "\": " << year << "\n"; - std::cout << "Month for \"" << articlePath << "\": " << month << "\n";*/ - // TODO: This code could be optimized by removing directory // checks for every single article. Instead, add // directory-checks for years and months to a queue (skipping @@ -145,29 +147,17 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st if (!std::filesystem::exists(oad)) std::filesystem::create_directory(oad); // Now create the article file. - oad /= getFilename(a); + oad /= getFilename(a, false); + oad += ".html"; FILE *articleOutput = fopen(oad.string().c_str(), "w"); - /*FILE *articleFile = fopen(a.c_str(), "r"); - - std::string articleContents; - - int buflen = 512; - char buf[buflen]; - - while (fgets(buf, buflen, articleFile) != NULL) { - articleContents += buf; - } - - fclose(articleFile);*/ std::string htmlTemplate = get_template(path); // NOTE: std::regex requires the C++11 standard. std::regex contentPlaceholder(""); - //articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, articleContents); std::string articleHtml = MarkdownParser::make_html(a); + std::string articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, articleHtml); - fputs(articleHtml.c_str(), articleOutput); - + fputs(articleContents.c_str(), articleOutput); fclose(articleOutput); } catch (std::filesystem::filesystem_error const &e) { std::cerr << "error: failed to create directory for an article from blog \""