Document tree generation to HTML output completed
[biaweb2.git] / biawebdocumenttree.hpp
index 9c91ed9..cf75c86 100644 (file)
@@ -5,6 +5,7 @@
 #include <iostream>
 #include <filesystem>
 #include "biawebdocument.hpp"
+#include "biawebstrings.hpp"
 
 // to implement a document tree - both with or without subtrees
 namespace biaweb {
@@ -40,7 +41,7 @@ namespace biaweb {
         }
 
         // create the document index for this tree
-        void create_index ();
+        void create_tree_html (std::string destdir);
 
         // set the title 
         void set_title (std::string title) {
@@ -50,13 +51,8 @@ namespace biaweb {
                 this->stub = convert_title (title);
         }
         
-        // set the stub either from a text or convert the title to stub
-        // if no stub is set explicitly
         void set_stub (std::string stub) {
-            if (stub != "")
-                this->stub = stub;
-            else
-                this->stub = convert_title (this->title);
+            this->stub = stub;
         }
 
         std::string get_title () {
@@ -71,19 +67,7 @@ namespace biaweb {
         unsigned int get_level (); 
 
         // get the stub hierarchy
-        std::string stub_hierarchy () {
-            std::list<std::string> levels;
-            DocumentTree *par = this->get_parent();
-            while (par!= nullptr) {
-                levels.push_front (par->get_stub());
-                par = par->get_parent ();
-            }
-            std::string stub_str;
-            for (std::string level : levels) {
-                stub_str += level + "/";
-            }
-            return stub_str;
-        }
+        std::string stub_hierarchy (); 
 
         // add a child tree to this tree
         void add_child (DocumentTree *child) {
@@ -116,6 +100,23 @@ namespace biaweb {
             return lev;
     }
 
+    // get the stub hierarchy for this tree
+    std::string DocumentTree::stub_hierarchy () {
+            std::list<std::string> levels;
+            DocumentTree *par = this->get_parent();
+            while (par!= nullptr) {
+                levels.push_front (par->get_stub());
+                par = par->get_parent ();
+            }
+            std::string stub_str;
+            for (std::string level : levels) {
+                // if stub is empty, don't append a /
+                if (level != "")
+                    stub_str += level + "/";
+            }
+            return stub_str;
+    }
+
     // print the representation of this tree
     void DocumentTree::visualize_tree () {
             // print the tree level
@@ -130,30 +131,78 @@ namespace biaweb {
                 child.visualize_tree ();
     }
 
-    // create the tree index - the index file for this tree
-    void DocumentTree::create_index () {
+    // create the tree - the index file for this tree and all the documents and
+    // the child trees recursively
+    void DocumentTree::create_tree_html (std::string destdir) {
         std::unique_ptr<Document> index (new Document (this->title));
         index.get()->set_index ();
-        // set the file name
-        std::string filepath = this->stub_hierarchy () + 
-                                    this->stub;
-        // create the sidebar
-        std::unique_ptr<SideBar> bar (new SideBar ());
-        bar.get()->set_title (this->title);
+        // set the file name path
+        std::string filepath = destdir + "/" + this->stub_hierarchy () + this->stub;
+        // set the url path - this should not have destdir as it is not part
+        // of the site tree
+        std::string urlpath = this->stub_hierarchy () + this->stub;
+        // if urlpath is not empty then append a / to the end of the URL. This 
+        // is so that the base URL is not absolute
+        if (urlpath != "")
+            urlpath += "/";
+
+        // create the sidebars
+        // First sidebar
+        // Create a link to the index page and 
+        // If this tree has a parent, create a sidebar link to the level up
+        std::unique_ptr<SideBar> bar1 (new SideBar());
+        SideBarItem item0;
+        item0.set_sidebar_text (INDEX);
+        item0.set_sidebar_url (urlpath + "index.html");
+        bar1.get()->add_sidebar_item (item0);
+        if (this->get_parent() != nullptr) {
+            SideBarItem item1;
+            item1.set_sidebar_text (GO_UP);
+            item1.set_sidebar_url (this->stub_hierarchy() + "index.html");
+            bar1.get()->add_sidebar_item (item1);
+        }
+        index.get()->add_side_bar (*bar1.get());
+
+        // create a sidebar for the child levels if there are children
+        std::unique_ptr<SideBar> bar2 (new SideBar ());
+        bar2.get()->set_title (SUB_CAT + this->title);
         for (DocumentTree tree : this->children) {
-            SideBarItem item (tree.get_title(), filepath + "/" + 
+            // we use site relative URLs that rely on the base href tag
+            // so for biaweb generated sites, the base href tag should be 
+            // used in the main template
+            SideBarItem item (tree.get_title(), urlpath + 
                                             tree.stub + "/" + "index.html");
-            bar.get()->add_sidebar_item (item);
+            bar2.get()->add_sidebar_item (item);
         }
-        index.get()->add_side_bar (*bar.get());
+        index.get()->add_side_bar (*bar2.get());
 
         // create the path and then the index file
         std::filesystem::create_directories (filepath);
-        index->output_to_html (filepath);
+
+        // Create the list of documents in this tree with links
+        // Reuse the sidebar class and sidebaritem class which is
+        // basically a list of links but instead of adding a sidebar
+        // add it to the content portion  
+        SideBar article_list;
+        article_list.set_title (this->title + ": " + ART_LIST);
+        for (Document doc : this->docs) {
+            SideBarItem item;
+            item.set_sidebar_text (doc.get_title());
+            item.set_sidebar_url (urlpath + doc.get_filename() + ".html");
+            article_list.add_sidebar_item (item);
+            // output the document also, add the side bars
+            if (this->get_parent() != nullptr)
+                doc.add_side_bar (*bar1.get());
+            doc.add_side_bar (*bar2.get());
+            doc.output_to_html (filepath);
+        }
+        index.get()->set_content (article_list.to_html());
+
+        index.get()->output_to_html (filepath);
 
         // create index for children
         for (DocumentTree tree : this->children)
-            tree.create_index ();
+            tree.create_tree_html (destdir);
     }
 }