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; 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) { 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 std::filesystem::path obp = get_output_path(path); // Output Blog Path
obp /= "blog"; obp /= "blog";
@ -149,16 +162,7 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
// Now create the article file. // Now create the article file.
oad /= getFilename(a, false); oad /= getFilename(a, false);
oad += ".html"; oad += ".html";
FILE *articleOutput = fopen(oad.string().c_str(), "w"); compile_markdown(path, a, oad);
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);
} 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 \""
<< blog->name << "\": " << e.what() << std::endl; << 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; if (ws_entry.path().string().find(path + "output/") == 0) continue;
std::string currentFile = getFilename(ws_entry.path()); std::string currentFile = getFilename(ws_entry.path());
// Assuming this is a non-MD file, copy it to the output. // Assuming this is a non-MD file, copy it to the output.
if (ws_entry.is_regular_file() && currentFile.find(".md") == std::string::npos if (ws_entry.is_regular_file() &&
&& !is_special_file(getFilename(ws_entry.path()))) { !is_special_file(getFilename(ws_entry.path()))) {
// Subdirectories should be respected! If this // Subdirectories should be respected! If this
// file is in a subdirectory, it should also be // file is in a subdirectory, it should also be
// created in the output. // created in the output.
@ -204,7 +208,16 @@ void build_website(SwgContext &ctx, std::string const &path) {
std::filesystem::create_directories(copied_subdir); std::filesystem::create_directories(copied_subdir);
std::cout << "--> Creating: " << copied_subdir << "\n"; std::cout << "--> Creating: " << copied_subdir << "\n";
} }
std::filesystem::copy(ws_entry, copied_subdir); // 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"; std::cout << "\t!: " << ws_entry.path().string().substr(path.length()) << "\n";
} }
} }