As a follow-up to my code's v0.1 revision, we now introduce V0.3, with capability for storage of attributes and a struct for each HTML tag. Just start the struct, call its functions, change its variables then use <fstream>
to make the complete HTML file using generateHTML()
!
#include <iostream>
#include <fstream>
#include <vector>
struct HtmlTag {
std::string tagname;
std::string tagcontent;
std::vector<std::string> tagattributes;
std::vector<std::string> tagattrcontent;
void addAttribute(std::string attrName, std::string attrCont) {
tagattributes.push_back(attrName);
tagattrcontent.push_back(attrCont);
}
int removeAttribute(int pos) {
tagattributes.erase(tagattributes.begin() + pos);
tagattrcontent.erase(tagattrcontent.begin() + pos);
return tagattributes.size();
}
std::string returnTag() {
std::string attributes = "";
if (tagattributes.size() != tagattrcontent.size()) {
return "ERR:UNMATCHINGLENGTH";
}
unsigned short i;
if(tagattributes.size() > 0 && tagattrcontent.size() > 0) {
for(i = 0; i < tagattributes.size(); i++) {
attributes = attributes + " " + tagattributes[i] + "=\"" + tagattrcontent[i] + "\"";
}
}
else {
return "<" + tagname + ">" + tagcontent + "</" + tagname + ">";
}
return "<" + tagname + attributes + ">" + tagcontent + "</" + tagname + ">";
}
};
std::string generateHTML(std::string doctype, std::string head, std::string body) {
return "<!DOCTYPE " + doctype + " />\n\n<html>\n <head>\n " + head + "\n </head>\n <body>\n " + body + "\n </body>\n</html>";
}
int main()
{
HtmlTag myHTML;
myHTML.addAttribute("spam", "eggs");
myHTML.tagcontent = "bar";
myHTML.tagname = "foo";
unsigned int i2;
for(i2 = 0; i2 < myHTML.tagattrcontent.size(); i2++) {
std::cout << myHTML.tagattrcontent[i2] << ":" << myHTML.tagattributes[i2] << std::endl;
}
std::cout << myHTML.returnTag() << std::endl;
std::cout << generateHTML("html", "<title>Hello World!</title>", myHTML.returnTag()) << std::endl;
}
libhtml
? \$\endgroup\$