Added Navigation bit to the top of documents for the categories
[biaweb2.git] / biawebdocumenttree.hpp
index 7c44634..c070131 100644 (file)
@@ -39,7 +39,7 @@ namespace biaweb {
 
         // create new top level document tree
         DocumentTree (std::string title, std::string stub = "") {
-            this->title = title;
+            this->title = escape_html (title);
             // if stub is not empty set it
             if (stub != "")
                 this->stub = stub;
@@ -74,7 +74,7 @@ namespace biaweb {
 
         // set the title 
         void set_title (std::string title) {
-            this->title = title;
+            this->title = escape_html (title);
             // if no stub is set
             if (this->stub == "")
                 this->stub = convert_title (title);
@@ -163,6 +163,7 @@ namespace biaweb {
     // create the tree - the index file for this tree and all the documents and
     // the child trees recursively - using the template specified
     void DocumentTree::create_tree_html (std::string templatedir, std::string destdir) {
+        // create a document to represent the index of the tree.
         std::unique_ptr<Document> index (new Document (this->title));
         index.get()->set_index ();
         // set the file name path
@@ -180,14 +181,15 @@ namespace biaweb {
         // 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");
+        GenericLinkItem item0;
+        bar1.get()->set_title (NAVIGATE);
+        item0.set_item_text (INDEX);
+        item0.set_item_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");
+            GenericLinkItem item1;
+            item1.set_item_text (GO_UP);
+            item1.set_item_url (this->stub_hierarchy() + "index.html");
             bar1.get()->add_sidebar_item (item1);
         }
         index.get()->add_side_bar (*bar1.get());
@@ -199,7 +201,7 @@ namespace biaweb {
             // 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 + 
+            GenericLinkItem item (tree.get_title(), urlpath + 
                                             tree.stub + "/" + "index.html");
             bar2.get()->add_sidebar_item (item);
         }
@@ -214,19 +216,40 @@ namespace biaweb {
         // sort the documents as per creation time and then add the document
         // links - newest documents should appear above older ones.
         sort_documents_creation_time ();
+
+        // create the navigation bit 
+        std::shared_ptr<NavigationBit> navbit (new NavigationBit ());
+        auto par1 = this;
+        // get the link to each level in the hierarchy and add it as 
+        // an inline list
+        while (par1 != nullptr) {
+            if (par1->parent != nullptr)
+                navbit.get()->add_link_item (GenericLinkItem(par1->stub, 
+                            par1->stub_hierarchy() + par1->stub + "/index.html"));
+            else
+                navbit.get()->add_link_item (GenericLinkItem(HOME, "index.html"));
+            par1 = par1->parent;
+        }
+
         for (Document doc : this->docs) {
             DocListItem item (doc.get_title(),
                             urlpath + doc.get_filename() + ".html", 
                             doc.get_creation_date(), doc.get_modified_date ());
             article_list.get()->add_document_item (item);
-            // output the document also, add the side bars
+            // output the document also, add the navigation bit and side bars
+
+            doc.set_navigation_bit (*navbit.get());
             doc.add_side_bar (*bar1.get());
             doc.add_side_bar (*bar2.get());
             doc.output_to_html (templatedir, filepath);
         }
+
+        // add the navigation bit
+        index.get()->set_navigation_bit (*navbit.get());
         // index should contain the summary followed by the article list
         index.get()->set_content (this->summary + article_list.get()->to_html(templatedir));
 
+        // output the index file
         index.get()->output_to_html (templatedir, filepath);
 
         // recursively create index for children
@@ -253,30 +276,28 @@ namespace biaweb {
                 }
                 // add the regular files as documents in the tree
                 else if (fsitem.is_regular_file ()) {
-                    // read the contents of the file
-                    std::ifstream infile (fsitem.path().string());
-                    std::string infilestr ((std::istreambuf_iterator<char> (infile)),
-                                            (std::istreambuf_iterator<char> ()));
-                    // if it is an index file (specially named index) add 
+                    // if it is an index file (specially named as index 
+                    // or index.md or whatever) directly add 
                     // the contents to the summary of the Doctree 
                     if (fsitem.path().stem().string() == "index")
                     {
+                        std::ifstream infile (fsitem.path());
+                        std::string infilestr ( (std::istreambuf_iterator<char> (infile)),
+                                                (std::istreambuf_iterator<char> ()) );
                         this->set_markdown_summary (infilestr);
                     }
                     // else it is a non-index file-  
                     // create a Document and add it to the tree
                     else {
+                        std::ifstream infile (fsitem.path ());
                         std::shared_ptr<Document> doc 
-                                (new Document (fsitem.path().stem().string()));
+                                (new Document (infile));
+                        infile.close ();
                         
-                        // file creation/modified date from system
+                        // file modified date from system
                         struct stat buf;
                         if (stat (fsitem.path().string().c_str(), &buf) == 0)
-                        {
-                            doc.get()->set_creation_date (buf.st_ctim.tv_sec);
                             doc.get()->set_modified_date (buf.st_mtim.tv_sec);
-                        }
-                        doc.get()->set_markdown_content (infilestr);
 
                         this->add_document (doc.get());
                     }