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

@ -0,0 +1,59 @@
#include "MarkdownConverter.h"
#include "HtmlDocument.h"
#include "HtmlElement.h"
#include "HtmlParagraphElement.h"
#include "HtmlTextRun.h"
#include "MarkdownDocument.h"
std::unique_ptr<HtmlDocument> MarkdownConverter::convert(MarkdownDocument* markdownDoc) const
{
auto html_doc = std::make_unique<HtmlDocument>();
for(unsigned idx=0; idx<markdownDoc->getNumElements();idx++)
{
auto md_element = markdownDoc->getElement(idx);
if (md_element->getType() == MarkdownElement::Type::HEADING)
{
auto heading_level = dynamic_cast<MarkdownHeading*>(md_element)->getLevel();
auto html_element = std::make_unique<HtmlHeadingElement>(heading_level);
html_element->setText(md_element->getTextContent());
html_doc->addElementToBody(std::move(html_element));
}
else if(md_element->getType() == MarkdownElement::Type::PARAGRAPH)
{
auto html_p_element = std::make_unique<HtmlParagraphElement>();
auto para_element = dynamic_cast<MarkdownParagraph*>(md_element);
for(unsigned idx=0; idx< para_element->getNumChildren(); idx++)
{
auto child = para_element->getChild(idx);
if (child->getType() == MarkdownElement::Type::INLINE_QUOTE)
{
auto html_quote = std::make_unique<HtmlCodeElement>();
html_quote->setText(child->getTextContent());
html_p_element->addChild(std::move(html_quote));
}
else if(child->getType() == MarkdownElement::Type::TEXT_SPAN)
{
auto html_text = std::make_unique<HtmlTextRun>();
html_text->setText(child->getTextContent());
html_p_element->addChild(std::move(html_text));
}
}
html_doc->addElementToBody(std::move(html_p_element));
}
else if(md_element->getType() == MarkdownElement::Type::MULTILINE_QUOTE)
{
auto html_quote = std::make_unique<HtmlCodeElement>();
html_quote->setText(md_element->getTextContent());
html_doc->addElementToBody(std::move(html_quote));
}
}
return std::move(html_doc);
}