Once we have built our node tree we need some way to parse it in order to produce a pretty html formatted page. When I was developing this it took way to much thinking time for the number of lines of code, but there are literally 2 functions that we use, and out of them, only one is called externally (the process_nodes function).
The first function is used to look for the first open sibling. If it finds one then it returns a pointer to that node, if not then it returns nullptr
//**************************************************************//
// ARE ALL DIRECT DECENDENTS OF THE CURRENT NODE CLOSED? //
//**************************************************************//
html_tree *pretty_html::find_first_open_sibling(html_tree *parent) {
html_tree *workNode = nullptr;
for(html_tree *it : parent->childNodes) {
if(it->closed() != html_tree::status::CLOSED) {
workNode = it;
break;
}
}
return(workNode);
}
And last but not least, the process node function, this is the function that actually creates the html page.
//**************************************************************//
// ARE ALL DIRECT DECENDENTS OF THE CURRENT NODE CLOSED? //
//**************************************************************//
void pretty_html::process_nodes(html_tree *primaryNode) {
html_tree *curNode = primaryNode;
unsigned tabs =0;
while(primaryNode->closed() != html_tree::status::CLOSED) {
if(curNode->closed() == html_tree::status::UNPROCESSED) {
curNode->open_node(tabs);
++tabs;
}
//if the current node is a leaf node and it is open then close it
if((curNode->is_leaf_node()) && (curNode->closed() == html_tree::status::OPEN)) {
--tabs;
curNode->close_node(tabs);
}
//NEXT NODE
//find the next node to work on
//first of all we need to see if the node has any open children
html_tree *sibling = find_first_open_sibling(curNode);
//if the node does have children then we need to drill down to the first unclosed child
if(sibling){
curNode = sibling;
}
//if it has no children then it can be closed regardless of whether it is a leaf node
//or not.
else{
//if the current node has no open children then close it
if(curNode->closed() == html_tree::status::OPEN) {
--tabs;
curNode->close_node(tabs);
}
//we can then make our way back up to the parent node
curNode = curNode->previousBranch;
}
}
}
How this function works is explained in the comments, once called the string passed to the primary iteration of the html_tree class will contain a valid html page (with any luck).

Recent Comments