Skip to content

Commit

Permalink
Bugfix for complex md links (giuspen#866)
Browse files Browse the repository at this point in the history
* Fixed bug where the parser got very confused with () in links

* Removed debug messages
  • Loading branch information
ForeverRainbow authored May 31, 2020
1 parent 86c8ac6 commit 408c46b
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions future/src/ct/ct_md_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ct_imports.h"
#include "ct_const.h"
#include "ct_misc_utils.h"
#include <iostream>


const std::vector<CtImportHandler::token_schema>& CtMDParser::_get_tokens()
Expand All @@ -39,7 +40,7 @@ const std::vector<CtImportHandler::token_schema>& CtMDParser::_get_tokens()
{"[", true, false, [this](const std::string& data) {
_add_text(data, false);
_in_link = true;
}, "]"},
}, "]", true},
// Second half of a link
{"(", true, false, [this](const std::string& data) {
if (_in_link) {
Expand All @@ -50,7 +51,7 @@ const std::vector<CtImportHandler::token_schema>& CtMDParser::_get_tokens()
// Just text in brackets
_add_text("(" + data + ")");
}
}, ")"},
}, ")", true},
// List
{"* ", false, false, [this](const std::string& data){
_add_list(0, data);
Expand Down Expand Up @@ -99,11 +100,24 @@ void CtMDParser::feed(std::istream& stream)
// Feed the line
auto tokens = _tokenize(line);

for (const auto& token : tokens) {
if (token.first) {
token.first->action(token.second);
for (auto iter = tokens.begin(); iter != tokens.end(); ++iter) {
if (iter->first) {
// This is needed for links with () in them
if ((iter + 1) != tokens.end()) {
if (!(iter + 1)->first && ((iter + 1)->second == ")")) {
// Excess bracket from link
iter->first->action(iter->second + ")");
++iter;
if ((iter + 1) != tokens.end()) ++iter;

continue;
}
}


iter->first->action(iter->second);
} else {
if (!token.second.empty()) _add_text(token.second);
if (!iter->second.empty()) _add_text(iter->second);
}
}
_add_newline();
Expand Down

0 comments on commit 408c46b

Please sign in to comment.