Some markdown processing.
This commit is contained in:
parent
31b479e9f6
commit
ec11529b9a
23 changed files with 677 additions and 135 deletions
59
src/web/markdown/MarkdownConverter.cpp
Normal file
59
src/web/markdown/MarkdownConverter.cpp
Normal 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);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue