Initial site generation
This commit is contained in:
parent
f44c79dc1f
commit
fc44290e3f
35 changed files with 667 additions and 303 deletions
|
@ -31,3 +31,8 @@ void HtmlDocument::addElementToBody(std::unique_ptr<HtmlElement> element)
|
|||
body_element->addChild(std::move(element));
|
||||
}
|
||||
}
|
||||
|
||||
HtmlBodyElement* HtmlDocument::getBodyElement() const
|
||||
{
|
||||
return dynamic_cast<HtmlBodyElement*>(getRoot()->getFirstChildWithTagName("body"));
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <memory>
|
||||
|
||||
class HtmlElement;
|
||||
class HtmlBodyElement;
|
||||
|
||||
class HtmlDocument : public XmlDocument
|
||||
{
|
||||
|
@ -17,6 +18,8 @@ public:
|
|||
|
||||
void addElementToBody(std::unique_ptr<HtmlElement> element);
|
||||
|
||||
HtmlBodyElement* getBodyElement() const;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
|
|
@ -19,7 +19,8 @@ public:
|
|||
HYPERLINK,
|
||||
IMAGE,
|
||||
UNORDERED_LIST,
|
||||
LIST_ITEM
|
||||
LIST_ITEM,
|
||||
DIV
|
||||
};
|
||||
|
||||
HtmlElement(const std::string& tagName);
|
||||
|
@ -27,6 +28,20 @@ public:
|
|||
virtual Type getType() const = 0;
|
||||
};
|
||||
|
||||
class HtmlDivElement : public HtmlElement
|
||||
{
|
||||
public:
|
||||
HtmlDivElement() : HtmlElement("div")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::DIV;
|
||||
}
|
||||
};
|
||||
|
||||
class HtmlCodeElement : public HtmlElement
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
#include "HtmlWriter.h"
|
||||
|
||||
#include "HtmlDocument.h"
|
||||
#include "HtmlElement.h"
|
||||
#include "XmlElement.h"
|
||||
#include "XmlAttribute.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
HtmlWriter::HtmlWriter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string HtmlWriter::toString(HtmlElement* element, unsigned startDepth)
|
||||
{
|
||||
return element->toString(startDepth);
|
||||
}
|
||||
|
||||
std::string HtmlWriter::toString(HtmlDocument* document)
|
||||
{
|
||||
std::string content = "<!DOCTYPE html>\n";
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <string>
|
||||
|
||||
class HtmlDocument;
|
||||
class HtmlElement;
|
||||
|
||||
class HtmlWriter
|
||||
{
|
||||
|
@ -10,4 +11,6 @@ public:
|
|||
HtmlWriter();
|
||||
|
||||
std::string toString(HtmlDocument* document);
|
||||
|
||||
std::string toString(HtmlElement* element, unsigned startDepth=0);
|
||||
};
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "MarkdownComponents.h"
|
||||
|
||||
#include "StringUtils.h"
|
||||
|
||||
MarkdownTextSpan::Type MarkdownTextSpan::getType() const
|
||||
{
|
||||
return Type::TEXT_SPAN;
|
||||
|
@ -87,6 +89,13 @@ MarkdownLink::Type MarkdownLink::getType() const
|
|||
return Type::LINK;
|
||||
}
|
||||
|
||||
void MarkdownLink::doFieldSubstitution(Type elementType, const std::string& searchPhrase, const std::string& replacementPhrase)
|
||||
{
|
||||
if (elementType == Type::LINK)
|
||||
{
|
||||
mTarget = StringUtils::replaceWith(mTarget, searchPhrase, replacementPhrase);
|
||||
}
|
||||
}
|
||||
|
||||
MarkdownImage::MarkdownImage(const std::string& source, const std::string& alt)
|
||||
: mSource(source),
|
||||
|
|
|
@ -13,6 +13,28 @@ public:
|
|||
Type getType() const override;
|
||||
};
|
||||
|
||||
|
||||
class MarkdownLink : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
MarkdownLink(const std::string& target);
|
||||
|
||||
virtual ~MarkdownLink() = default;
|
||||
|
||||
const std::string& getTarget() const;
|
||||
|
||||
Type getType() const override;
|
||||
|
||||
void setTarget(const std::string& target)
|
||||
{
|
||||
mTarget = target;
|
||||
}
|
||||
|
||||
void doFieldSubstitution(Type elementType, const std::string& searchPhrase, const std::string& replacementPhrase) override;
|
||||
private:
|
||||
std::string mTarget;
|
||||
};
|
||||
|
||||
class MarkdownParagraph : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
|
@ -26,6 +48,27 @@ public:
|
|||
|
||||
MarkdownInlineElement* getChild(std::size_t idx) const;
|
||||
|
||||
void doFieldSubstitution(Type elementType, const std::string& searchPhrase, const std::string& replacementPhrase) override
|
||||
{
|
||||
for(auto& child : mChildren)
|
||||
{
|
||||
child->doFieldSubstitution(elementType, searchPhrase, replacementPhrase);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<MarkdownLink*> getAllLinks() const
|
||||
{
|
||||
std::vector<MarkdownLink*> links;
|
||||
for(auto& child : mChildren)
|
||||
{
|
||||
if (child->getType() == Type::LINK)
|
||||
{
|
||||
links.push_back(dynamic_cast<MarkdownLink*>(child.get()));
|
||||
}
|
||||
}
|
||||
return links;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<MarkdownInlineElement> > mChildren;
|
||||
};
|
||||
|
@ -79,20 +122,6 @@ public:
|
|||
Type getType() const override;
|
||||
};
|
||||
|
||||
class MarkdownLink : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
MarkdownLink(const std::string& target);
|
||||
|
||||
virtual ~MarkdownLink() = default;
|
||||
|
||||
const std::string& getTarget() const;
|
||||
|
||||
Type getType() const override;
|
||||
private:
|
||||
std::string mTarget;
|
||||
};
|
||||
|
||||
class MarkdownImage : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -3,16 +3,15 @@
|
|||
#include "HtmlDocument.h"
|
||||
#include "HtmlElement.h"
|
||||
#include "HtmlParagraphElement.h"
|
||||
#include "HtmlBodyElement.h"
|
||||
#include "HtmlTextRun.h"
|
||||
#include "MarkdownElement.h"
|
||||
#include "MarkdownComponents.h"
|
||||
|
||||
#include "MarkdownDocument.h"
|
||||
|
||||
std::unique_ptr<HtmlDocument> MarkdownConverter::convert(MarkdownDocument* markdownDoc) const
|
||||
void MarkdownConverter::convert(MarkdownDocument* markdownDoc, HtmlElement* parentElement) const
|
||||
{
|
||||
auto html_doc = std::make_unique<HtmlDocument>();
|
||||
|
||||
for(unsigned idx=0; idx<markdownDoc->getNumElements();idx++)
|
||||
{
|
||||
auto md_element = markdownDoc->getElement(idx);
|
||||
|
@ -23,7 +22,7 @@ std::unique_ptr<HtmlDocument> MarkdownConverter::convert(MarkdownDocument* markd
|
|||
auto html_element = std::make_unique<HtmlHeadingElement>(heading_level);
|
||||
html_element->setText(md_element->getTextContent());
|
||||
|
||||
html_doc->addElementToBody(std::move(html_element));
|
||||
parentElement->addChild(std::move(html_element));
|
||||
}
|
||||
else if(md_element->getType() == MarkdownElement::Type::PARAGRAPH)
|
||||
{
|
||||
|
@ -59,7 +58,7 @@ std::unique_ptr<HtmlDocument> MarkdownConverter::convert(MarkdownDocument* markd
|
|||
html_p_element->addChild(std::move(html_text));
|
||||
}
|
||||
}
|
||||
html_doc->addElementToBody(std::move(html_p_element));
|
||||
parentElement->addChild(std::move(html_p_element));
|
||||
}
|
||||
else if(md_element->getType() == MarkdownElement::Type::BULLET_LIST)
|
||||
{
|
||||
|
@ -72,15 +71,23 @@ std::unique_ptr<HtmlDocument> MarkdownConverter::convert(MarkdownDocument* markd
|
|||
html_list_item->setText(child->getTextContent());
|
||||
html_list->addChild(std::move(html_list_item));
|
||||
}
|
||||
html_doc->addElementToBody(std::move(html_list));
|
||||
parentElement->addChild(std::move(html_list));
|
||||
}
|
||||
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));
|
||||
parentElement->addChild(std::move(html_quote));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<HtmlDocument> MarkdownConverter::convert(MarkdownDocument* markdownDoc) const
|
||||
{
|
||||
auto html_doc = std::make_unique<HtmlDocument>();
|
||||
|
||||
auto body_element = html_doc->getBodyElement();
|
||||
convert(markdownDoc, body_element);
|
||||
|
||||
return std::move(html_doc);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <memory>
|
||||
|
||||
class HtmlDocument;
|
||||
class HtmlElement;
|
||||
class MarkdownDocument;
|
||||
|
||||
class MarkdownConverter
|
||||
|
@ -10,4 +11,6 @@ class MarkdownConverter
|
|||
public:
|
||||
std::unique_ptr<HtmlDocument> convert(MarkdownDocument* markdownDoc) const;
|
||||
|
||||
void convert(MarkdownDocument* markdownDoc, HtmlElement* parentElement) const;
|
||||
|
||||
};
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "MarkdownDocument.h"
|
||||
|
||||
#include "MarkdownElement.h"
|
||||
#include "MarkdownComponents.h"
|
||||
|
||||
|
||||
void MarkdownDocument::addElement(std::unique_ptr<MarkdownElement> element)
|
||||
{
|
||||
|
@ -16,3 +18,25 @@ MarkdownElement* MarkdownDocument::getElement(std::size_t idx) const
|
|||
{
|
||||
return mElements[idx].get();
|
||||
}
|
||||
|
||||
void MarkdownDocument::doLinkTargetSubstitution(const std::string& targetString, const std::string& replacementString)
|
||||
{
|
||||
for(auto& element : mElements)
|
||||
{
|
||||
element->doFieldSubstitution(MarkdownElement::Type::LINK, targetString, replacementString);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<MarkdownLink*> MarkdownDocument::getAllLinks() const
|
||||
{
|
||||
std::vector<MarkdownLink*> links;
|
||||
for(auto& element : mElements)
|
||||
{
|
||||
if (element->getType() == MarkdownElement::Type::PARAGRAPH)
|
||||
{
|
||||
auto para_links = dynamic_cast<MarkdownParagraph*>(element.get())->getAllLinks();
|
||||
links.insert(links.end(), para_links.begin(), para_links.end());
|
||||
}
|
||||
}
|
||||
return links;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <memory>
|
||||
|
||||
class MarkdownElement;
|
||||
class MarkdownLink;
|
||||
|
||||
class MarkdownDocument
|
||||
{
|
||||
|
@ -14,6 +15,10 @@ public:
|
|||
|
||||
MarkdownElement* getElement(std::size_t idx) const;
|
||||
|
||||
void doLinkTargetSubstitution(const std::string& targetString, const std::string& replacementString);
|
||||
|
||||
std::vector<MarkdownLink*> getAllLinks() const;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<MarkdownElement> > mElements;
|
||||
};
|
||||
|
|
|
@ -29,6 +29,11 @@ public:
|
|||
const std::string& getTextContent() const;
|
||||
|
||||
virtual Type getType() const = 0;
|
||||
|
||||
virtual void doFieldSubstitution(Type elementType, const std::string& searchPhrase, const std::string& replacementPhrase)
|
||||
{
|
||||
|
||||
}
|
||||
private:
|
||||
std::string mTextContent;
|
||||
};
|
||||
|
|
|
@ -19,7 +19,6 @@ MarkdownParser::~MarkdownParser()
|
|||
|
||||
void MarkdownParser::onMultilineQuote()
|
||||
{
|
||||
std::cout << "Adding multiline quote " << mDocumentContent << std::endl;
|
||||
auto quote = std::make_unique<MarkdownMultilineQuote>(mWorkingTag);
|
||||
quote->appendTextContent(mDocumentContent);
|
||||
|
||||
|
@ -34,8 +33,6 @@ void MarkdownParser::onMultilineQuote()
|
|||
|
||||
void MarkdownParser::onInlineQuote()
|
||||
{
|
||||
std::cout << "Adding inline quote " << mLineContent << std::endl;
|
||||
|
||||
auto quote = std::make_unique<MarkdownInlineQuote>();
|
||||
quote->appendTextContent(mLineContent);
|
||||
mLineContent.clear();
|
||||
|
@ -49,8 +46,6 @@ void MarkdownParser::onInlineQuote()
|
|||
|
||||
void MarkdownParser::onHeading(unsigned level)
|
||||
{
|
||||
std::cout << "Adding heading: " << mLineContent << std::endl;
|
||||
|
||||
auto heading = std::make_unique<MarkdownHeading>(level);
|
||||
heading->appendTextContent(mLineContent);
|
||||
mMarkdownDocument->addElement(std::move(heading));
|
||||
|
@ -60,7 +55,6 @@ void MarkdownParser::onNewParagraph()
|
|||
{
|
||||
if (mWorkingBulletList)
|
||||
{
|
||||
std::cout << "Adding bullets to document" << std::endl;
|
||||
mMarkdownDocument->addElement(std::move(mWorkingBulletList));
|
||||
mWorkingBulletList.reset();
|
||||
mDocumentState == DocumentState::NONE;
|
||||
|
@ -71,7 +65,6 @@ void MarkdownParser::onNewParagraph()
|
|||
|
||||
if (!mWorkingParagraph->getNumChildren() == 0)
|
||||
{
|
||||
std::cout << "Adding para to document" << std::endl;
|
||||
mMarkdownDocument->addElement(std::move(mWorkingParagraph));
|
||||
}
|
||||
}
|
||||
|
@ -84,8 +77,6 @@ void MarkdownParser::onTextSpan()
|
|||
|
||||
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));
|
||||
|
@ -141,7 +132,6 @@ std::pair<unsigned, bool> MarkdownParser::onTick(unsigned tickCount)
|
|||
|
||||
void MarkdownParser::onLink()
|
||||
{
|
||||
std::cout << "Adding hyperlink to " << mLineContent << " with tag " << mWorkingTag << std::endl;
|
||||
auto element = std::make_unique<MarkdownLink>(mLineContent);
|
||||
mLineContent.clear();
|
||||
|
||||
|
@ -157,7 +147,6 @@ void MarkdownParser::onLink()
|
|||
|
||||
void MarkdownParser::onImage()
|
||||
{
|
||||
std::cout << "Adding image with path " << mLineContent << " and alt" << mWorkingTag << std::endl;
|
||||
auto element = std::make_unique<MarkdownImage>(mLineContent, mWorkingTag);
|
||||
mLineContent.clear();
|
||||
|
||||
|
@ -173,8 +162,6 @@ void MarkdownParser::onImage()
|
|||
|
||||
void MarkdownParser::onBulletItem()
|
||||
{
|
||||
std::cout << "Adding bullet item " << mLineContent << std::endl;
|
||||
|
||||
if (!mWorkingBulletList)
|
||||
{
|
||||
mWorkingBulletList = std::make_unique<MarkdownBulletList>();
|
||||
|
@ -231,7 +218,6 @@ void MarkdownParser::processLine()
|
|||
{
|
||||
if (!flushed_pre_inline)
|
||||
{
|
||||
std::cout << "Flushing pre-line " << std::endl;
|
||||
mDocumentContent += mLineContent;
|
||||
onTextSpan();
|
||||
flushed_pre_inline = true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue