Start adding markdown conversion to site generator.
This commit is contained in:
parent
f0091f9e04
commit
f44c79dc1f
31 changed files with 692 additions and 461 deletions
|
@ -15,6 +15,10 @@ list(APPEND web_LIB_INCLUDES
|
|||
markdown/MarkdownConverter.cpp
|
||||
markdown/MarkdownDocument.h
|
||||
markdown/MarkdownDocument.cpp
|
||||
markdown/MarkdownComponents.h
|
||||
markdown/MarkdownComponents.cpp
|
||||
markdown/MarkdownElement.h
|
||||
markdown/MarkdownElement.cpp
|
||||
html/HtmlWriter.cpp
|
||||
html/HtmlDocument.cpp
|
||||
html/HtmlElement.cpp
|
||||
|
|
122
src/web/markdown/MarkdownComponents.cpp
Normal file
122
src/web/markdown/MarkdownComponents.cpp
Normal file
|
@ -0,0 +1,122 @@
|
|||
#include "MarkdownComponents.h"
|
||||
|
||||
MarkdownTextSpan::Type MarkdownTextSpan::getType() const
|
||||
{
|
||||
return Type::TEXT_SPAN;
|
||||
}
|
||||
|
||||
MarkdownParagraph::Type MarkdownParagraph::getType() const
|
||||
{
|
||||
return Type::PARAGRAPH;
|
||||
}
|
||||
|
||||
void MarkdownParagraph::addChild(std::unique_ptr<MarkdownInlineElement> child)
|
||||
{
|
||||
mChildren.push_back(std::move(child));
|
||||
}
|
||||
|
||||
std::size_t MarkdownParagraph::getNumChildren() const
|
||||
{
|
||||
return mChildren.size();
|
||||
}
|
||||
|
||||
MarkdownInlineElement* MarkdownParagraph::getChild(std::size_t idx) const
|
||||
{
|
||||
return mChildren[idx].get();
|
||||
}
|
||||
|
||||
MarkdownBulletItem::Type MarkdownBulletItem::getType() const
|
||||
{
|
||||
return Type::BULLET_ITEM;
|
||||
}
|
||||
|
||||
MarkdownBulletList::Type MarkdownBulletList::getType() const
|
||||
{
|
||||
return Type::BULLET_LIST;
|
||||
}
|
||||
|
||||
void MarkdownBulletList::addChild(std::unique_ptr<MarkdownBulletItem> child)
|
||||
{
|
||||
mChildren.push_back(std::move(child));
|
||||
}
|
||||
|
||||
std::size_t MarkdownBulletList::getNumChildren() const
|
||||
{
|
||||
return mChildren.size();
|
||||
}
|
||||
|
||||
MarkdownBulletItem* MarkdownBulletList::getChild(std::size_t idx) const
|
||||
{
|
||||
return mChildren[idx].get();
|
||||
}
|
||||
|
||||
MarkdownHeading::MarkdownHeading(unsigned level)
|
||||
: mLevel(level)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MarkdownHeading::Type MarkdownHeading::getType() const
|
||||
{
|
||||
return Type::HEADING;
|
||||
}
|
||||
|
||||
unsigned MarkdownHeading::getLevel() const
|
||||
{
|
||||
return mLevel;
|
||||
}
|
||||
|
||||
MarkdownInlineQuote::Type MarkdownInlineQuote::getType() const
|
||||
{
|
||||
return Type::INLINE_QUOTE;
|
||||
}
|
||||
|
||||
MarkdownLink::MarkdownLink(const std::string& target)
|
||||
: mTarget(target)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
const std::string& MarkdownLink::getTarget() const
|
||||
{
|
||||
return mTarget;
|
||||
}
|
||||
|
||||
MarkdownLink::Type MarkdownLink::getType() const
|
||||
{
|
||||
return Type::LINK;
|
||||
}
|
||||
|
||||
|
||||
MarkdownImage::MarkdownImage(const std::string& source, const std::string& alt)
|
||||
: mSource(source),
|
||||
mAlt(alt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MarkdownImage::Type MarkdownImage::getType() const
|
||||
{
|
||||
return Type::IMAGE;
|
||||
}
|
||||
|
||||
const std::string& MarkdownImage::getSource() const
|
||||
{
|
||||
return mSource;
|
||||
}
|
||||
|
||||
const std::string& MarkdownImage::getAlt() const
|
||||
{
|
||||
return mAlt;
|
||||
}
|
||||
|
||||
MarkdownMultilineQuote::MarkdownMultilineQuote(const std::string& tag)
|
||||
: mTag(tag)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MarkdownMultilineQuote::Type MarkdownMultilineQuote::getType() const
|
||||
{
|
||||
return Type::MULTILINE_QUOTE;
|
||||
}
|
124
src/web/markdown/MarkdownComponents.h
Normal file
124
src/web/markdown/MarkdownComponents.h
Normal file
|
@ -0,0 +1,124 @@
|
|||
#pragma once
|
||||
|
||||
#include "MarkdownElement.h"
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
class MarkdownTextSpan : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownTextSpan() = default;
|
||||
|
||||
Type getType() const override;
|
||||
};
|
||||
|
||||
class MarkdownParagraph : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownParagraph() = default;
|
||||
|
||||
Type getType() const override;
|
||||
|
||||
void addChild(std::unique_ptr<MarkdownInlineElement> child);
|
||||
|
||||
std::size_t getNumChildren() const;
|
||||
|
||||
MarkdownInlineElement* getChild(std::size_t idx) const;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<MarkdownInlineElement> > mChildren;
|
||||
};
|
||||
|
||||
class MarkdownBulletItem : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownBulletItem() = default;
|
||||
|
||||
Type getType() const override;
|
||||
};
|
||||
|
||||
class MarkdownBulletList : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownBulletList() = default;
|
||||
|
||||
Type getType() const override;
|
||||
|
||||
void addChild(std::unique_ptr<MarkdownBulletItem> child);
|
||||
|
||||
std::size_t getNumChildren() const;
|
||||
|
||||
MarkdownBulletItem* getChild(std::size_t idx) const;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<MarkdownBulletItem> > mChildren;
|
||||
};
|
||||
|
||||
class MarkdownHeading : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
MarkdownHeading(unsigned level);
|
||||
|
||||
virtual ~MarkdownHeading() = default;
|
||||
|
||||
Type getType() const override;
|
||||
|
||||
unsigned getLevel() const;
|
||||
|
||||
private:
|
||||
unsigned mLevel{1};
|
||||
};
|
||||
|
||||
class MarkdownInlineQuote : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~MarkdownInlineQuote() = default;
|
||||
|
||||
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:
|
||||
MarkdownImage(const std::string& source, const std::string& alt);
|
||||
|
||||
virtual ~MarkdownImage() = default;
|
||||
|
||||
Type getType() const override;
|
||||
|
||||
const std::string& getSource() const;
|
||||
|
||||
const std::string& getAlt() const;
|
||||
|
||||
private:
|
||||
std::string mSource;
|
||||
std::string mAlt;
|
||||
};
|
||||
|
||||
class MarkdownMultilineQuote : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
MarkdownMultilineQuote(const std::string& tag);
|
||||
|
||||
virtual ~MarkdownMultilineQuote() = default;
|
||||
|
||||
Type getType() const override;
|
||||
private:
|
||||
std::string mTag;
|
||||
};
|
|
@ -4,6 +4,8 @@
|
|||
#include "HtmlElement.h"
|
||||
#include "HtmlParagraphElement.h"
|
||||
#include "HtmlTextRun.h"
|
||||
#include "MarkdownElement.h"
|
||||
#include "MarkdownComponents.h"
|
||||
|
||||
#include "MarkdownDocument.h"
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
#include "MarkdownDocument.h"
|
||||
|
||||
#include "MarkdownElement.h"
|
||||
|
||||
void MarkdownDocument::addElement(std::unique_ptr<MarkdownElement> element)
|
||||
{
|
||||
mElements.push_back(std::move(element));
|
||||
}
|
||||
|
||||
std::size_t MarkdownDocument::getNumElements() const
|
||||
{
|
||||
return mElements.size();
|
||||
}
|
||||
|
||||
MarkdownElement* MarkdownDocument::getElement(std::size_t idx) const
|
||||
{
|
||||
return mElements[idx].get();
|
||||
}
|
|
@ -3,262 +3,16 @@
|
|||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
class MarkdownElement
|
||||
{
|
||||
public:
|
||||
enum class Type
|
||||
{
|
||||
HEADING,
|
||||
PARAGRAPH,
|
||||
TEXT_SPAN,
|
||||
INLINE_CODE,
|
||||
MULTILINE_CODE,
|
||||
INLINE_QUOTE,
|
||||
MULTILINE_QUOTE,
|
||||
INLINE_SPECIAL,
|
||||
MULTILINE_SPECIAL,
|
||||
LINK,
|
||||
IMAGE,
|
||||
BULLET_ITEM,
|
||||
BULLET_LIST
|
||||
};
|
||||
|
||||
virtual ~MarkdownElement() = default;
|
||||
|
||||
void appendTextContent(const std::string& content)
|
||||
{
|
||||
mTextContent += content;
|
||||
}
|
||||
|
||||
const std::string& getTextContent() const
|
||||
{
|
||||
return mTextContent;
|
||||
}
|
||||
|
||||
virtual Type getType() const = 0;
|
||||
private:
|
||||
std::string mTextContent;
|
||||
};
|
||||
|
||||
class MarkdownInlineElement : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownInlineElement() = default;
|
||||
};
|
||||
|
||||
class MarkdownTextSpan : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownTextSpan() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::TEXT_SPAN;
|
||||
}
|
||||
};
|
||||
|
||||
class MarkdownParagraph : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownParagraph() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::PARAGRAPH;
|
||||
}
|
||||
|
||||
void addChild(std::unique_ptr<MarkdownInlineElement> child)
|
||||
{
|
||||
mChildren.push_back(std::move(child));
|
||||
}
|
||||
|
||||
std::size_t getNumChildren() const
|
||||
{
|
||||
return mChildren.size();
|
||||
}
|
||||
|
||||
MarkdownInlineElement* getChild(std::size_t idx) const
|
||||
{
|
||||
return mChildren[idx].get();
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<MarkdownInlineElement> > mChildren;
|
||||
};
|
||||
|
||||
class MarkdownBulletItem : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownBulletItem() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::BULLET_ITEM;
|
||||
}
|
||||
};
|
||||
|
||||
class MarkdownBulletList : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownBulletList() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::BULLET_LIST;
|
||||
}
|
||||
|
||||
void addChild(std::unique_ptr<MarkdownBulletItem> child)
|
||||
{
|
||||
mChildren.push_back(std::move(child));
|
||||
}
|
||||
|
||||
std::size_t getNumChildren() const
|
||||
{
|
||||
return mChildren.size();
|
||||
}
|
||||
|
||||
MarkdownBulletItem* getChild(std::size_t idx) const
|
||||
{
|
||||
return mChildren[idx].get();
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<MarkdownBulletItem> > mChildren;
|
||||
};
|
||||
|
||||
class MarkdownHeading : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
MarkdownHeading(unsigned level)
|
||||
: mLevel(level)
|
||||
{
|
||||
|
||||
}
|
||||
virtual ~MarkdownHeading() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::HEADING;
|
||||
}
|
||||
|
||||
unsigned getLevel() const
|
||||
{
|
||||
return mLevel;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned mLevel{1};
|
||||
};
|
||||
|
||||
|
||||
|
||||
class MarkdownInlineQuote : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~MarkdownInlineQuote() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::INLINE_QUOTE;
|
||||
}
|
||||
};
|
||||
|
||||
class MarkdownLink : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
|
||||
MarkdownLink(const std::string& target)
|
||||
: mTarget(target)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual ~MarkdownLink() = default;
|
||||
|
||||
const std::string& getTarget() const
|
||||
{
|
||||
return mTarget;
|
||||
}
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::LINK;
|
||||
}
|
||||
private:
|
||||
std::string mTarget;
|
||||
};
|
||||
|
||||
class MarkdownImage : public MarkdownInlineElement
|
||||
{
|
||||
public:
|
||||
MarkdownImage(const std::string& source, const std::string& alt)
|
||||
: mSource(source),
|
||||
mAlt(alt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual ~MarkdownImage() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::IMAGE;
|
||||
}
|
||||
|
||||
const std::string& getSource() const
|
||||
{
|
||||
return mSource;
|
||||
}
|
||||
|
||||
const std::string& getAlt() const
|
||||
{
|
||||
return mAlt;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string mSource;
|
||||
std::string mAlt;
|
||||
};
|
||||
|
||||
class MarkdownMultilineQuote : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
MarkdownMultilineQuote(const std::string& tag)
|
||||
: mTag(tag)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual ~MarkdownMultilineQuote() = default;
|
||||
|
||||
Type getType() const override
|
||||
{
|
||||
return Type::MULTILINE_QUOTE;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string mTag;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class MarkdownElement;
|
||||
|
||||
class MarkdownDocument
|
||||
{
|
||||
public:
|
||||
void addElement(std::unique_ptr<MarkdownElement> element)
|
||||
{
|
||||
mElements.push_back(std::move(element));
|
||||
}
|
||||
void addElement(std::unique_ptr<MarkdownElement> element);
|
||||
|
||||
std::size_t getNumElements() const
|
||||
{
|
||||
return mElements.size();
|
||||
}
|
||||
std::size_t getNumElements() const;
|
||||
|
||||
MarkdownElement* getElement(std::size_t idx) const
|
||||
{
|
||||
return mElements[idx].get();
|
||||
}
|
||||
MarkdownElement* getElement(std::size_t idx) const;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<MarkdownElement> > mElements;
|
||||
|
|
11
src/web/markdown/MarkdownElement.cpp
Normal file
11
src/web/markdown/MarkdownElement.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include "MarkdownElement.h"
|
||||
|
||||
void MarkdownElement::appendTextContent(const std::string& content)
|
||||
{
|
||||
mTextContent += content;
|
||||
}
|
||||
|
||||
const std::string& MarkdownElement::getTextContent() const
|
||||
{
|
||||
return mTextContent;
|
||||
}
|
40
src/web/markdown/MarkdownElement.h
Normal file
40
src/web/markdown/MarkdownElement.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class MarkdownElement
|
||||
{
|
||||
public:
|
||||
enum class Type
|
||||
{
|
||||
HEADING,
|
||||
PARAGRAPH,
|
||||
TEXT_SPAN,
|
||||
INLINE_CODE,
|
||||
MULTILINE_CODE,
|
||||
INLINE_QUOTE,
|
||||
MULTILINE_QUOTE,
|
||||
INLINE_SPECIAL,
|
||||
MULTILINE_SPECIAL,
|
||||
LINK,
|
||||
IMAGE,
|
||||
BULLET_ITEM,
|
||||
BULLET_LIST
|
||||
};
|
||||
|
||||
virtual ~MarkdownElement() = default;
|
||||
|
||||
void appendTextContent(const std::string& content);
|
||||
|
||||
const std::string& getTextContent() const;
|
||||
|
||||
virtual Type getType() const = 0;
|
||||
private:
|
||||
std::string mTextContent;
|
||||
};
|
||||
|
||||
class MarkdownInlineElement : public MarkdownElement
|
||||
{
|
||||
public:
|
||||
virtual ~MarkdownInlineElement() = default;
|
||||
};
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "MarkdownDocument.h"
|
||||
#include "StringUtils.h"
|
||||
#include "MarkdownComponents.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue