From 84459bd663f62c2d1e83c7f47aa8bc96eb6543e2 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Tue, 28 Oct 2025 01:38:28 -0700 Subject: move regex to header file --- include/markdown_translator.hpp | 6 ++++++ src/markdown_translator.cpp | 29 +++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/markdown_translator.hpp b/include/markdown_translator.hpp index 6c0c1bc..6a3b91f 100644 --- a/include/markdown_translator.hpp +++ b/include/markdown_translator.hpp @@ -17,6 +17,12 @@ public: std::string processLine(const std::string& line); private: + // Regex for various tags + const std::string headerRegexStr{"^(#{1,6})\\s+(.*)$"}; + const std::string boldRegexStr{"\\*\\*([^\\*]+)\\*\\*|__([^_]+)__"}; + const std::string italicRegexStr{"\\*([^\\*]+)\\*|_([^_]+)_"}; + const std::string linkRegexStr{"\\[([^\\]]+)\\]\\(([^\\)]+)\\)"}; + const std::string imageRegexStr{"!\\[(.*?)\\]\\(([^\\s\"]+)(\\s+\"(.*?)\")?\\)"}; // Helper functions for different markdown elements void processMetadata(const std::vector& lines); std::string processHeaders(const std::string& line); diff --git a/src/markdown_translator.cpp b/src/markdown_translator.cpp index 6cd6bb8..9ec37a6 100644 --- a/src/markdown_translator.cpp +++ b/src/markdown_translator.cpp @@ -67,15 +67,16 @@ std::string MarkdownTranslator::translate(const std::string& markdownContent, co markdownStream.str(markdownContent); // Start with basic HTML structure - htmlOutput << "\n"; - htmlOutput << "\n"; - htmlOutput << "\n"; - htmlOutput << " \n"; - htmlOutput << " \n"; - htmlOutput << " " + title + "\n"; - htmlOutput << " \n"; - htmlOutput << "\n"; - htmlOutput << "\n"; + htmlOutput << R"( + + + + + )" << title << R"( + + + +)"; // Add navigation sidebar generateSideBar(htmlOutput, headers); @@ -208,7 +209,7 @@ std::string MarkdownTranslator::processLine(const std::string& line) { std::string MarkdownTranslator::processHeaders(const std::string& line) { // Check for H1-H6 - std::regex headerRegex("^(#{1,6})\\s+(.*)$"); + std::regex headerRegex(headerRegexStr); std::smatch matches; if (std::regex_match(line, matches, headerRegex)) { int level = matches[1].length(); @@ -223,7 +224,7 @@ std::string MarkdownTranslator::processHeaders(const std::string& line) { std::string MarkdownTranslator::processBold(const std::string& text) { // Replace **text** or __text__ with text std::string result = text; - std::regex boldRegex("\\*\\*([^\\*]+)\\*\\*|__([^_]+)__"); + std::regex boldRegex(boldRegexStr); result = std::regex_replace(result, boldRegex, "$1$2"); return result; } @@ -231,7 +232,7 @@ std::string MarkdownTranslator::processBold(const std::string& text) { std::string MarkdownTranslator::processItalic(const std::string& text) { // Replace *text* or _text_ with text std::string result = text; - std::regex italicRegex("\\*([^\\*]+)\\*|_([^_]+)_"); + std::regex italicRegex(italicRegexStr); result = std::regex_replace(result, italicRegex, "$1$2"); return result; } @@ -239,7 +240,7 @@ std::string MarkdownTranslator::processItalic(const std::string& text) { std::string MarkdownTranslator::processLinks(const std::string& text) { // Replace [text](url) with text std::string result = text; - std::regex linkRegex("\\[([^\\]]+)\\]\\(([^\\)]+)\\)"); + std::regex linkRegex(linkRegexStr); result = std::regex_replace(result, linkRegex, "$1"); return result; } @@ -251,7 +252,7 @@ std::string MarkdownTranslator::processParagraph(const std::string& text) { std::string MarkdownTranslator::processSingleFigure(const std::string& text) { // Extract image details using regex - std::regex imageRegex("!\\[(.*?)\\]\\(([^\\s\"]+)(\\s+\"(.*?)\")?\\)"); + std::regex imageRegex(imageRegexStr); std::smatch matches; if (std::regex_search(text, matches, imageRegex)) { -- cgit v1.2.3