(FEAT): Includes support for code block segments.

This includes the addition of lots of new node elements.
This commit is contained in:
Hayden Hargreaves 2025-10-28 17:45:40 -07:00
parent 2881897c23
commit 1c81d2aa41
7 changed files with 89 additions and 1 deletions

View File

@ -38,3 +38,10 @@ world
- hello - hello
world number two world number two
```
int x = 5;
int y = 10;
console.log(x + y); // '15'
```

View File

@ -23,3 +23,5 @@ string BoldItalicNode::ToHtml() const {
} }
string CodeNode::ToHtml() const { return "<code>" + this->content + "</code>"; } string CodeNode::ToHtml() const { return "<code>" + this->content + "</code>"; }
string RawTextNode::ToHtml() const { return this->content; };

View File

@ -117,4 +117,17 @@ public:
std::string ToHtml() const; std::string ToHtml() const;
}; };
/**
* @desc A raw text node.
*
* This node returns only it content, with no formatting at all.
*
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
*/
class RawTextNode : public InlineNode {
public:
RawTextNode(std::string content) : InlineNode(content) {};
std::string ToHtml() const;
};
#endif #endif

View File

@ -4,6 +4,7 @@
#include "structureNode.h" #include "structureNode.h"
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
@ -95,7 +96,12 @@ std::unique_ptr<Node> Parser::ParseBlock() {
return ParseList(true); return ParseList(true);
} }
// 4. Parser paragraph // 4. Parse code block
if (c == '`' && c_next == '`' && Peek(2) == '`') {
return ParseCodeBlock();
}
// 5. Parser paragraph
return ParseParagraph(); return ParseParagraph();
} }
@ -177,6 +183,37 @@ std::unique_ptr<Node> Parser::ParseList(bool ordered) {
return node; return node;
}; };
std::unique_ptr<Node> Parser::ParseCodeBlock() {
auto node = std::make_unique<CodeBlockNode>();
string str;
// Remove the first three characters, the '```'
Consume(3);
// Parse text into a single text node until '```' is found, include everything
// else
while (!IsEOF()) {
char c = Peek();
if (c == '`' && Peek(1) == '`' && Peek(2) == '`') {
Consume(3);
break;
}
// Swap any '\n' with BR tags, so it will visually break
if (c == '\n')
str += "\n<br>\n";
else
str += c;
Consume();
}
auto text_node = std::make_unique<RawTextNode>(str);
node->AddChild(std::move(text_node));
return node;
}
vector<std::unique_ptr<Node>> Parser::ParseInline() { vector<std::unique_ptr<Node>> Parser::ParseInline() {
vector<std::unique_ptr<Node>> nodes; vector<std::unique_ptr<Node>> nodes;
string str; string str;

View File

@ -123,6 +123,7 @@ private:
std::unique_ptr<Node> ParseHeading(); std::unique_ptr<Node> ParseHeading();
std::unique_ptr<Node> ParseList(bool ordered); std::unique_ptr<Node> ParseList(bool ordered);
vector<std::unique_ptr<Node>> ParseInline(); vector<std::unique_ptr<Node>> ParseInline();
std::unique_ptr<Node> ParseCodeBlock();
// The only differences are the exit condition // The only differences are the exit condition
vector<std::unique_ptr<Node>> ParseInlineHeading(); vector<std::unique_ptr<Node>> ParseInlineHeading();

View File

@ -67,3 +67,16 @@ string ListNode::ToHtml() const {
ss << (this->ordered ? "</ol>" : "</ul>") << "\n"; ss << (this->ordered ? "</ol>" : "</ul>") << "\n";
return ss.str(); return ss.str();
} }
string CodeBlockNode::ToHtml() const {
std::stringstream ss;
ss << "<code>\n";
for (const auto &child : this->GetChilren()) {
ss << child->ToHtml() << "\n";
}
ss << "</code>\n";
return ss.str();
}

View File

@ -111,4 +111,19 @@ public:
std::string ToHtml() const; std::string ToHtml() const;
}; };
/**
* @desc A code block container node.
*
* This node is used to wrap a code block node. When three '`' are used a
* code block should be created. This node's children are expected to be simple
* text nodes - containing no formatting at all. Since code blocks are not parsed
* any deeper then their parents.
*
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
*/
class CodeBlockNode : public StructureNode {
public:
std::string ToHtml() const;
};
#endif #endif