From 29a0ab1f2b04a9b9a45282b6c9026136b1ed1f6d Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Tue, 28 Oct 2025 01:59:24 -0700 Subject: set CSS theme as a member variable --- include/markdown_translator.hpp | 18 +++++++++++++++-- src/main.cpp | 4 ++-- src/markdown_translator.cpp | 43 ++++++++++++----------------------------- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/include/markdown_translator.hpp b/include/markdown_translator.hpp index 6eb32f8..8bf6c84 100644 --- a/include/markdown_translator.hpp +++ b/include/markdown_translator.hpp @@ -13,9 +13,13 @@ public: // Destructor ~MarkdownTranslator(); // Main translation function - takes markdown content and returns HTML - std::string translate(const std::string& markdownContent, const std::string& cssPath = "styles/carbon.css"); + std::string translate(const std::string& markdownContent); std::string processLine(const std::string& line); + enum Theme { + carbon + }; + private: // Regex for various tags const std::string headerRegexStr{"^(#{1,6})\\s+(.*)$"}; @@ -48,7 +52,7 @@ private: } // HTML builders - std::string buildHTMLHeader(const std::string& title, const std::string& cssPath){ + std::string buildHTMLHeader(const std::string& title){ return R"( @@ -72,8 +76,18 @@ private: )"; } + void setTheme(const Theme& theme){ + switch(theme){ + case Theme::carbon: + cssPath = "styles/carbon.css"; + default: + cssPath = "styles/carbon.css"; + } + } + // Member variables std::string title; + std::string cssPath{"styles/carbon.css"}; }; #endif // MARKDOWN_TRANSLATOR_H diff --git a/src/main.cpp b/src/main.cpp index 82f16c2..e926df7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,12 +64,12 @@ int main(int argc, char* argv[]) { MarkdownTranslator translator; // Get CSS path from parameters or use default - std::string cssPath = "styles/ffxiv-style.css"; + std::string cssPath = ""; if (params.find("-css") != params.end()) { cssPath = params["-css"]; } - std::string htmlOutput = translator.translate(markdownContent, cssPath); + std::string htmlOutput = translator.translate(markdownContent); // Write to output file std::string outputFile = params["-o"]; diff --git a/src/markdown_translator.cpp b/src/markdown_translator.cpp index 3c97874..5bd1cc4 100644 --- a/src/markdown_translator.cpp +++ b/src/markdown_translator.cpp @@ -3,7 +3,7 @@ #include #include -MarkdownTranslator::MarkdownTranslator() : title("Carbon") { +MarkdownTranslator::MarkdownTranslator() : title("WikiMD2HTML") { } @@ -25,50 +25,31 @@ void MarkdownTranslator::processMetadata(const std::vector& lines){ } } -std::string MarkdownTranslator::translate(const std::string& markdownContent, const std::string& cssPath) { +std::string MarkdownTranslator::translate(const std::string& markdownContent) { std::stringstream htmlOutput; std::stringstream markdownStream(markdownContent); std::string line; std::vector headers; - std::string currentLine; + // Parse metadata section if it exists bool metadataExists{false}; std::vector metadataLines; - while (std::getline(markdownStream, currentLine)) { - if(currentLine == "---"){ - if(metadataExists){ + if (std::getline(markdownStream, currentLine) && currentLine == "---") { + metadataExists = true; + while (std::getline(markdownStream, currentLine)) { + if (currentLine == "---") { break; - } else { - metadataExists = true; - continue; - } - } - if(!metadataExists){ - break; - } - metadataLines.push_back(currentLine); - } - processMetadata(metadataLines); - - while (std::getline(markdownStream, currentLine)) { - std::regex headerRegex("^(#{1,3})\\s+(.*)$"); - std::smatch matches; - if (std::regex_match(currentLine, matches, headerRegex)) { - int level = matches[1].length(); - std::string content = matches[2]; - if (level <= 3) { - headers.push_back(std::to_string(level) + ":" + content); } + metadataLines.push_back(currentLine); } } - markdownStream.clear(); - markdownStream.str(markdownContent); - htmlOutput << buildHTMLHeader(title, cssPath); - // Add navigation sidebar + // Process and apply metadata to curr object + if(metadataExists) + processMetadata(metadataLines); + htmlOutput << buildHTMLHeader(title); generateSideBar(htmlOutput, headers); - // Main content container htmlOutput << "
\n"; htmlOutput << "
\n"; -- cgit v1.2.3