X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebdocumenttree.hpp;h=66dcd9f7eea322b22e2a9c2a31c431997bf62e09;hp=41aa81aea2aa4c3f483decfccea2d87a5ccb9a8b;hb=fe9a0fef0b31ee3b94b3e73b7e319087a49a3054;hpb=406aa544d873974501056201467c88366f9d31f9 diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index 41aa81a..66dcd9f 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -4,10 +4,13 @@ #include #include #include +#include #include "biawebdocument.hpp" #include "biawebstrings.hpp" +#include "biawebutil.hpp" +#include "biawebdoclist.hpp" -// to implement a document tree - both with or without subtrees +// class to implement a document tree - both with or without subtrees namespace biaweb { class DocumentTree { protected: @@ -17,6 +20,9 @@ namespace biaweb { std::list children; // title of this tree std::string title; + // summary for this tree - this is displayed in the index.html file of + // this tree before the list of articles in the tree + std::string summary; // file stub of this tree std::string stub; // list of documents in this tree @@ -28,9 +34,12 @@ namespace biaweb { } public: + // method to build a document tree from a path + void document_tree_builder (std::string srcpath); + // 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; @@ -40,12 +49,32 @@ namespace biaweb { this->parent = nullptr; } + // set the summary for this tree + void set_summary (std::string summary) { + this->summary = summary; + } + + // set the summary for this tree as markdown text + void set_markdown_summary (std::string summary) { + this->summary = convert_to_markdown (summary); + } + + std::string get_summary () { + return this->summary; + } + + // sort the documents as per creation time from latest to oldest + void sort_documents_creation_time () { + this->docs.sort ([] (Document &a, Document &b) + {return (a.get_creation_date() > b.get_creation_date()); }); + } + // create the document index for this tree - void create_index (std::string destdir); + void create_tree_html (std::string templatedir, std::string destdir); // 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); @@ -125,14 +154,15 @@ namespace biaweb { for (unsigned int i = 0; i < this->get_level(); i ++) std::cout << "+--"; // print the title of this tree - std::cout << this->title << std::endl; + std::cout << this->title << std::endl; // recurse through the child trees if any and so on for (DocumentTree child : children) child.visualize_tree (); } - // create the tree index - the index file for this tree - void DocumentTree::create_index (std::string destdir) { + // 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) { std::unique_ptr index (new Document (this->title)); index.get()->set_index (); // set the file name path @@ -145,26 +175,121 @@ namespace biaweb { if (urlpath != "") urlpath += "/"; - // create the sidebar - std::unique_ptr bar (new SideBar ()); - bar.get()->set_title (SUB_CAT + this->title); + // 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 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 bar2 (new SideBar ()); + bar2.get()->set_title (SUB_CAT + this->title); for (DocumentTree tree : this->children) { // 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 index for children + // Create the list of documents in this tree with links + std::unique_ptr article_list (new DocList ()); + article_list.get()->set_title (this->title + ": " + ART_LIST); + // sort the documents as per creation time and then add the document + // links - newest documents should appear above older ones. + sort_documents_creation_time (); + 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 + doc.add_side_bar (*bar1.get()); + doc.add_side_bar (*bar2.get()); + doc.output_to_html (templatedir, filepath); + } + // index should contain the summary followed by the article list + index.get()->set_content (this->summary + article_list.get()->to_html(templatedir)); + + index.get()->output_to_html (templatedir, filepath); + + // recursively create index for children for (DocumentTree tree : this->children) - tree.create_index (destdir); + tree.create_tree_html (templatedir, destdir); + } + + // build a document tree from a filesystem path recursively + void DocumentTree::document_tree_builder (std::string srcpath_str) { + std::filesystem::path srcpath (srcpath_str); + this->title = srcpath.stem().string (); + + // Get the directories to this child and add them as sub document + // trees + try { + for (auto fsitem : std::filesystem::directory_iterator (srcpath) ) + { + // if it is a directory then build the tree for that directory + if (fsitem.is_directory ()) { + std::shared_ptr doctree + (new DocumentTree (fsitem.path().filename().string())); + + this->add_child (doctree.get()); + } + // 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 (infile)), + (std::istreambuf_iterator ())); + // if it is an index file (specially named index) add + // the contents to the summary of the Doctree + if (fsitem.path().stem().string() == "index") + { + this->set_markdown_summary (infilestr); + } + // else it is a non-index file- + // create a Document and add it to the tree + else { + std::shared_ptr doc + (new Document (fsitem.path().stem().string())); + + // file creation/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()); + } + } + } + } + catch (std::filesystem::filesystem_error) { + std::cout << "No such path! Specify an existing file path" << std::endl; + } + + // add the trees for the children recursively + for (DocumentTree &child : this->children) + child.document_tree_builder (srcpath_str + "/" + child.title); } }