I'm making a framework for building simple html websites for an embedded system and I want to make it bulletproof in a way that a user can't make mistakes in building the html document. As I've learned, it is hard to make and maintain an html markup inside c++ code. In one framework that I found (only one I could find) you could add any tag and add any attribute to one node. I want to control that. To do so I need to have a lot of data which I've gathered from w3schools.com. My next step would be putting the data into the code.
To explain briefly:
- I have a list of allowed tags
- Each tag supports a list of global attributes
- Each tag supports a list of event attributes
- Some of the tags have a tag-specific attributes
- Each attribute has a list of allowed values
The problem I'm having is how to properly connect those lists. I could put everything into enums
, but that would defeat the purpose of this framework. I could also have a class for each of the tags but would result in a lot of redundant code.
The end result I wish to have:
//tag temp(tag.type);
// OR
//tag.type temp;
tag.a temp;
//temp.addAttribute(attribute.type.value)
temp.addAttribute(att.href("http://www.w3schools.com"));
temp.addAttribute(att.target.blank);
temp.addAttribute(att.onclick("alert('You are goint away :(')"));
temp.value("Visit W3Schools!");
temp.ToString()
//document.addNode(temp);
//this will produce the following tag
//<a href="http://www.w3schools.com" target="_blank" onclick="alert('You are goint away :(')">Visit W3Schools!</a>
Any idea on how to structure the data to achieve this or something similarly?
const std::map
s from strings to lists of strings seems like an obvious and adequate way to store most of your "this tag allows these attributes" information (assuming you have C++11 to initialize them properly). Any reason you haven't considered that?enum class tag { div, a, h1}; map<tag, ?>
?