(FEAT): Heading parser uses a new inline method.
The only difference between this and the original one is the exit condition.
This commit is contained in:
parent
d863b93132
commit
e6dc318306
6
input.md
6
input.md
@ -15,4 +15,8 @@ this is too far`
|
|||||||
|
|
||||||
## **Hello world**
|
## **Hello world**
|
||||||
|
|
||||||
### hello world
|
### hello *world*
|
||||||
|
|
||||||
|
# ***This is both!***
|
||||||
|
|
||||||
|
###### This is neither
|
||||||
|
|||||||
@ -108,25 +108,15 @@ std::unique_ptr<Node> Parser::ParseHeading() {
|
|||||||
|
|
||||||
ConsumeWhiteSpace();
|
ConsumeWhiteSpace();
|
||||||
|
|
||||||
std::string str;
|
// This should call parse inline
|
||||||
while (!IsEOF()) {
|
auto text_nodes = ParseInline();
|
||||||
c = Peek();
|
for (auto &text_node : text_nodes) {
|
||||||
// We can stop as soon as we see a new line. Headings are single line blocks
|
node->AddChild(std::move(text_node));
|
||||||
if (c == '\n')
|
|
||||||
break;
|
|
||||||
|
|
||||||
// If a newline, use a space instead
|
|
||||||
str += c;
|
|
||||||
Consume();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BUG: Why do we need to check this?
|
if (node->IsEmpty())
|
||||||
if (str == "")
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto text_node = std::make_unique<TextNode>(str);
|
|
||||||
node->AddChild(std::move(text_node));
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,6 +169,54 @@ vector<std::unique_ptr<Node>> Parser::ParseInline() {
|
|||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<std::unique_ptr<Node>> Parser::ParseInlineHeading() {
|
||||||
|
vector<std::unique_ptr<Node>> nodes;
|
||||||
|
string str;
|
||||||
|
|
||||||
|
while (!IsEOF()) {
|
||||||
|
char c = Peek();
|
||||||
|
// We can stop as soon as we see a new line. Headings are single line blocks
|
||||||
|
if (c == '\n')
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (c == '*' && Peek(1) == '*' && Peek(2) == '*') {
|
||||||
|
PushTextNode(nodes, str);
|
||||||
|
auto node = ParseBoldItalic();
|
||||||
|
if (!node->IsEmpty())
|
||||||
|
nodes.push_back(std::move(node));
|
||||||
|
continue;
|
||||||
|
} else if (c == '*' && Peek(1) == '*') {
|
||||||
|
PushTextNode(nodes, str);
|
||||||
|
auto node = ParseBold();
|
||||||
|
if (!node->IsEmpty())
|
||||||
|
nodes.push_back(std::move(node));
|
||||||
|
continue;
|
||||||
|
} else if (c == '*') {
|
||||||
|
PushTextNode(nodes, str);
|
||||||
|
auto node = ParseItalic();
|
||||||
|
if (!node->IsEmpty())
|
||||||
|
nodes.push_back(std::move(node));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == '`') {
|
||||||
|
PushTextNode(nodes, str);
|
||||||
|
auto node = ParseCode();
|
||||||
|
if (!node->IsEmpty())
|
||||||
|
nodes.push_back(std::move(node));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a newline, use a space instead
|
||||||
|
str += (c == '\n' ? ' ' : c);
|
||||||
|
Consume();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push the last node, if the string is not empty
|
||||||
|
PushTextNode(nodes, str);
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Node> Parser::ParseItalic() {
|
std::unique_ptr<Node> Parser::ParseItalic() {
|
||||||
string str;
|
string str;
|
||||||
Consume(1);
|
Consume(1);
|
||||||
|
|||||||
@ -117,10 +117,15 @@ private:
|
|||||||
// Working input content
|
// Working input content
|
||||||
string content;
|
string content;
|
||||||
|
|
||||||
|
// TODO: Document these methods, no more magic methods :)
|
||||||
|
|
||||||
std::unique_ptr<Node> ParseParagraph();
|
std::unique_ptr<Node> ParseParagraph();
|
||||||
std::unique_ptr<Node> ParseHeading();
|
std::unique_ptr<Node> ParseHeading();
|
||||||
vector<std::unique_ptr<Node>> ParseInline();
|
vector<std::unique_ptr<Node>> ParseInline();
|
||||||
|
|
||||||
|
// The only difference is the exit condition
|
||||||
|
vector<std::unique_ptr<Node>> ParseInlineHeading();
|
||||||
|
|
||||||
void PushTextNode(vector<std::unique_ptr<Node>> &nodes, string &str);
|
void PushTextNode(vector<std::unique_ptr<Node>> &nodes, string &str);
|
||||||
|
|
||||||
std::unique_ptr<Node> ParseItalic();
|
std::unique_ptr<Node> ParseItalic();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user