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.
This commit is contained in:
luca0N! 2022-03-04 18:20:08 -03:00
parent ba706016b9
commit 99e2f412cd
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 12 additions and 22 deletions

View File

@ -38,11 +38,16 @@ std::string blog_relative_path(std::string const &pathPrefix, std::string const
return path.substr(pathPrefix.length() - 1); 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. // Backwards search for directory separator.
for (int x = path.length(); x > 0; --x) { for (int x = path.length(); x > 0; --x) {
if (path[x] == '/') if (path[x] == '/') return ext ? path.substr(x + 1) : path.substr(x + 1, extSeparator - x - 1);
return path.substr(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; return path;
} }
@ -126,9 +131,6 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
std::string year = match->str(), std::string year = match->str(),
month = (++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 // TODO: This code could be optimized by removing directory
// checks for every single article. Instead, add // checks for every single article. Instead, add
// directory-checks for years and months to a queue (skipping // 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); if (!std::filesystem::exists(oad)) std::filesystem::create_directory(oad);
// Now create the article file. // Now create the article file.
oad /= getFilename(a); oad /= getFilename(a, false);
oad += ".html";
FILE *articleOutput = fopen(oad.string().c_str(), "w"); 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); std::string htmlTemplate = get_template(path);
// NOTE: std::regex requires the C++11 standard. // NOTE: std::regex requires the C++11 standard.
std::regex contentPlaceholder("<!--\\[_SWG: \\$CONTENT\\]-->"); std::regex contentPlaceholder("<!--\\[_SWG: \\$CONTENT\\]-->");
//articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, articleContents);
std::string articleHtml = MarkdownParser::make_html(a); 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); fclose(articleOutput);
} catch (std::filesystem::filesystem_error const &e) { } catch (std::filesystem::filesystem_error const &e) {
std::cerr << "error: failed to create directory for an article from blog \"" std::cerr << "error: failed to create directory for an article from blog \""