From 31b479e9f6edab0ea4b2a935400ee95a41a1e3c7 Mon Sep 17 00:00:00 2001 From: James Grogan Date: Thu, 1 Dec 2022 11:49:57 +0000 Subject: [PATCH] Small html cleanup --- src/console/CMakeLists.txt | 2 +- src/publishing/CMakeLists.txt | 3 +- src/{web => publishing}/DocumentConverter.cpp | 2 +- src/{web => publishing}/DocumentConverter.h | 0 src/web/CMakeLists.txt | 2 +- src/web/html/HtmlDocument.cpp | 13 +- src/web/html/HtmlDocument.h | 10 +- src/web/html/HtmlWriter.cpp | 39 +++--- src/web/html/HtmlWriter.h | 10 +- src/web/markdown/MarkdownParser.cpp | 2 +- src/web/xml/XmlDocument.cpp | 20 ++- src/web/xml/XmlDocument.h | 27 ++-- src/web/xml/XmlParser.cpp | 130 +++++++++--------- src/web/xml/XmlParser.h | 79 ++++++----- src/web/xml/XmlWriter.cpp | 42 +++--- src/web/xml/XmlWriter.h | 8 +- src/web/xml/xml-elements/XmlAttribute.cpp | 17 ++- src/web/xml/xml-elements/XmlAttribute.h | 19 ++- src/web/xml/xml-elements/XmlElement.cpp | 32 +++-- src/web/xml/xml-elements/XmlElement.h | 37 ++--- src/web/xml/xml-elements/XmlProlog.cpp | 21 +-- src/web/xml/xml-elements/XmlProlog.h | 27 ++-- test/data/sample_markdown.md | 12 +- test/publishing/CMakeLists.txt | 1 + test/publishing/TestDocumentConverter.cpp | 19 +++ test/web/TestMarkdownParser.cpp | 2 +- test/web/TestXmlParser.cpp | 6 +- 27 files changed, 330 insertions(+), 252 deletions(-) rename src/{web => publishing}/DocumentConverter.cpp (95%) rename src/{web => publishing}/DocumentConverter.h (100%) create mode 100644 test/publishing/TestDocumentConverter.cpp diff --git a/src/console/CMakeLists.txt b/src/console/CMakeLists.txt index db7ec96..5b1179c 100644 --- a/src/console/CMakeLists.txt +++ b/src/console/CMakeLists.txt @@ -8,5 +8,5 @@ target_include_directories(console PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" ) set_property(TARGET console PROPERTY FOLDER src) -target_link_libraries(console PUBLIC core audio network database web graphics) +target_link_libraries(console PUBLIC core audio network database web graphics publishing) set_target_properties( console PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) \ No newline at end of file diff --git a/src/publishing/CMakeLists.txt b/src/publishing/CMakeLists.txt index a962bab..d0d8109 100644 --- a/src/publishing/CMakeLists.txt +++ b/src/publishing/CMakeLists.txt @@ -22,6 +22,7 @@ list(APPEND publishing_LIB_INCLUDES pdf/PdfStream.cpp pdf/PdfXRefTable.cpp pdf/PdfWriter.cpp + DocumentConverter.cpp ) add_library(publishing SHARED ${publishing_LIB_INCLUDES} ${publishing_INCLUDES} ${publishing_HEADERS}) @@ -31,6 +32,6 @@ target_include_directories(publishing PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/pdf" ) set_target_properties( publishing PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) -target_link_libraries( publishing PUBLIC core) +target_link_libraries( publishing PUBLIC core web) set_property(TARGET publishing PROPERTY FOLDER src) \ No newline at end of file diff --git a/src/web/DocumentConverter.cpp b/src/publishing/DocumentConverter.cpp similarity index 95% rename from src/web/DocumentConverter.cpp rename to src/publishing/DocumentConverter.cpp index 43a79ae..4f28b4e 100644 --- a/src/web/DocumentConverter.cpp +++ b/src/publishing/DocumentConverter.cpp @@ -67,7 +67,7 @@ void DocumentConverter::markdownToHtml(File* input, File* output) auto html_document = parser.getHtml(); HtmlWriter writer; - std::string html_string = writer.ToString(html_document); + std::string html_string = writer.toString(html_document.get()); output->open(File::AccessMode::Write); *(output->getOutHandle()) << html_string; diff --git a/src/web/DocumentConverter.h b/src/publishing/DocumentConverter.h similarity index 100% rename from src/web/DocumentConverter.h rename to src/publishing/DocumentConverter.h diff --git a/src/web/CMakeLists.txt b/src/web/CMakeLists.txt index 1e30881..2794cd8 100644 --- a/src/web/CMakeLists.txt +++ b/src/web/CMakeLists.txt @@ -17,7 +17,7 @@ list(APPEND web_LIB_INCLUDES html/HtmlElement.cpp html/elements/HtmlHeadElement.cpp html/elements/HtmlBodyElement.cpp - DocumentConverter.cpp) +) # add the executable add_library(web SHARED ${web_LIB_INCLUDES}) diff --git a/src/web/html/HtmlDocument.cpp b/src/web/html/HtmlDocument.cpp index a5ccc1c..77afca5 100644 --- a/src/web/html/HtmlDocument.cpp +++ b/src/web/html/HtmlDocument.cpp @@ -3,20 +3,23 @@ #include "HtmlHeadElement.h" #include "HtmlBodyElement.h" +#include + HtmlDocument::HtmlDocument() : XmlDocument() { auto root = XmlElement::Create("html"); - SetRoot(std::move(root)); + + setRoot(std::move(root)); auto header = std::make_unique(); - GetRoot()->AddChild(std::move(header)); + getRoot()->addChild(std::move(header)); auto body = std::make_unique(); - GetRoot()->AddChild(std::move(body)); + getRoot()->addChild(std::move(body)); } -std::shared_ptr HtmlDocument::Create() +std::unique_ptr HtmlDocument::Create() { - return std::make_shared(); + return std::make_unique(); } diff --git a/src/web/html/HtmlDocument.h b/src/web/html/HtmlDocument.h index 60b4ddc..c0126c6 100644 --- a/src/web/html/HtmlDocument.h +++ b/src/web/html/HtmlDocument.h @@ -1,15 +1,17 @@ #pragma once -#include + #include "XmlDocument.h" +#include + class HtmlDocument : public XmlDocument { public: - HtmlDocument(); - static std::shared_ptr Create(); + virtual ~HtmlDocument() = default; + static std::unique_ptr Create(); }; -using HtmlDocumentPtr = std::shared_ptr; +using HtmlDocumentPtr = std::unique_ptr; diff --git a/src/web/html/HtmlWriter.cpp b/src/web/html/HtmlWriter.cpp index 2bca4a6..d1301bf 100644 --- a/src/web/html/HtmlWriter.cpp +++ b/src/web/html/HtmlWriter.cpp @@ -1,23 +1,29 @@ #include "HtmlWriter.h" +#include "HtmlDocument.h" +#include "XmlElement.h" +#include "XmlAttribute.h" + +#include + HtmlWriter::HtmlWriter() { } -std::string HtmlWriter::ToString(XmlElement* element, unsigned depth) +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 content = prefix + "<" + element->getTagName(); + for (std::size_t idx=0; idx< element->getNumAttributes(); idx++) { - auto attribute = element->GetAttribute(idx); - content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\""; + auto attribute = element->getAttribute(idx); + content += " " + attribute->getName() + "=\"" + attribute->getValue() + "\""; } - const auto num_children = element->GetNumChildren(); - if (num_children == 0 && element->GetText().empty()) + const auto num_children = element->getNumChildren(); + if (num_children == 0 && element->getText().empty()) { content += "/>\n"; return content; @@ -27,36 +33,35 @@ std::string HtmlWriter::ToString(XmlElement* element, unsigned depth) content += ">"; } - if (!element->GetText().empty()) + if (!element->getText().empty()) { - content += element->GetText(); + content += element->getText(); } if (num_children>0) { content += "\n"; } - for (std::size_t idx=0; idx< element->GetNumChildren(); idx++) + for (std::size_t idx=0; idx< element->getNumChildren(); idx++) { - auto child = element->GetChild(idx); - content += ToString(child, depth+1); + auto child = element->getChild(idx); + content += toString(child, depth+1); } if (num_children>0) { content += prefix; } - content += "GetTagName() + ">\n"; + content += "getTagName() + ">\n"; return content; } -std::string HtmlWriter::ToString(HtmlDocumentPtr document) +std::string HtmlWriter::toString(HtmlDocument* document) { std::string content = "\n"; - - if (auto root = document->GetRoot()) + if (auto root = document->getRoot()) { - content += ToString(root); + content += toString(root); } return content; } diff --git a/src/web/html/HtmlWriter.h b/src/web/html/HtmlWriter.h index 4ef2cff..7083780 100644 --- a/src/web/html/HtmlWriter.h +++ b/src/web/html/HtmlWriter.h @@ -1,15 +1,17 @@ #pragma once -#include "HtmlDocument.h" +#include + +class HtmlDocument; +class XmlElement; class HtmlWriter { public: - HtmlWriter(); - std::string ToString(HtmlDocumentPtr document); + std::string toString(HtmlDocument* document); private: - std::string ToString(XmlElement* element, unsigned depth=0); + std::string toString(XmlElement* element, unsigned depth=0); }; diff --git a/src/web/markdown/MarkdownParser.cpp b/src/web/markdown/MarkdownParser.cpp index 736ac40..64b6ed5 100644 --- a/src/web/markdown/MarkdownParser.cpp +++ b/src/web/markdown/MarkdownParser.cpp @@ -26,5 +26,5 @@ void MarkdownParser::run(const std::string& content) HtmlDocumentPtr MarkdownParser::getHtml() { - return mHtmlDocument; + return std::move(mHtmlDocument); } diff --git a/src/web/xml/XmlDocument.cpp b/src/web/xml/XmlDocument.cpp index 2150db4..f2bf626 100644 --- a/src/web/xml/XmlDocument.cpp +++ b/src/web/xml/XmlDocument.cpp @@ -1,32 +1,42 @@ #include "XmlDocument.h" +#include "XmlProlog.h" +#include "XmlElement.h" + +#include + XmlDocument::XmlDocument() :mProlog(XmlProlog::Create("xml")) { } -XmlDocumentUPtr XmlDocument::Create() +XmlDocument::~XmlDocument() +{ + +} + +XmlDocumentPtr XmlDocument::Create() { return std::make_unique(); } -void XmlDocument::SetProlog(XmlPrologUPtr prolog) +void XmlDocument::setProlog(XmlPrologPtr prolog) { mProlog = std::move(prolog); } -XmlProlog* XmlDocument::GetProlog() const +XmlProlog* XmlDocument::getProlog() const { return mProlog.get(); } -void XmlDocument::SetRoot(XmlElementUPtr root) +void XmlDocument::setRoot(XmlElementPtr root) { mRoot = std::move(root); } -XmlElement* XmlDocument::GetRoot() const +XmlElement* XmlDocument::getRoot() const { return mRoot.get(); } diff --git a/src/web/xml/XmlDocument.h b/src/web/xml/XmlDocument.h index 5a4aee2..049b0ef 100644 --- a/src/web/xml/XmlDocument.h +++ b/src/web/xml/XmlDocument.h @@ -2,25 +2,30 @@ #include #include -#include "xml-elements/XmlElement.h" -#include "xml-elements/XmlProlog.h" +#include "XmlElement.h" +#include "XmlProlog.h" + +class XmlElement; +class XmlProlog; +using XmlPrologPtr = std::unique_ptr; +using XmlElementPtr = std::unique_ptr; class XmlDocument { - XmlPrologUPtr mProlog; - XmlElementUPtr mRoot; - public: XmlDocument(); + virtual ~XmlDocument(); static std::unique_ptr Create(); - XmlProlog* GetProlog() const; - XmlElement* GetRoot() const; + XmlProlog* getProlog() const; + XmlElement* getRoot() const; - void SetProlog(XmlPrologUPtr prolog); - void SetRoot(XmlElementUPtr root); + void setProlog(XmlPrologPtr prolog); + void setRoot(XmlElementPtr root); +private: + XmlPrologPtr mProlog; + XmlElementPtr mRoot; }; -using XmlDocumentPtr = std::shared_ptr; -using XmlDocumentUPtr = std::unique_ptr; +using XmlDocumentPtr = std::unique_ptr; diff --git a/src/web/xml/XmlParser.cpp b/src/web/xml/XmlParser.cpp index bc022e6..32fad1c 100644 --- a/src/web/xml/XmlParser.cpp +++ b/src/web/xml/XmlParser.cpp @@ -1,5 +1,11 @@ #include "XmlParser.h" + #include "StringUtils.h" + +#include "XmlDocument.h" +#include "XmlElement.h" +#include "XmlAttribute.h" + #include using LS = XmlParser::LineState; @@ -14,64 +20,64 @@ XmlParser::XmlParser() } -void XmlParser::ProcessLine(const std::string& input) +void XmlParser::processLine(const std::string& input) { for (std::size_t idx=0; idxSetText(mWorkingText); + mWorkingElements.top()->setText(mWorkingText); } void XmlParser::onElementTagEnd() @@ -294,7 +300,7 @@ void XmlParser::onElementTagEnd() mLineState = LS::Await_Tag_Open; } -void XmlParser::OnTagNameStart(char c) +void XmlParser::onTagNameStart(char c) { mWorkingTagName = c; mLineState = LS::Await_Tag_Name_End; @@ -304,11 +310,11 @@ void XmlParser::OnTagNameStart(char c) } } -void XmlParser::OnTagNameEnd() +void XmlParser::onTagNameEnd() { if(mDocumentState == DS::Build_Prolog) { - mDocument->GetProlog()->SetTagName(mWorkingTagName); + mDocument->getProlog()->setTagName(mWorkingTagName); mLineState = LS::Await_Attribute_Name; } else if(mDocumentState == DS::Build_Element) @@ -316,59 +322,59 @@ void XmlParser::OnTagNameEnd() auto new_element = XmlElement::Create(mWorkingTagName); auto working_element = new_element.get(); - if (!mDocument->GetRoot()) + if (!mDocument->getRoot()) { - mDocument->SetRoot(std::move(new_element)); + mDocument->setRoot(std::move(new_element)); } else { - mWorkingElements.top()->AddChild(std::move(new_element)); + mWorkingElements.top()->addChild(std::move(new_element)); } mWorkingElements.push(working_element); mLineState = LS::Await_Attribute_Name; } } -void XmlParser::OnAttributeNameStart(char c) +void XmlParser::onAttributeNameStart(char c) { mWorkingAttributeName = c; mLineState = LS::Await_Attribute_Name_End; } -void XmlParser::OnAttributeNameEnd() +void XmlParser::onAttributeNameEnd() { auto attribute = XmlAttribute::Create(mWorkingAttributeName); if(mDocumentState == DS::Build_Prolog) { - mDocument->GetProlog()->AddAttribute(std::move(attribute)); + mDocument->getProlog()->addAttribute(std::move(attribute)); } else if(mDocumentState == DS::Build_Element) { - mWorkingElements.top()->AddAttribute(std::move(attribute)); + mWorkingElements.top()->addAttribute(std::move(attribute)); } mLineState = LS::Await_Attribute_Value; } -void XmlParser::OnAttributeValueStart() +void XmlParser::onAttributeValueStart() { mWorkingAttributeValue = ""; mLineState = LS::Await_Attribute_Value_End; } -void XmlParser::OnAttributeValueEnd() +void XmlParser::onAttributeValueEnd() { if(mDocumentState == DS::Build_Prolog) { - mDocument->GetProlog()->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue); + mDocument->getProlog()->getAttribute(mWorkingAttributeName)->setValue(mWorkingAttributeValue); } else if(mDocumentState == DS::Build_Element) { - mWorkingElements.top()->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue); + mWorkingElements.top()->getAttribute(mWorkingAttributeName)->setValue(mWorkingAttributeValue); } mLineState = LS::Await_Attribute_Name; } -void XmlParser::OnStartProlog() +void XmlParser::onStartProlog() { mDocumentState = DS::Build_Prolog; mLineState = LS::Await_Tag_Name_End; @@ -376,12 +382,12 @@ void XmlParser::OnStartProlog() void XmlParser::onFinishProlog() { - mDocument->GetProlog()->Update(); + mDocument->getProlog()->update(); mDocumentState = DS::Await_Element; mLineState = LS::Await_Tag_Open; } -XmlDocumentPtr XmlParser::GetDocument() const +XmlDocumentPtr XmlParser::getDocument() { - return mDocument; + return std::move(mDocument); } diff --git a/src/web/xml/XmlParser.h b/src/web/xml/XmlParser.h index d50ab91..f152aeb 100644 --- a/src/web/xml/XmlParser.h +++ b/src/web/xml/XmlParser.h @@ -2,7 +2,12 @@ #include #include -#include "XmlDocument.h" +#include + +class XmlDocument; +using XmlDocumentPtr = std::unique_ptr; + +class XmlElement; class XmlParser { @@ -29,72 +34,70 @@ public: Await_Text_End }; -private: - - DocumentState mDocumentState; - LineState mLineState; - XmlDocumentPtr mDocument; - std::stack mWorkingElements; - std::string mWorkingAttributeName; - std::string mWorkingTagName; - std::string mWorkingAttributeValue; - std::string mWorkingText; - public: - XmlParser(); - void ProcessLine(const std::string& input); + void processLine(const std::string& input); - XmlDocumentPtr GetDocument() const; + XmlDocumentPtr getDocument(); private: + void onLeftBracket(); - void OnLeftBracket(); + void onRightBracket(); - void OnRightBracket(); + void onQuestionMark(); - void OnQuestionMark(); + void onForwardSlash(); - void OnForwardSlash(); + void onChar(char c); - void OnChar(char c); + void onSpace(char c); - void OnSpace(char c); + void onAlphaNumeric(char c); - void OnAlphaNumeric(char c); + void onNonAlphaNumeric(char c); - void OnNonAlphaNumeric(char c); + void onEquals(); - void OnEquals(); + void onDoubleQuote(); - void OnDoubleQuote(); + void onTagOpen(); - void OnTagOpen(); + void onTagNameStart(char c); - void OnTagNameStart(char c); + void onTagNameEnd(); - void OnTagNameEnd(); + void onTagClose(); - void OnTagClose(); + void onTextStart(char c); - void OnTextStart(char c); + void onTextEnd(); - void OnTextEnd(); + void onAttributeNameStart(char c); - void OnAttributeNameStart(char c); + void onAttributeNameEnd(); - void OnAttributeNameEnd(); + void onAttributeValueStart(); - void OnAttributeValueStart(); + void onAttributeValueEnd(); - void OnAttributeValueEnd(); + void onPrologId(); - void OnPrologId(); - - void OnStartProlog(); + void onStartProlog(); void onFinishProlog(); void onElementTagEnd(); + +private: + DocumentState mDocumentState; + LineState mLineState; + XmlDocumentPtr mDocument; + std::stack mWorkingElements; + + std::string mWorkingAttributeName; + std::string mWorkingTagName; + std::string mWorkingAttributeValue; + std::string mWorkingText; }; diff --git a/src/web/xml/XmlWriter.cpp b/src/web/xml/XmlWriter.cpp index 790ef9f..0e5dcbc 100644 --- a/src/web/xml/XmlWriter.cpp +++ b/src/web/xml/XmlWriter.cpp @@ -1,19 +1,21 @@ #include "XmlWriter.h" +#include "XmlDocument.h" +#include "XmlAttribute.h" -std::string XmlWriter::ToString(XmlElement* element, unsigned depth) +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 content = prefix + "<" + element->getTagName(); + for (std::size_t idx=0; idx< element->getNumAttributes(); idx++) { - auto attribute = element->GetAttribute(idx); - content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\""; + auto attribute = element->getAttribute(idx); + content += " " + attribute->getName() + "=\"" + attribute->getValue() + "\""; } - const auto num_children = element->GetNumChildren(); - if (num_children == 0 && element->GetText().empty()) + const auto num_children = element->getNumChildren(); + if (num_children == 0 && element->getText().empty()) { content += "/>\n"; return content; @@ -23,46 +25,46 @@ std::string XmlWriter::ToString(XmlElement* element, unsigned depth) content += ">"; } - if (!element->GetText().empty()) + if (!element->getText().empty()) { - content += element->GetText(); + content += element->getText(); } if (num_children>0) { content += "\n"; } - for (std::size_t idx=0; idx< element->GetNumChildren(); idx++) + for (std::size_t idx=0; idx< element->getNumChildren(); idx++) { - auto child = element->GetChild(idx); - content += ToString(child, depth+1); + auto child = element->getChild(idx); + content += toString(child, depth+1); } if (num_children>0) { content += prefix; } - content += "GetTagName() + ">\n"; + content += "getTagName() + ">\n"; return content; } -std::string XmlWriter::ToString(XmlDocument* document) +std::string XmlWriter::toString(XmlDocument* document) { std::string content; - if (auto prolog = document->GetProlog()) + if (auto prolog = document->getProlog()) { content += "GetNumAttributes(); idx++) + for (std::size_t idx=0; idx< prolog->getNumAttributes(); idx++) { - auto attribute = prolog->GetAttribute(idx); - content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\""; + auto attribute = prolog->getAttribute(idx); + content += " " + attribute->getName() + "=\"" + attribute->getValue() + "\""; } content += "?>\n"; } - if (auto root = document->GetRoot()) + if (auto root = document->getRoot()) { - content += ToString(root); + content += toString(root); } return content; } diff --git a/src/web/xml/XmlWriter.h b/src/web/xml/XmlWriter.h index 8af9dab..d48bfc5 100644 --- a/src/web/xml/XmlWriter.h +++ b/src/web/xml/XmlWriter.h @@ -1,15 +1,17 @@ #pragma once -#include "XmlDocument.h" #include +class XmlDocument; +class XmlElement; + class XmlWriter { public: XmlWriter() = default; - std::string ToString(XmlDocument* document); + std::string toString(XmlDocument* document); private: - std::string ToString(XmlElement* element, unsigned depth=0); + std::string toString(XmlElement* element, unsigned depth=0); }; diff --git a/src/web/xml/xml-elements/XmlAttribute.cpp b/src/web/xml/xml-elements/XmlAttribute.cpp index 19f662b..7c4142e 100644 --- a/src/web/xml/xml-elements/XmlAttribute.cpp +++ b/src/web/xml/xml-elements/XmlAttribute.cpp @@ -1,6 +1,5 @@ #include "XmlAttribute.h" - XmlAttribute::XmlAttribute(const std::string& name) : mName(name), mValue() @@ -8,22 +7,22 @@ XmlAttribute::XmlAttribute(const std::string& name) } -XmlAttributeUPtr XmlAttribute::Create(const std::string& name) +XmlAttributePtr XmlAttribute::Create(const std::string& name) { return std::make_unique(name); } -void XmlAttribute::SetValue(const std::string& value) -{ - mValue = value; -} - -std::string XmlAttribute::GetName() const +const std::string& XmlAttribute::getName() const { return mName; } -std::string XmlAttribute::GetValue() const +const std::string& XmlAttribute::getValue() const { return mValue; } + +void XmlAttribute::setValue(const std::string& value) +{ + mValue = value; +} diff --git a/src/web/xml/xml-elements/XmlAttribute.h b/src/web/xml/xml-elements/XmlAttribute.h index 551f7c7..302c5bf 100644 --- a/src/web/xml/xml-elements/XmlAttribute.h +++ b/src/web/xml/xml-elements/XmlAttribute.h @@ -1,26 +1,23 @@ #pragma once + #include -#include #include class XmlAttribute { -private: - std::string mName; - std::string mValue; - public: - XmlAttribute(const std::string& name); static std::unique_ptr Create(const std::string& name); - void SetValue(const std::string& value); + const std::string& getName() const; - std::string GetName() const; + const std::string& getValue() const; - std::string GetValue() const; + void setValue(const std::string& value); +private: + std::string mName; + std::string mValue; }; -using XmlAttributePtr = std::shared_ptr; -using XmlAttributeUPtr = std::unique_ptr; +using XmlAttributePtr = std::unique_ptr; diff --git a/src/web/xml/xml-elements/XmlElement.cpp b/src/web/xml/xml-elements/XmlElement.cpp index 02246b9..82d44c8 100644 --- a/src/web/xml/xml-elements/XmlElement.cpp +++ b/src/web/xml/xml-elements/XmlElement.cpp @@ -1,5 +1,6 @@ #include "XmlElement.h" +#include "XmlAttribute.h" XmlElement::XmlElement(const std::string& tagName) : mTagName(tagName), @@ -8,46 +9,51 @@ XmlElement::XmlElement(const std::string& tagName) } -XmlElementUPtr XmlElement::Create(const std::string& tagName) +XmlElement::~XmlElement() +{ + +} + +XmlElementPtr XmlElement::Create(const std::string& tagName) { return std::make_unique(tagName); } -void XmlElement::SetTagName(const std::string& tagName) +void XmlElement::setTagName(const std::string& tagName) { mTagName = tagName; } -void XmlElement::AddChild(XmlElementUPtr child) +void XmlElement::addChild(XmlElementPtr child) { mChildren.push_back(std::move(child)); } -void XmlElement::AddAttribute(XmlAttributeUPtr attribute) +void XmlElement::addAttribute(XmlAttributePtr attribute) { mAttributes.push_back(std::move(attribute)); } -std::string XmlElement::GetTagName() const +const std::string& XmlElement::getTagName() const { return mTagName; } -std::string XmlElement::GetText() const +const std::string& XmlElement::getText() const { return mText; } -void XmlElement::SetText(const std::string& text) +void XmlElement::setText(const std::string& text) { mText = text; } -XmlAttribute* XmlElement::GetAttribute(const std::string& attributeName) const +XmlAttribute* XmlElement::getAttribute(const std::string& attributeName) const { for(const auto& attribute : mAttributes) { - if(attribute->GetName() == attributeName) + if(attribute->getName() == attributeName) { return attribute.get(); } @@ -55,7 +61,7 @@ XmlAttribute* XmlElement::GetAttribute(const std::string& attributeName) const return nullptr; } -XmlAttribute* XmlElement::GetAttribute(std::size_t index) const +XmlAttribute* XmlElement::getAttribute(std::size_t index) const { if(index < mAttributes.size()) { @@ -64,17 +70,17 @@ XmlAttribute* XmlElement::GetAttribute(std::size_t index) const return nullptr; } -std::size_t XmlElement::GetNumAttributes() const +std::size_t XmlElement::getNumAttributes() const { return mAttributes.size(); } -std::size_t XmlElement::GetNumChildren() const +std::size_t XmlElement::getNumChildren() const { return mChildren.size(); } -XmlElement* XmlElement::GetChild(std::size_t index) const +XmlElement* XmlElement::getChild(std::size_t index) const { return mChildren[index].get(); } diff --git a/src/web/xml/xml-elements/XmlElement.h b/src/web/xml/xml-elements/XmlElement.h index cc30a9f..1254241 100644 --- a/src/web/xml/xml-elements/XmlElement.h +++ b/src/web/xml/xml-elements/XmlElement.h @@ -1,40 +1,43 @@ #pragma once -#include "XmlAttribute.h" + #include #include #include +class XmlAttribute; +using XmlAttributePtr = std::unique_ptr; + class XmlElement { public: XmlElement(const std::string& tagName); - virtual ~XmlElement() = default; + virtual ~XmlElement(); static std::unique_ptr Create(const std::string& tagName); - void AddAttribute(XmlAttributeUPtr attribute); - void AddChild(std::unique_ptr child); + void addAttribute(XmlAttributePtr attribute); + void addChild(std::unique_ptr child); - std::string GetTagName() const; - std::string GetText() const; - XmlAttribute* GetAttribute(const std::string& attribute) const; - XmlAttribute* GetAttribute(std::size_t index) const; - std::size_t GetNumAttributes() const; + const std::string& getTagName() const; + const std::string& getText() const; + XmlAttribute* getAttribute(const std::string& attribute) const; + XmlAttribute* getAttribute(std::size_t index) const; + std::size_t getNumAttributes() const; - std::size_t GetNumChildren() const; - XmlElement* GetChild(std::size_t index) 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); + void setText(const std::string& text); + void setTagName(const std::string& tagName); protected: std::string mTagName; - std::vector mAttributes; - std::vector > mChildren; std::string mText; + + std::vector mAttributes; + std::vector > mChildren; }; -using XmlElementPtr = std::shared_ptr; -using XmlElementUPtr = std::unique_ptr; +using XmlElementPtr = std::unique_ptr; diff --git a/src/web/xml/xml-elements/XmlProlog.cpp b/src/web/xml/xml-elements/XmlProlog.cpp index 1df0096..eb9f622 100644 --- a/src/web/xml/xml-elements/XmlProlog.cpp +++ b/src/web/xml/xml-elements/XmlProlog.cpp @@ -1,5 +1,6 @@ #include "XmlProlog.h" +#include "XmlAttribute.h" XmlProlog::XmlProlog(const std::string& tagName) : XmlElement(tagName), @@ -9,22 +10,22 @@ XmlProlog::XmlProlog(const std::string& tagName) } -XmlPrologUPtr XmlProlog::Create(const std::string& tagName) +XmlPrologPtr XmlProlog::Create(const std::string& tagName) { return std::make_unique(tagName); } -XmlProlog::Encoding XmlProlog::GetEncoding() const +XmlProlog::Encoding XmlProlog::getEncoding() const { return mEncoding; } -XmlProlog::Version XmlProlog::GetVersion() const +XmlProlog::Version XmlProlog::getVersion() const { return mVersion; } -void XmlProlog::SetEncoding(const std::string& encoding) +void XmlProlog::setEncoding(const std::string& encoding) { if(encoding == "UTF-8") { @@ -32,7 +33,7 @@ void XmlProlog::SetEncoding(const std::string& encoding) } } -void XmlProlog::SetVersion(const std::string& version) +void XmlProlog::setVersion(const std::string& version) { if(version == "1.0") { @@ -40,15 +41,15 @@ void XmlProlog::SetVersion(const std::string& version) } } -void XmlProlog::Update() +void XmlProlog::update() { - if(const auto version = GetAttribute("version")) + if(const auto version = getAttribute("version")) { - SetVersion(version->GetValue()); + setVersion(version->getValue()); } - if(const auto encoding = GetAttribute("encoding")) + if(const auto encoding = getAttribute("encoding")) { - SetEncoding(encoding->GetValue()); + setEncoding(encoding->getValue()); } } diff --git a/src/web/xml/xml-elements/XmlProlog.h b/src/web/xml/xml-elements/XmlProlog.h index d516e36..8a30288 100644 --- a/src/web/xml/xml-elements/XmlProlog.h +++ b/src/web/xml/xml-elements/XmlProlog.h @@ -1,9 +1,10 @@ #pragma once -#include -#include #include "XmlElement.h" +#include +#include + class XmlProlog : public XmlElement { public: @@ -15,23 +16,21 @@ public: UTF8 }; -private: - - Version mVersion; - Encoding mEncoding; - public: XmlProlog(const std::string& tagName); static std::unique_ptr Create(const std::string& tagName); - Encoding GetEncoding() const; - Version GetVersion() const; + Encoding getEncoding() const; + Version getVersion() const; - void SetEncoding(const std::string& encoding); - void SetVersion(const std::string& version); - void Update(); + void setEncoding(const std::string& encoding); + void setVersion(const std::string& version); + void update(); + +private: + Version mVersion; + Encoding mEncoding; }; -using XmlPrologPtr = std::shared_ptr; -using XmlPrologUPtr = std::unique_ptr; +using XmlPrologPtr = std::unique_ptr; diff --git a/test/data/sample_markdown.md b/test/data/sample_markdown.md index f9f5166..2decf41 100644 --- a/test/data/sample_markdown.md +++ b/test/data/sample_markdown.md @@ -17,4 +17,14 @@ I'm a bullet point list: * Second point * Third point -With a [hyperlink](www.imahyperlink.com) embedded. \ No newline at end of file +With a [hyperlink](www.imahyperlink.com) embedded. + +# I'm another level one header + +I'm some inline math $a = b + c$ and I'm some standalone math: + +$$ +d = e + f +$$ + +![This is an image](https://myoctocat.com/assets/images/base-octocat.svg) diff --git a/test/publishing/CMakeLists.txt b/test/publishing/CMakeLists.txt index ae1c301..dccf849 100644 --- a/test/publishing/CMakeLists.txt +++ b/test/publishing/CMakeLists.txt @@ -1,5 +1,6 @@ set(PUBLISHING_UNIT_TEST_FILES publishing/TestPdfWriter.cpp + publishing/TestDocumentConverter.cpp PARENT_SCOPE ) diff --git a/test/publishing/TestDocumentConverter.cpp b/test/publishing/TestDocumentConverter.cpp new file mode 100644 index 0000000..53164d4 --- /dev/null +++ b/test/publishing/TestDocumentConverter.cpp @@ -0,0 +1,19 @@ +#include "PdfDocument.h" +#include "PdfWriter.h" +#include "PdfObject.h" +#include "File.h" + +#include "TestFramework.h" +#include "TestUtils.h" + +TEST_CASE(TestDocumentConverterMarkdownToPdf, "publishing") +{ + auto document = std::make_unique(); + + PdfWriter writer; + const auto output = writer.ToString(document); + + File pdf_file(TestUtils::getTestOutputDir() / "TestDocumentConverterMarkdownToPdf.pdf"); + pdf_file.open(File::AccessMode::Write); + pdf_file.writeText(output); +} diff --git a/test/web/TestMarkdownParser.cpp b/test/web/TestMarkdownParser.cpp index 5d5a170..99b2215 100644 --- a/test/web/TestMarkdownParser.cpp +++ b/test/web/TestMarkdownParser.cpp @@ -16,7 +16,7 @@ TEST_CASE(TestMarkdownParser, "web") auto html = parser.getHtml(); HtmlWriter writer; - const auto html_string = writer.ToString(html); + const auto html_string = writer.toString(html.get()); File html_file(TestUtils::getTestOutputDir() / "TestMarkdownParserOut.html"); html_file.writeText(html_string); diff --git a/test/web/TestXmlParser.cpp b/test/web/TestXmlParser.cpp index a96041e..2473ec8 100644 --- a/test/web/TestXmlParser.cpp +++ b/test/web/TestXmlParser.cpp @@ -3,6 +3,8 @@ #include "XmlParser.h" #include "XmlWriter.h" +#include "XmlDocument.h" + #include "File.h" #include "TestFramework.h" @@ -18,12 +20,12 @@ TEST_CASE(TestXmlParser, "web") { std::string line; std::getline(xml_file, line); - parser.ProcessLine(line); + parser.processLine(line); } xml_file.close(); XmlWriter writer; - auto content = writer.ToString(parser.GetDocument().get()); + auto content = writer.toString(parser.getDocument().get()); auto outFile = std::make_unique(TestUtils::getTestOutputDir() / "test_out.xml"); outFile->writeText(content);