(FEAT): Includes support for code block segments.
This includes the addition of lots of new node elements.
This commit is contained in:
parent
2881897c23
commit
1c81d2aa41
7
input.md
7
input.md
@ -38,3 +38,10 @@ world
|
||||
- hello
|
||||
|
||||
world number two
|
||||
|
||||
```
|
||||
int x = 5;
|
||||
int y = 10;
|
||||
|
||||
console.log(x + y); // '15'
|
||||
```
|
||||
|
||||
@ -23,3 +23,5 @@ string BoldItalicNode::ToHtml() const {
|
||||
}
|
||||
|
||||
string CodeNode::ToHtml() const { return "<code>" + this->content + "</code>"; }
|
||||
|
||||
string RawTextNode::ToHtml() const { return this->content; };
|
||||
|
||||
@ -117,4 +117,17 @@ public:
|
||||
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
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include "structureNode.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@ -95,7 +96,12 @@ std::unique_ptr<Node> Parser::ParseBlock() {
|
||||
return ParseList(true);
|
||||
}
|
||||
|
||||
// 4. Parser paragraph
|
||||
// 4. Parse code block
|
||||
if (c == '`' && c_next == '`' && Peek(2) == '`') {
|
||||
return ParseCodeBlock();
|
||||
}
|
||||
|
||||
// 5. Parser paragraph
|
||||
return ParseParagraph();
|
||||
}
|
||||
|
||||
@ -177,6 +183,37 @@ std::unique_ptr<Node> Parser::ParseList(bool ordered) {
|
||||
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>> nodes;
|
||||
string str;
|
||||
|
||||
@ -123,6 +123,7 @@ private:
|
||||
std::unique_ptr<Node> ParseHeading();
|
||||
std::unique_ptr<Node> ParseList(bool ordered);
|
||||
vector<std::unique_ptr<Node>> ParseInline();
|
||||
std::unique_ptr<Node> ParseCodeBlock();
|
||||
|
||||
// The only differences are the exit condition
|
||||
vector<std::unique_ptr<Node>> ParseInlineHeading();
|
||||
|
||||
@ -67,3 +67,16 @@ string ListNode::ToHtml() const {
|
||||
ss << (this->ordered ? "</ol>" : "</ul>") << "\n";
|
||||
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();
|
||||
}
|
||||
|
||||
@ -111,4 +111,19 @@ public:
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user