Some markdown processing.

This commit is contained in:
James Grogan 2022-12-01 17:13:54 +00:00
parent 31b479e9f6
commit ec11529b9a
23 changed files with 677 additions and 135 deletions

View file

@ -1,30 +1,231 @@
#include "MarkdownParser.h"
#include "MarkdownDocument.h"
#include <sstream>
#include <iostream>
MarkdownParser::MarkdownParser()
: mHtmlDocument(HtmlDocument::Create())
{
}
void MarkdownParser::processLine(const std::string& line)
MarkdownParser::~MarkdownParser()
{
}
void MarkdownParser::run(const std::string& content)
void MarkdownParser::onMultilineQuote()
{
std::stringstream ss(content);
std::string line;
while (std::getline(ss, line, '\n'))
std::cout << "Adding multiline quote " << mDocumentContent << std::endl;
auto quote = std::make_unique<MarkdownMultilineQuote>(mMultilineTag);
quote->appendTextContent(mDocumentContent);
mDocumentContent.clear();
mDocumentState = DocumentState::NONE;
mMarkdownDocument->addElement(std::move(quote));
onNewParagraph();
}
void MarkdownParser::onInlineQuote()
{
std::cout << "Adding inline quote " << mLineContent << std::endl;
auto quote = std::make_unique<MarkdownInlineQuote>();
quote->appendTextContent(mLineContent);
mLineContent.clear();
mLineState = LineState::NONE;
if(mWorkingParagraph)
{
processLine(line);
mWorkingParagraph->addChild(std::move(quote));
}
}
HtmlDocumentPtr MarkdownParser::getHtml()
void MarkdownParser::onHeading(unsigned level)
{
return std::move(mHtmlDocument);
std::cout << "Adding heading: " << mLineContent << std::endl;
auto heading = std::make_unique<MarkdownHeading>(level);
heading->appendTextContent(mLineContent);
mMarkdownDocument->addElement(std::move(heading));
}
void MarkdownParser::onNewParagraph()
{
if (mWorkingParagraph)
{
onTextSpan();
if (!mWorkingParagraph->getNumChildren() == 0)
{
std::cout << "Adding para to document" << std::endl;
mMarkdownDocument->addElement(std::move(mWorkingParagraph));
}
}
mWorkingParagraph = std::make_unique<MarkdownParagraph>();
}
void MarkdownParser::onTextSpan()
{
mLineContent.clear();
if(mWorkingParagraph && !mDocumentContent.empty())
{
std::cout << "Adding text " << mDocumentContent << std::endl;
auto text_span = std::make_unique<MarkdownTextSpan>();
text_span->appendTextContent(mDocumentContent);
mWorkingParagraph->addChild(std::move(text_span));
mDocumentContent.clear();
}
}
std::pair<unsigned, bool> MarkdownParser::onTick(unsigned tickCount)
{
unsigned new_tick_count = tickCount;
bool stop_line_processing = false;
if (tickCount == 2)
{
if (mDocumentState == DocumentState::IN_MULTILINEQUOTE)
{
onMultilineQuote();
stop_line_processing = true;
}
else
{
onNewParagraph();
mLineState = LineState::IN_MULTILINE_TAG;
new_tick_count = 0;
mDocumentState = DocumentState::IN_MULTILINEQUOTE;
}
}
else if(mLineState == LineState::IN_INLINEQUOTE)
{
if (mLineContent.empty())
{
mLineState = LineState::NONE;
new_tick_count++;
}
else
{
new_tick_count = 0;
onInlineQuote();
}
}
else if(mDocumentState == DocumentState::IN_MULTILINEQUOTE)
{
new_tick_count++;
mLineContent += '`';
}
else
{
new_tick_count++;
mLineState = LineState::IN_INLINEQUOTE;
}
return {new_tick_count, stop_line_processing};
}
void MarkdownParser::processLine()
{
mLineContent.clear();
mLineState = LineState::NONE;
unsigned heading_level{0};
unsigned tick_count{0};
bool flushed_pre_inline = false;
for(auto c : mWorkingLine)
{
if (c == '`')
{
auto [ret_tick_count, stop_line_processing] = onTick(tick_count);
tick_count = ret_tick_count;
if(stop_line_processing)
{
return;
}
}
else
{
if (mLineState == LineState::IN_INLINEQUOTE)
{
if (!flushed_pre_inline)
{
std::cout << "Flushing pre-line " << std::endl;
mDocumentContent += mLineContent;
onTextSpan();
flushed_pre_inline = true;
}
mLineContent += c;
}
else if (mDocumentState == DocumentState::IN_MULTILINEQUOTE)
{
mLineContent += c;
}
else
{
if (c == '#')
{
onNewParagraph();
mLineState = LineState::IN_HEADING;
heading_level++;
}
else
{
mLineContent += c;
}
}
}
}
if (mLineState == LineState::IN_HEADING)
{
onHeading(heading_level);
}
else if(mLineState == LineState::IN_MULTILINE_TAG)
{
mMultilineTag = mLineContent;
}
else if (mLineState == LineState::IN_INLINEQUOTE)
{
onTextSpan();
}
else
{
if (mLineContent.size() > 0)
{
mDocumentContent.append(mLineContent);
}
}
}
void MarkdownParser::onEmptyLine()
{
onNewParagraph();
}
std::unique_ptr<MarkdownDocument> MarkdownParser::run(const std::string& content)
{
mMarkdownDocument = std::make_unique<MarkdownDocument>();
std::stringstream ss(content);
std::string line;
while (std::getline(ss, line, '\n'))
{
if (line.empty())
{
onEmptyLine();
continue;
}
mWorkingLine = line;
processLine();
}
onTextSpan();
onNewParagraph();
return std::move(mMarkdownDocument);
}