1 #ifndef __BIAWEBDOCUMENTTREE__
2 #define __BIAWEBDOCUMENTTREE__
7 #include "biawebdocument.hpp"
8 #include "biawebstrings.hpp"
10 // to implement a document tree - both with or without subtrees
14 // the pointer to the parent tree if there is one or nullptr
17 std::list<DocumentTree> children;
20 // file stub of this tree
22 // list of documents in this tree
23 std::list<Document> docs;
24 // set the parent - protected function as this has to be
25 // called only by add_child
26 void set_parent (DocumentTree *parent) {
27 this->parent = parent;
31 // create new top level document tree
32 DocumentTree (std::string title, std::string stub = "") {
34 // if stub is not empty set it
37 // make the stub from the title
39 this->stub = convert_title (title);
40 this->parent = nullptr;
43 // create the document index for this tree
44 void create_index (std::string destdir);
47 void set_title (std::string title) {
51 this->stub = convert_title (title);
54 void set_stub (std::string stub) {
58 std::string get_title () {
62 std::string get_stub () {
66 // get the child level of this tree
67 unsigned int get_level ();
69 // get the stub hierarchy
70 std::string stub_hierarchy ();
72 // add a child tree to this tree
73 void add_child (DocumentTree *child) {
74 child->set_parent (this);
75 this->children.push_back (*child);
78 // add a document to this tree
79 void add_document (Document *doc) {
80 this->docs.push_back (*doc);
83 // print a visual representation of this tree with levels
84 void visualize_tree ();
86 // get a pointer to the parent of this tree
87 DocumentTree *get_parent () {
92 // get the tree level - 0 if top level
93 unsigned int DocumentTree::get_level () {
95 DocumentTree *par = this->get_parent ();
96 while (par != nullptr) {
98 par = par->get_parent ();
103 // get the stub hierarchy for this tree
104 std::string DocumentTree::stub_hierarchy () {
105 std::list<std::string> levels;
106 DocumentTree *par = this->get_parent();
107 while (par!= nullptr) {
108 levels.push_front (par->get_stub());
109 par = par->get_parent ();
111 std::string stub_str;
112 for (std::string level : levels) {
113 // if stub is empty, don't append a /
115 stub_str += level + "/";
120 // print the representation of this tree
121 void DocumentTree::visualize_tree () {
122 // print the tree level
123 std::cout << std::setw(3) << std::left << this->get_level ();
124 // indent as per the level
125 for (unsigned int i = 0; i < this->get_level(); i ++)
127 // print the title of this tree
128 std::cout << this->title << std::endl;
129 // recurse through the child trees if any and so on
130 for (DocumentTree child : children)
131 child.visualize_tree ();
134 // create the tree index - the index file for this tree
135 void DocumentTree::create_index (std::string destdir) {
136 std::unique_ptr<Document> index (new Document (this->title));
137 index.get()->set_index ();
138 // set the file name path
139 std::string filepath = destdir + "/" + this->stub_hierarchy () + this->stub;
140 // set the url path - this should not have destdir as it is not part
142 std::string urlpath = this->stub_hierarchy () + this->stub;
143 // if urlpath is not empty then append a / to the end of the URL. This
144 // is so that the base URL is not absolute
148 // create the sidebar
149 std::unique_ptr<SideBar> bar (new SideBar ());
150 bar.get()->set_title (SUB_CAT + this->title);
151 for (DocumentTree tree : this->children) {
152 // we use site relative URLs that rely on the base href tag
153 // so for biaweb generated sites, the base href tag should be
154 // used in the main template
155 SideBarItem item (tree.get_title(), urlpath +
156 tree.stub + "/" + "index.html");
157 bar.get()->add_sidebar_item (item);
159 index.get()->add_side_bar (*bar.get());
161 // create the path and then the index file
162 std::filesystem::create_directories (filepath);
163 index->output_to_html (filepath);
165 // create index for children
166 for (DocumentTree tree : this->children)
167 tree.create_index (destdir);