Some markdown processing.

This commit is contained in:
James Grogan 2022-12-01 17:13:54 +00:00
parent 31b479e9f6
commit ec11529b9a
23 changed files with 677 additions and 135 deletions

View file

@ -3,51 +3,6 @@
#include "XmlDocument.h"
#include "XmlAttribute.h"
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;
@ -64,7 +19,7 @@ std::string XmlWriter::toString(XmlDocument* document)
if (auto root = document->getRoot())
{
content += toString(root);
content += root->toString();
}
return content;
}

View file

@ -11,7 +11,4 @@ public:
XmlWriter() = default;
std::string toString(XmlDocument* document);
private:
std::string toString(XmlElement* element, unsigned depth=0);
};

View file

@ -49,6 +49,19 @@ void XmlElement::setText(const std::string& text)
mText = text;
}
XmlElement* XmlElement::getFirstChildWithTagName(const std::string& tag)
{
for(auto& child : mChildren)
{
if (child->getTagName() == tag)
{
return child.get();
}
}
return nullptr;
}
XmlAttribute* XmlElement::getAttribute(const std::string& attributeName) const
{
for(const auto& attribute : mAttributes)
@ -84,3 +97,48 @@ XmlElement* XmlElement::getChild(std::size_t index) const
{
return mChildren[index].get();
}
std::string XmlElement::toString(unsigned depth) const
{
const auto prefix = std::string(2*depth, ' ');
auto content = prefix + "<" + getTagName();
for (std::size_t idx=0; idx< getNumAttributes(); idx++)
{
auto attribute = getAttribute(idx);
content += " " + attribute->getName() + "=\"" + attribute->getValue() + "\"";
}
const auto num_children = getNumChildren();
if (num_children == 0 && getText().empty())
{
content += "/>\n";
return content;
}
else
{
content += ">";
}
if (!getText().empty())
{
content += getText();
}
if (num_children>0)
{
content += "\n";
}
for (std::size_t idx=0; idx< getNumChildren(); idx++)
{
auto child = getChild(idx);
content += child->toString(depth+1);
}
if (num_children>0)
{
content += prefix;
}
content += "</" + getTagName() + ">\n";
return content;
}

View file

@ -29,9 +29,13 @@ public:
std::size_t getNumChildren() const;
XmlElement* getChild(std::size_t index) const;
XmlElement* getFirstChildWithTagName(const std::string& tag);
void setText(const std::string& text);
void setTagName(const std::string& tagName);
virtual std::string toString(unsigned depth = 0) const;
protected:
std::string mTagName;
std::string mText;