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:
parent
ba706016b9
commit
99e2f412cd
|
@ -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("<!--\\[_SWG: \\$CONTENT\\]-->");
|
||||
//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 \""
|
||||
|
|
Loading…
Reference in New Issue