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 // summary for this tree - this is displayed in the index.html file of
21 // this tree before the list of articles in the tree
23 // file stub of this tree
25 // list of documents in this tree
26 std::list<Document> docs;
27 // set the parent - protected function as this has to be
28 // called only by add_child
29 void set_parent (DocumentTree *parent) {
30 this->parent = parent;
34 // create new top level document tree
35 DocumentTree (std::string title, std::string stub = "") {
37 // if stub is not empty set it
40 // make the stub from the title
42 this->stub = convert_title (title);
43 this->parent = nullptr;
46 // set the summary for this tree
47 void set_summary (std::string summary) {
48 this->summary = summary;
51 // set the summary for this tree as markdown text
52 void set_markdown_summary (std::string summary) {
53 this->summary = convert_to_markdown (summary);
56 std::string get_summary () {
60 // create the document index for this tree
61 void create_tree_html (std::string destdir);
64 void set_title (std::string title) {
68 this->stub = convert_title (title);
71 void set_stub (std::string stub) {
75 std::string get_title () {
79 std::string get_stub () {
83 // get the child level of this tree
84 unsigned int get_level ();
86 // get the stub hierarchy
87 std::string stub_hierarchy ();
89 // add a child tree to this tree
90 void add_child (DocumentTree *child) {
91 child->set_parent (this);
92 this->children.push_back (*child);
95 // add a document to this tree
96 void add_document (Document *doc) {
97 this->docs.push_back (*doc);
100 // print a visual representation of this tree with levels
101 void visualize_tree ();
103 // get a pointer to the parent of this tree
104 DocumentTree *get_parent () {
109 // get the tree level - 0 if top level
110 unsigned int DocumentTree::get_level () {
111 unsigned int lev = 0;
112 DocumentTree *par = this->get_parent ();
113 while (par != nullptr) {
115 par = par->get_parent ();
120 // get the stub hierarchy for this tree
121 std::string DocumentTree::stub_hierarchy () {
122 std::list<std::string> levels;
123 DocumentTree *par = this->get_parent();
124 while (par!= nullptr) {
125 levels.push_front (par->get_stub());
126 par = par->get_parent ();
128 std::string stub_str;
129 for (std::string level : levels) {
130 // if stub is empty, don't append a /
132 stub_str += level + "/";
137 // print the representation of this tree
138 void DocumentTree::visualize_tree () {
139 // print the tree level
140 std::cout << std::setw(3) << std::left << this->get_level ();
141 // indent as per the level
142 for (unsigned int i = 0; i < this->get_level(); i ++)
144 // print the title of this tree
145 std::cout << this->title << std::endl;
146 // recurse through the child trees if any and so on
147 for (DocumentTree child : children)
148 child.visualize_tree ();
151 // create the tree - the index file for this tree and all the documents and
152 // the child trees recursively
153 void DocumentTree::create_tree_html (std::string destdir) {
154 std::unique_ptr<Document> index (new Document (this->title));
155 index.get()->set_index ();
156 // set the file name path
157 std::string filepath = destdir + "/" + this->stub_hierarchy () + this->stub;
158 // set the url path - this should not have destdir as it is not part
160 std::string urlpath = this->stub_hierarchy () + this->stub;
161 // if urlpath is not empty then append a / to the end of the URL. This
162 // is so that the base URL is not absolute
166 // create the sidebars
168 // Create a link to the index page and
169 // If this tree has a parent, create a sidebar link to the level up
170 std::unique_ptr<SideBar> bar1 (new SideBar());
172 item0.set_sidebar_text (INDEX);
173 item0.set_sidebar_url (urlpath + "index.html");
174 bar1.get()->add_sidebar_item (item0);
175 if (this->get_parent() != nullptr) {
177 item1.set_sidebar_text (GO_UP);
178 item1.set_sidebar_url (this->stub_hierarchy() + "index.html");
179 bar1.get()->add_sidebar_item (item1);
181 index.get()->add_side_bar (*bar1.get());
183 // create a sidebar for the child levels if there are children
184 std::unique_ptr<SideBar> bar2 (new SideBar ());
185 bar2.get()->set_title (SUB_CAT + this->title);
186 for (DocumentTree tree : this->children) {
187 // we use site relative URLs that rely on the base href tag
188 // so for biaweb generated sites, the base href tag should be
189 // used in the main template
190 SideBarItem item (tree.get_title(), urlpath +
191 tree.stub + "/" + "index.html");
192 bar2.get()->add_sidebar_item (item);
194 index.get()->add_side_bar (*bar2.get());
196 // create the path and then the index file
197 std::filesystem::create_directories (filepath);
199 // Create the list of documents in this tree with links
200 // Reuse the sidebar class and sidebaritem class which is
201 // basically a list of links but instead of adding a sidebar
202 // add it to the content portion
203 SideBar article_list;
204 article_list.set_title (this->title + ": " + ART_LIST);
205 for (Document doc : this->docs) {
207 item.set_sidebar_text (doc.get_title());
208 item.set_sidebar_url (urlpath + doc.get_filename() + ".html");
209 article_list.add_sidebar_item (item);
210 // output the document also, add the side bars
211 if (this->get_parent() != nullptr)
212 doc.add_side_bar (*bar1.get());
213 doc.add_side_bar (*bar2.get());
214 doc.output_to_html (filepath);
216 index.get()->set_content (this->summary + article_list.to_html());
218 index.get()->output_to_html (filepath);
220 // create index for children
221 for (DocumentTree tree : this->children)
222 tree.create_tree_html (destdir);