From 5ae637a52daba3fc4c05cfc7553ce6c32901803b Mon Sep 17 00:00:00 2001 From: luca0N! Date: Mon, 21 Mar 2022 23:55:36 -0300 Subject: [PATCH] Compile non-blog Markdown files SWG now compiles Markdown files that aren't meant to be interpreted as blog articles, such as index pages. --- src/WebsiteBuilder.cxx | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/WebsiteBuilder.cxx b/src/WebsiteBuilder.cxx index d9a060e..892d514 100644 --- a/src/WebsiteBuilder.cxx +++ b/src/WebsiteBuilder.cxx @@ -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(""); + 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 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(""); - 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"; } - 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"; } }