Skip to content

Commit

Permalink
Merge branch 'vedderb:master' into ci
Browse files Browse the repository at this point in the history
  • Loading branch information
darwinbeing authored Jul 6, 2024
2 parents a0dcd5a + 48e6e34 commit 6491a41
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 46 deletions.
8 changes: 8 additions & 0 deletions maddy/blockparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// -----------------------------------------------------------------------------

#include <functional>
#include <memory>
#include <sstream>
#include <string>
// windows compatibility includes
Expand Down Expand Up @@ -119,6 +120,12 @@ class BlockParser
return this->result;
}

std::string
GetReparseLine()
{
return this->reparseLine;
}

/**
* Clear
*
Expand All @@ -138,6 +145,7 @@ class BlockParser
protected:
std::stringstream result;
std::shared_ptr<BlockParser> childParser;
std::string reparseLine;

virtual bool isInlineBlockAllowed() const = 0;
virtual bool isLineParserAllowed() const = 0;
Expand Down
76 changes: 46 additions & 30 deletions maddy/paragraphparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@

#include <functional>
#include <string>
#include <QDebug>

#include "maddy/blockparser.h"
#include "maddy/codeblockparser.h"
#include "maddy/headlineparser.h"
#include "maddy/horizontallineparser.h"
#include "maddy/quoteparser.h"
#include "maddy/tableparser.h"
#include "maddy/checklistparser.h"
#include "maddy/orderedlistparser.h"
#include "maddy/unorderedlistparser.h"
#include "maddy/htmlparser.h"

// -----------------------------------------------------------------------------

Expand Down Expand Up @@ -87,42 +97,48 @@ class ParagraphParser : public BlockParser
return true;
}

void
parseBlock(std::string& line) override
{
if (this->isEnabled && !this->isStarted)
{
line = "<p>" + line + " ";
this->isStarted = true;
return;
}
else if (!this->isEnabled && !this->isStarted)
{
line += " ";
this->isStarted = true;
return;
}

if (this->isEnabled && line.empty())
{
line += "</p>";
this->isFinished = true;
return;
}
else if (!this->isEnabled && line.empty())
{
line += "<br/>";
this->isFinished = true;
return;
}

line += " ";
void parseBlock(std::string& line) override {
// Check if another parser should take over
if (this->isStarted && (
maddy::CodeBlockParser::IsStartingLine(line) ||
maddy::HeadlineParser::IsStartingLine(line) ||
maddy::HorizontalLineParser::IsStartingLine(line) ||
maddy::QuoteParser::IsStartingLine(line) ||
maddy::TableParser::IsStartingLine(line) ||
maddy::ChecklistParser::IsStartingLine(line) ||
maddy::OrderedListParser::IsStartingLine(line) ||
maddy::UnorderedListParser::IsStartingLine(line) ||
maddy::HtmlParser::IsStartingLine(line)
)) {
this->reparseLine = line;
line = this->isEnabled ? "</p>" : "<br/>";
this->isFinished = true;
return;
}

if (!this->isStarted) {
if (this->isEnabled) {
line = "<p>" + line + " ";
} else {
line += " ";
}

this->isStarted = true;
} else {
if (line.empty()) {
line += this->isEnabled ? "</p>" : "<br/>";
this->isFinished = true;
} else {
line += " ";
}
}
}

private:
bool isStarted;
bool isFinished;
bool isEnabled;

}; // class ParagraphParser

// -----------------------------------------------------------------------------
Expand Down
40 changes: 26 additions & 14 deletions maddy/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <functional>
#include <string>
#include <QDebug>

#include "maddy/parserconfig.h"

Expand Down Expand Up @@ -158,23 +159,34 @@ class Parser
std::string result = "";
std::shared_ptr<BlockParser> currentBlockParser = nullptr;

for (std::string line; std::getline(markdown, line);)
{
if (!currentBlockParser)
{
currentBlockParser = getBlockParserForLine(line);
}
for (std::string line; std::getline(markdown, line);) {
bool reparse = true;

if (currentBlockParser)
{
currentBlockParser->AddLine(line);
while (reparse) {
reparse = false;

if (currentBlockParser->IsFinished())
{
result += currentBlockParser->GetResult().str();
currentBlockParser = nullptr;
if (!currentBlockParser)
{
currentBlockParser = getBlockParserForLine(line);
}

if (currentBlockParser)
{
currentBlockParser->AddLine(line);

if (currentBlockParser->IsFinished())
{
result += currentBlockParser->GetResult().str();

if (!currentBlockParser->GetReparseLine().empty()) {
reparse = true;
line = currentBlockParser->GetReparseLine();
}

currentBlockParser = nullptr;
}
}
}
}
}

// make sure, that all parsers are finished
Expand Down
2 changes: 1 addition & 1 deletion maddy/parserconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum PARSER_TYPE : uint32_t
UNORDERED_LIST_PARSER = 0b0100000000000000000,
LATEX_BLOCK_PARSER = 0b1000000000000000000,

DEFAULT = 0b0111111111110111111,
DEFAULT = 0b0111111111111111111,
ALL = 0b1111111111111111111,
TEST = 0b1110011110111110111,
};
Expand Down
2 changes: 1 addition & 1 deletion utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ bool Utility::downloadUrlEventloop(QString path, QString dest)
QString Utility::md2html(QString md)
{
std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();
config->enabledParsers = maddy::types::ALL;
config->enabledParsers = maddy::types::DEFAULT;
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(config);

std::stringstream markdownInput(md.toStdString());
Expand Down

0 comments on commit 6491a41

Please sign in to comment.