From a1001cb9913f00b3d4d5dff3c57703af64666d25 Mon Sep 17 00:00:00 2001 From: Petra Baranski Date: Mon, 24 Jul 2023 20:32:43 +0200 Subject: [PATCH] feat: add class to block code --- CHANGELOG.md | 1 + include/maddy/codeblockparser.h | 9 ++++++++- tests/maddy/test_maddy_codeblockparser.cpp | 23 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b81ab52..5fb0515 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ maddy uses [semver versioning](https://semver.org/). * ![**ADDED**](https://img.shields.io/badge/-ADDED-%23099) GitHub workflow * ![**DEPRECATED**](https://img.shields.io/badge/-DEPRECATED-%23666) config flags `isEmphasizedParserEnabled` and `isHTMLWrappedInParagraph` * ![**ADDED**](https://img.shields.io/badge/-ADDED-%23099) config flag `enabledParsers` to en-/disable each parser separately +* ![**ADDED**](https://img.shields.io/badge/-ADDED-%23099) class attribute to code blocks if there is text after the three backticks like ` ```cpp` * ? ## version 1.1.2 2020-10-04 diff --git a/include/maddy/codeblockparser.h b/include/maddy/codeblockparser.h index 2cb4a97..9f69db9 100644 --- a/include/maddy/codeblockparser.h +++ b/include/maddy/codeblockparser.h @@ -74,7 +74,7 @@ public: static bool IsStartingLine(const std::string& line) { - static std::regex re("^(?:`){3}$"); + static std::regex re("^(?:`){3}(.*)$"); return std::regex_match(line, re); } @@ -123,6 +123,13 @@ protected: return; } } + else if (!this->isStarted && line.substr(0, 3) == "```") + { + line = "
\n";
+      this->isStarted = true;
+      this->isFinished = false;
+      return;
+    }
 
     line += "\n";
   }
diff --git a/tests/maddy/test_maddy_codeblockparser.cpp b/tests/maddy/test_maddy_codeblockparser.cpp
index 849c7d8..319af98 100644
--- a/tests/maddy/test_maddy_codeblockparser.cpp
+++ b/tests/maddy/test_maddy_codeblockparser.cpp
@@ -59,3 +59,26 @@ TEST_F(MADDY_CODEBLOCKPARSER, ItReplacesMarkdownWithAnHtmlCodeBlock)
 
   ASSERT_EQ(expected, outputString);
 }
+
+TEST_F(MADDY_CODEBLOCKPARSER, ItShouldUseAnythingBehindFirstBackticksAsClass)
+{
+  std::vector markdown = {
+    "```cpp"
+    , "some code"
+    , "some other code"
+    , "```"
+  };
+
+  std::string expected = "
\nsome code\nsome other code\n
"; + + for (std::string md : markdown) + { + cbParser->AddLine(md); + } + ASSERT_TRUE(cbParser->IsFinished()); + + std::stringstream& output(cbParser->GetResult()); + const std::string& outputString = output.str(); + + ASSERT_EQ(expected, outputString); +}