Add PDF writer.
This commit is contained in:
parent
c05b7b6315
commit
9c116b1efd
72 changed files with 1819 additions and 114 deletions
|
@ -8,6 +8,9 @@ list(APPEND web_LIB_INCLUDES
|
|||
markdown/MarkdownParser.cpp
|
||||
html/HtmlWriter.cpp
|
||||
html/HtmlDocument.cpp
|
||||
html/HtmlElement.cpp
|
||||
html/elements/HtmlHeadElement.cpp
|
||||
html/elements/HtmlBodyElement.cpp
|
||||
DocumentConverter.cpp)
|
||||
|
||||
# add the executable
|
||||
|
@ -21,6 +24,7 @@ target_include_directories(web PUBLIC
|
|||
"${CMAKE_CURRENT_SOURCE_DIR}/xml"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/xml/xml-elements"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/html"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/html/elements"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/markdown"
|
||||
)
|
||||
set_property(TARGET web PROPERTY FOLDER src)
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
#include "HtmlDocument.h"
|
||||
|
||||
HtmlDocument::HtmlDocument()
|
||||
: XmlDocument()
|
||||
{
|
||||
#include "HtmlHeadElement.h"
|
||||
#include "HtmlBodyElement.h"
|
||||
|
||||
HtmlDocument::HtmlDocument()
|
||||
: XmlDocument()
|
||||
{
|
||||
auto root = XmlElement::Create("html");
|
||||
SetRoot(std::move(root));
|
||||
|
||||
auto header = std::make_unique<HtmlHeadElement>();
|
||||
GetRoot()->AddChild(std::move(header));
|
||||
|
||||
auto body = std::make_unique<HtmlBodyElement>();
|
||||
GetRoot()->AddChild(std::move(body));
|
||||
}
|
||||
|
||||
std::shared_ptr<HtmlDocument> HtmlDocument::Create()
|
||||
|
|
|
@ -6,9 +6,10 @@ class HtmlDocument : public XmlDocument
|
|||
{
|
||||
public:
|
||||
|
||||
HtmlDocument();
|
||||
HtmlDocument();
|
||||
|
||||
static std::shared_ptr<HtmlDocument> Create();
|
||||
|
||||
static std::shared_ptr<HtmlDocument> Create();
|
||||
};
|
||||
|
||||
using HtmlDocumentPtr = std::shared_ptr<HtmlDocument>;
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include "HtmlDocument.h";
|
||||
|
||||
class HtmlDomManager
|
||||
{
|
||||
public:
|
||||
|
||||
HtmlDomManager();
|
||||
|
||||
HtmlDocument* GetDocument();
|
||||
|
||||
private:
|
||||
HtmlDocumentPtr mDocument;
|
||||
};
|
|
@ -6,7 +6,7 @@ HtmlElement::HtmlElement(const std::string& tagName)
|
|||
|
||||
}
|
||||
|
||||
static std::unique_ptr<HtmlElement> HtmlElement::CreateUnique(const std::string& tagName)
|
||||
std::unique_ptr<HtmlElement> HtmlElement::CreateUnique(const std::string& tagName)
|
||||
{
|
||||
return std::make_unique<HtmlElement>(tagName);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
#include "../xml/xml-elements/XmlElement.h"
|
||||
#include "XmlElement.h"
|
||||
|
||||
class HtmlElement : public XmlElement
|
||||
{
|
||||
HtmlElement(const std::string& tagName);
|
||||
public:
|
||||
HtmlElement(const std::string& tagName);
|
||||
|
||||
static std::unique_ptr<HtmlElement> CreateUnique(const std::string& tagName);
|
||||
static std::unique_ptr<HtmlElement> CreateUnique(const std::string& tagName);
|
||||
};
|
||||
|
||||
using HtmlElementUPtr = std::unique_ptr<HtmlElement>;
|
||||
|
|
|
@ -5,7 +5,58 @@ HtmlWriter::HtmlWriter()
|
|||
|
||||
}
|
||||
|
||||
std::string HtmlWriter::ToString(XmlElement* element, unsigned depth)
|
||||
{
|
||||
const auto prefix = std::string(2*depth, ' ');
|
||||
|
||||
auto content = prefix + "<" + element->GetTagName();
|
||||
for (std::size_t idx=0; idx< element->GetNumAttributes(); idx++)
|
||||
{
|
||||
auto attribute = element->GetAttribute(idx);
|
||||
content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\"";
|
||||
}
|
||||
|
||||
const auto num_children = element->GetNumChildren();
|
||||
if (num_children == 0 && element->GetText().empty())
|
||||
{
|
||||
content += "/>\n";
|
||||
return content;
|
||||
}
|
||||
else
|
||||
{
|
||||
content += ">";
|
||||
}
|
||||
|
||||
if (!element->GetText().empty())
|
||||
{
|
||||
content += element->GetText();
|
||||
}
|
||||
|
||||
if (num_children>0)
|
||||
{
|
||||
content += "\n";
|
||||
}
|
||||
for (std::size_t idx=0; idx< element->GetNumChildren(); idx++)
|
||||
{
|
||||
auto child = element->GetChild(idx);
|
||||
content += ToString(child, depth+1);
|
||||
}
|
||||
if (num_children>0)
|
||||
{
|
||||
content += prefix;
|
||||
}
|
||||
|
||||
content += "</" + element->GetTagName() + ">\n";
|
||||
return content;
|
||||
}
|
||||
|
||||
std::string HtmlWriter::ToString(HtmlDocumentPtr document)
|
||||
{
|
||||
return "<html>Uh oh!!!!</html>";
|
||||
std::string content = "<!DOCTYPE html>\n";
|
||||
|
||||
if (auto root = document->GetRoot())
|
||||
{
|
||||
content += ToString(root);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,10 @@ class HtmlWriter
|
|||
{
|
||||
public:
|
||||
|
||||
HtmlWriter();
|
||||
HtmlWriter();
|
||||
|
||||
std::string ToString(HtmlDocumentPtr document);
|
||||
std::string ToString(HtmlDocumentPtr document);
|
||||
|
||||
private:
|
||||
std::string ToString(XmlElement* element, unsigned depth=0);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include "HtmlElement.h"
|
||||
|
||||
class HtmlBodyElement : public HtmlElement
|
||||
{
|
||||
public:
|
||||
HtmlBodyElement() : HtmlElement("body")
|
||||
{
|
||||
|
||||
}
|
||||
};
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include "HtmlElement.h"
|
||||
|
||||
class HtmlHeadElement : public HtmlElement
|
||||
{
|
||||
public:
|
||||
HtmlHeadElement() : HtmlElement("head")
|
||||
{
|
||||
|
||||
}
|
||||
};
|
|
@ -1,17 +1,30 @@
|
|||
#include "MarkdownParser.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
MarkdownParser::MarkdownParser()
|
||||
: mHtmlDocument(HtmlDocument::Create())
|
||||
: mHtmlDocument(HtmlDocument::Create())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MarkdownParser::ProcessLine(const std::string)
|
||||
void MarkdownParser::ProcessLine(const std::string& line)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MarkdownParser::Run(const std::string& content)
|
||||
{
|
||||
std::stringstream ss(content);
|
||||
std::string line;
|
||||
while (std::getline(ss, line, '\n'))
|
||||
{
|
||||
ProcessLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
HtmlDocumentPtr MarkdownParser::GetHtml()
|
||||
{
|
||||
return mHtmlDocument;
|
||||
return mHtmlDocument;
|
||||
}
|
||||
|
|
|
@ -4,13 +4,27 @@
|
|||
|
||||
class MarkdownParser
|
||||
{
|
||||
HtmlDocumentPtr mHtmlDocument;
|
||||
enum class DocumentState
|
||||
{
|
||||
None
|
||||
};
|
||||
|
||||
enum class LineState
|
||||
{
|
||||
None
|
||||
};
|
||||
|
||||
|
||||
DocumentState mDocumentState {DocumentState::None};
|
||||
HtmlDocumentPtr mHtmlDocument;
|
||||
|
||||
public:
|
||||
|
||||
MarkdownParser();
|
||||
MarkdownParser();
|
||||
|
||||
void ProcessLine(const std::string);
|
||||
HtmlDocumentPtr GetHtml();
|
||||
|
||||
HtmlDocumentPtr GetHtml();
|
||||
void ProcessLine(const std::string& line);
|
||||
|
||||
void Run(const std::string& content);
|
||||
};
|
||||
|
|
|
@ -9,15 +9,14 @@ XmlParser::XmlParser()
|
|||
: mDocument(XmlDocument::Create()),
|
||||
mDocumentState(XmlParser::DocumentState::Await_Prolog),
|
||||
mLineState(XmlParser::LineState::Await_Tag_Open),
|
||||
mParentElement(nullptr),
|
||||
mWorkingElement(nullptr)
|
||||
mWorkingElements()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void XmlParser::ProcessLine(const std::string& input)
|
||||
{
|
||||
for(std::size_t idx=0; idx<input.size(); idx++)
|
||||
for (std::size_t idx=0; idx<input.size(); idx++)
|
||||
{
|
||||
switch (input[idx])
|
||||
{
|
||||
|
@ -269,14 +268,11 @@ void XmlParser::OnTagClose()
|
|||
}
|
||||
else if(mDocumentState == DS::Close_Element)
|
||||
{
|
||||
if(mWorkingElement->GetTagName() == mWorkingTagName)
|
||||
mDocumentState = DS::Await_Element;
|
||||
mLineState = LS::Await_Tag_Open;
|
||||
if (!mWorkingElements.empty())
|
||||
{
|
||||
mDocumentState = DS::Await_Element;
|
||||
mLineState = LS::Await_Tag_Open;
|
||||
if(mParentElement)
|
||||
{
|
||||
mWorkingElement = mParentElement;
|
||||
}
|
||||
mWorkingElements.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -289,7 +285,7 @@ void XmlParser::OnTextStart(char c)
|
|||
|
||||
void XmlParser::OnTextEnd()
|
||||
{
|
||||
mWorkingElement->SetText(mWorkingText);
|
||||
mWorkingElements.top()->SetText(mWorkingText);
|
||||
}
|
||||
|
||||
void XmlParser::onElementTagEnd()
|
||||
|
@ -318,14 +314,17 @@ void XmlParser::OnTagNameEnd()
|
|||
else if(mDocumentState == DS::Build_Element)
|
||||
{
|
||||
auto new_element = XmlElement::Create(mWorkingTagName);
|
||||
auto working_element = new_element.get();
|
||||
|
||||
mParentElement = mWorkingElement;
|
||||
mWorkingElement = new_element.get();
|
||||
|
||||
if(!mDocument->GetRoot())
|
||||
if (!mDocument->GetRoot())
|
||||
{
|
||||
mDocument->SetRoot(std::move(new_element));
|
||||
}
|
||||
else
|
||||
{
|
||||
mWorkingElements.top()->AddChild(std::move(new_element));
|
||||
}
|
||||
mWorkingElements.push(working_element);
|
||||
mLineState = LS::Await_Attribute_Name;
|
||||
}
|
||||
}
|
||||
|
@ -345,7 +344,7 @@ void XmlParser::OnAttributeNameEnd()
|
|||
}
|
||||
else if(mDocumentState == DS::Build_Element)
|
||||
{
|
||||
mWorkingElement->AddAttribute(std::move(attribute));
|
||||
mWorkingElements.top()->AddAttribute(std::move(attribute));
|
||||
}
|
||||
mLineState = LS::Await_Attribute_Value;
|
||||
}
|
||||
|
@ -358,7 +357,14 @@ void XmlParser::OnAttributeValueStart()
|
|||
|
||||
void XmlParser::OnAttributeValueEnd()
|
||||
{
|
||||
mWorkingElement->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
|
||||
if(mDocumentState == DS::Build_Prolog)
|
||||
{
|
||||
mDocument->GetProlog()->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
|
||||
}
|
||||
else if(mDocumentState == DS::Build_Element)
|
||||
{
|
||||
mWorkingElements.top()->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
|
||||
}
|
||||
mLineState = LS::Await_Attribute_Name;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <stack>
|
||||
#include "XmlDocument.h"
|
||||
|
||||
class XmlParser
|
||||
|
@ -33,8 +34,7 @@ private:
|
|||
DocumentState mDocumentState;
|
||||
LineState mLineState;
|
||||
XmlDocumentPtr mDocument;
|
||||
XmlElement* mParentElement;
|
||||
XmlElement* mWorkingElement;
|
||||
std::stack<XmlElement*> mWorkingElements;
|
||||
std::string mWorkingAttributeName;
|
||||
std::string mWorkingTagName;
|
||||
std::string mWorkingAttributeValue;
|
||||
|
|
|
@ -1,6 +1,68 @@
|
|||
#include "XmlWriter.h"
|
||||
|
||||
void XmlWriter::Write(File* file, XmlDocument* document)
|
||||
{
|
||||
|
||||
std::string XmlWriter::ToString(XmlElement* element, unsigned depth)
|
||||
{
|
||||
const auto prefix = std::string(2*depth, ' ');
|
||||
|
||||
auto content = prefix + "<" + element->GetTagName();
|
||||
for (std::size_t idx=0; idx< element->GetNumAttributes(); idx++)
|
||||
{
|
||||
auto attribute = element->GetAttribute(idx);
|
||||
content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\"";
|
||||
}
|
||||
|
||||
const auto num_children = element->GetNumChildren();
|
||||
if (num_children == 0 && element->GetText().empty())
|
||||
{
|
||||
content += "/>\n";
|
||||
return content;
|
||||
}
|
||||
else
|
||||
{
|
||||
content += ">";
|
||||
}
|
||||
|
||||
if (!element->GetText().empty())
|
||||
{
|
||||
content += element->GetText();
|
||||
}
|
||||
|
||||
if (num_children>0)
|
||||
{
|
||||
content += "\n";
|
||||
}
|
||||
for (std::size_t idx=0; idx< element->GetNumChildren(); idx++)
|
||||
{
|
||||
auto child = element->GetChild(idx);
|
||||
content += ToString(child, depth+1);
|
||||
}
|
||||
if (num_children>0)
|
||||
{
|
||||
content += prefix;
|
||||
}
|
||||
|
||||
content += "</" + element->GetTagName() + ">\n";
|
||||
return content;
|
||||
}
|
||||
|
||||
std::string XmlWriter::ToString(XmlDocument* document)
|
||||
{
|
||||
std::string content;
|
||||
if (auto prolog = document->GetProlog())
|
||||
{
|
||||
content += "<?xml";
|
||||
for (std::size_t idx=0; idx< prolog->GetNumAttributes(); idx++)
|
||||
{
|
||||
auto attribute = prolog->GetAttribute(idx);
|
||||
content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\"";
|
||||
}
|
||||
content += "?>\n";
|
||||
}
|
||||
|
||||
if (auto root = document->GetRoot())
|
||||
{
|
||||
content += ToString(root);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include "File.h"
|
||||
#include "XmlDocument.h"
|
||||
#include <string>
|
||||
|
||||
class XmlWriter
|
||||
{
|
||||
public:
|
||||
XmlWriter() = default;
|
||||
|
||||
void Write(File* file, XmlDocument* document);
|
||||
std::string ToString(XmlDocument* document);
|
||||
|
||||
private:
|
||||
std::string ToString(XmlElement* element, unsigned depth=0);
|
||||
};
|
||||
|
|
|
@ -68,3 +68,13 @@ std::size_t XmlElement::GetNumAttributes() const
|
|||
{
|
||||
return mAttributes.size();
|
||||
}
|
||||
|
||||
std::size_t XmlElement::GetNumChildren() const
|
||||
{
|
||||
return mChildren.size();
|
||||
}
|
||||
|
||||
XmlElement* XmlElement::GetChild(std::size_t index) const
|
||||
{
|
||||
return mChildren[index].get();
|
||||
}
|
||||
|
|
|
@ -1,18 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "XmlAttribute.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "xml-elements/XmlAttribute.h"
|
||||
|
||||
class XmlElement
|
||||
{
|
||||
protected:
|
||||
std::string mTagName;
|
||||
std::vector<XmlAttributeUPtr> mAttributes;
|
||||
std::vector<std::unique_ptr<XmlElement> > mChildren;
|
||||
std::string mText;
|
||||
|
||||
public:
|
||||
XmlElement(const std::string& tagName);
|
||||
virtual ~XmlElement() = default;
|
||||
|
@ -28,8 +23,17 @@ public:
|
|||
XmlAttribute* GetAttribute(std::size_t index) const;
|
||||
std::size_t GetNumAttributes() const;
|
||||
|
||||
std::size_t GetNumChildren() const;
|
||||
XmlElement* GetChild(std::size_t index) const;
|
||||
|
||||
void SetText(const std::string& text);
|
||||
void SetTagName(const std::string& tagName);
|
||||
|
||||
protected:
|
||||
std::string mTagName;
|
||||
std::vector<XmlAttributeUPtr> mAttributes;
|
||||
std::vector<std::unique_ptr<XmlElement> > mChildren;
|
||||
std::string mText;
|
||||
};
|
||||
|
||||
using XmlElementPtr = std::shared_ptr<XmlElement>;
|
||||
|
|
|
@ -2,12 +2,11 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "xml-elements/XmlElement.h"
|
||||
#include "XmlElement.h"
|
||||
|
||||
class XmlProlog : public XmlElement
|
||||
{
|
||||
public:
|
||||
|
||||
enum class Version{
|
||||
V1_0
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue