From: Harishankar Date: Sat, 16 May 2020 09:29:13 +0000 (+0530) Subject: Cleaned up the generation of document tree index X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=commitdiff_plain;h=406aa544d873974501056201467c88366f9d31f9 Cleaned up the generation of document tree index Cleaned up the generation of document tree by including a destination dir and separating URL path from destination dir --- diff --git a/biaweb b/biaweb deleted file mode 100755 index 3681f1b..0000000 Binary files a/biaweb and /dev/null differ diff --git a/biaweb.cpp b/biaweb.cpp index a50bf01..0d9a3e8 100644 --- a/biaweb.cpp +++ b/biaweb.cpp @@ -8,6 +8,7 @@ int main (int argc, char *argv[]) { if (argc == 2) { std::shared_ptr tree (new DocumentTree (argv[1])); + tree.get()->set_stub (""); std::shared_ptr a1 (new DocumentTree("Child a1")); std::shared_ptr a2 (new DocumentTree("Child a2")); @@ -19,7 +20,7 @@ int main (int argc, char *argv[]) { tree.get()->add_child (a1.get()); std::cout << a3.get()->stub_hierarchy () << a3.get()->get_stub () << std::endl; tree.get()->visualize_tree (); - tree.get()->create_index (); + tree.get()->create_index (convert_title (argv[1])); } else std::cout << "Usage: " << argv[0] << "
" << std::endl; diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index 9c91ed9..41aa81a 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -5,6 +5,7 @@ #include #include #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_index (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 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 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 @@ -131,17 +132,27 @@ namespace biaweb { } // create the tree index - the index file for this tree - void DocumentTree::create_index () { + void DocumentTree::create_index (std::string destdir) { std::unique_ptr index (new Document (this->title)); index.get()->set_index (); - // set the file name - std::string filepath = this->stub_hierarchy () + - this->stub; + // 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 sidebar std::unique_ptr bar (new SideBar ()); - bar.get()->set_title (this->title); + bar.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); } @@ -153,7 +164,7 @@ namespace biaweb { // create index for children for (DocumentTree tree : this->children) - tree.create_index (); + tree.create_index (destdir); } } diff --git a/biawebstrings.hpp b/biawebstrings.hpp new file mode 100644 index 0000000..8e2f947 --- /dev/null +++ b/biawebstrings.hpp @@ -0,0 +1,9 @@ +#ifndef __BIAWEBSTRINGS__ +#define __BIAWEBSTRINGS__ + +namespace biaweb { + const char* GO_UP = "Go up"; + const char* SUB_CAT = "Sub categories: "; +} + +#endif diff --git a/templates/main.tpl.html b/templates/main.tpl.html index 197cb80..0bdf62f 100644 --- a/templates/main.tpl.html +++ b/templates/main.tpl.html @@ -1,9 +1,9 @@ -%s +My Site - %s - +