MarkdownToHtmlTranspiler/lib/structureNode.cpp
Hayden Hargreaves e2178604dd (FEAT): Block quotes implemented
However, an issue has been outlines, deeper recursion. Recursive
functions should by nature...recurse. Which they do not...
2025-11-10 22:16:12 -07:00

114 lines
2.2 KiB
C++

#include "structureNode.h"
#include <cstdio>
#include <sstream>
#include <stdexcept>
#include <string>
using std::string;
string DocumentNode::ToHtml() const {
std::stringstream ss;
ss << "<!DOCTYPE html>\n\t<html lang=\"en\">\n\t\t<head>\n\t\t\t<meta "
"charset=\"UTF-8\">\n\t\t\t<meta "
"name=\"viewport\" content=\"width=device-width, "
"initial-scale=1.0\">\n\t\t\t<title>Document</title>\n\t\t</"
"head>\n\t<body>\n";
for (const auto &child : this->GetChilren()) {
ss << *child;
}
ss << "\n\t</body>\n</html>";
return ss.str();
}
HeadingNode::HeadingNode(int size) {
if (1 <= size && size <= 6) {
this->size = size;
return;
}
throw std::runtime_error("HeadingNode.size must be between 1 and 6.");
}
string HeadingNode::ToHtml() const {
std::stringstream ss;
ss << "<h" << size << ">";
for (const auto &child : this->GetChilren()) {
ss << *child;
}
ss << "</h" << size << ">\n";
return ss.str();
}
string ParagraphNode::ToHtml() const {
std::stringstream ss;
ss << "<p>";
for (const auto &child : this->GetChilren()) {
ss << *child;
}
ss << "</p>\n";
return ss.str();
}
string ListNode::ToHtml() const {
std::stringstream ss;
ss << (this->ordered ? "<ol>" : "<ul>") << "\n";
for (const auto &child : this->GetChilren()) {
ss << *child;
}
ss << (this->ordered ? "</ol>" : "</ul>") << "\n";
return ss.str();
}
string ListElementNode::ToHtml() const {
std::stringstream ss;
ss << "<li>";
for (const auto &child : this->GetChilren()) {
ss << *child;
}
ss << "</li>" << "\n";
return ss.str();
}
string CodeBlockNode::ToHtml() const {
std::stringstream ss;
ss << "<code>\n";
for (const auto &child : this->GetChilren()) {
ss << *child << "\n";
}
ss << "</code>\n";
return ss.str();
}
string ImageNode::ToHtml() const {
std::stringstream ss;
ss << "<img src=\"" << this->src << "\" alt=\"" << this->alt << "\" />\n";
return ss.str();
}
string BlockQuoteNode::ToHtml() const {
std::stringstream ss;
ss << "<blockquote>";
for (const auto &child : this->GetChilren()) {
ss << *child;
}
ss << "</blockquote>\n";
return ss.str();
}