{"id":73,"date":"2019-06-01T19:47:09","date_gmt":"2019-06-01T18:47:09","guid":{"rendered":"https:\/\/blog.inplico.uk\/?p=73"},"modified":"2019-06-01T19:47:09","modified_gmt":"2019-06-01T18:47:09","slug":"formatted-html-pretty_html-pt-2","status":"publish","type":"post","link":"https:\/\/blog.inplico.uk\/?p=73","title":{"rendered":"Formatted html &#8211; pretty_html (pt.2)"},"content":{"rendered":"<p>Once we have built our node tree we need some way to parse it in order to produce a pretty html formatted page.\u00a0 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).<\/p>\n<p>The first function is used to look for the first open sibling.\u00a0 If it finds one then it returns a pointer to that node, if not then it returns nullptr<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/**************************************************************\/\/\r\n\/\/ ARE ALL DIRECT DECENDENTS OF THE CURRENT NODE CLOSED?        \/\/\r\n\/\/**************************************************************\/\/\r\nhtml_tree *pretty_html::find_first_open_sibling(html_tree *parent) {\r\n\r\n    html_tree *workNode = nullptr;\r\n\r\n    for(html_tree *it : parent-&gt;childNodes) {\r\n        if(it-&gt;closed() != html_tree::status::CLOSED) {\r\n            workNode = it;\r\n            break;\r\n        }\r\n    }\r\n\r\n    return(workNode);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>And last but not least, the process node function, this is the function that actually creates the html page.<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/**************************************************************\/\/\r\n\/\/ ARE ALL DIRECT DECENDENTS OF THE CURRENT NODE CLOSED?\t\/\/\r\n\/\/**************************************************************\/\/\r\nvoid pretty_html::process_nodes(html_tree *primaryNode) {\r\n\r\n    html_tree *curNode = primaryNode;\r\n    unsigned tabs =0;\r\n    while(primaryNode-&gt;closed() != html_tree::status::CLOSED) {\r\n\r\n        if(curNode-&gt;closed() == html_tree::status::UNPROCESSED) {\r\n            curNode-&gt;open_node(tabs);\r\n            ++tabs;\r\n        }\r\n\r\n        \/\/if the current node is a leaf node and it is open then close it\r\n        if((curNode-&gt;is_leaf_node()) &amp;&amp; (curNode-&gt;closed() == html_tree::status::OPEN)) {\r\n\r\n            --tabs;\r\n            curNode-&gt;close_node(tabs);\r\n\r\n        }\r\n\r\n        \/\/NEXT NODE\r\n        \/\/find the next node to work on\r\n        \/\/first of all we need to see if the node has any open children\r\n        html_tree *sibling = find_first_open_sibling(curNode);\r\n\r\n        \/\/if the node does have children then we need to drill down to the first unclosed child\r\n        if(sibling){\r\n            curNode = sibling;\r\n        }\r\n\r\n        \/\/if it has no children then it can be closed regardless of whether it is a leaf node\r\n        \/\/or not.\r\n        else{\r\n\r\n            \/\/if the current node has no open children then close it\r\n            if(curNode-&gt;closed() == html_tree::status::OPEN) {\r\n                --tabs;\r\n                curNode-&gt;close_node(tabs);\r\n            }\r\n\r\n            \/\/we can then make our way back up to the parent node\r\n            curNode = curNode-&gt;previousBranch;\r\n        }\r\n\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>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).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once we have built our node tree we need some way to parse it in order to produce a pretty html formatted page.\u00a0 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, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-cgi"],"_links":{"self":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":3,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}