Compile non-blog Markdown files

SWG now compiles Markdown files that aren't meant to be interpreted as
blog articles, such as index pages.
This commit is contained in:
luca0N! 2022-03-21 23:55:36 -03:00
parent b8b1228720
commit 5ae637a52d
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 26 additions and 13 deletions

View File

@ -111,6 +111,19 @@ std::string get_template(std::string const &path) {
return htmlTemplate;
}
void compile_markdown(std::string const &path, std::string const &md, std::string const &to) {
FILE *articleOutput = fopen(to.c_str(), "w");
std::string htmlTemplate = get_template(path);
// NOTE: std::regex requires the C++11 standard.
std::regex contentPlaceholder("<!--\\[_SWG: \\$CONTENT\\]-->");
std::string articleHtml = MarkdownParser::make_html(md);
std::string articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, articleHtml);
fputs(articleContents.c_str(), articleOutput);
fclose(articleOutput);
}
void build_blog_structure(std::string const &path, std::string const &prefix, std::list<std::string> const &articles, Blog *blog) {
std::filesystem::path obp = get_output_path(path); // Output Blog Path
obp /= "blog";
@ -149,16 +162,7 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
// Now create the article file.
oad /= getFilename(a, false);
oad += ".html";
FILE *articleOutput = fopen(oad.string().c_str(), "w");
std::string htmlTemplate = get_template(path);
// NOTE: std::regex requires the C++11 standard.
std::regex contentPlaceholder("<!--\\[_SWG: \\$CONTENT\\]-->");
std::string articleHtml = MarkdownParser::make_html(a);
std::string articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, articleHtml);
fputs(articleContents.c_str(), articleOutput);
fclose(articleOutput);
compile_markdown(path, a, oad);
} catch (std::filesystem::filesystem_error const &e) {
std::cerr << "error: failed to create directory for an article from blog \""
<< blog->name << "\": " << e.what() << std::endl;
@ -189,8 +193,8 @@ void build_website(SwgContext &ctx, std::string const &path) {
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()))) {
if (ws_entry.is_regular_file() &&
!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.
@ -204,7 +208,16 @@ void build_website(SwgContext &ctx, std::string const &path) {
std::filesystem::create_directories(copied_subdir);
std::cout << "--> Creating: " << copied_subdir << "\n";
}
// If this isn't a Markdown file, just copy it.
if (currentFile.find(".md") == std::string::npos)
std::filesystem::copy(ws_entry, copied_subdir);
else {
// This is a Markdown file. Compile a HTML version.
std::filesystem::path output_html = copied_subdir;
// Remove the .md extension and use the HTML extension instead
output_html /= currentFile.substr(0, currentFile.length() - 3) + ".html";
compile_markdown(path, ws_entry.path(), output_html);
}
std::cout << "\t!: " << ws_entry.path().string().substr(path.length()) << "\n";
}
}