X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebdocumenttree.hpp;h=608b70242179dc0056c8ada7318983891bd40812;hp=a65e334cc75667cb1e1248f560297448128cfb5a;hb=1d1f04629b04c7e7efd14d8f281e17f5e491c888;hpb=f56e2150cc79638c890ef23ef82097a4bdffd2ce diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index a65e334..608b702 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -4,8 +4,10 @@ #include #include #include +#include #include "biawebdocument.hpp" #include "biawebstrings.hpp" +#include "biawebutil.hpp" // to implement a document tree - both with or without subtrees namespace biaweb { @@ -31,6 +33,9 @@ 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; @@ -57,8 +62,14 @@ namespace biaweb { 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_tree_html (std::string destdir); + void create_tree_html (std::string templatedir, std::string destdir); // set the title void set_title (std::string title) { @@ -142,15 +153,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 - the index file for this tree and all the documents and - // the child trees recursively - void DocumentTree::create_tree_html (std::string destdir) { + // 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 @@ -202,24 +213,84 @@ namespace biaweb { // add it to the content portion SideBar article_list; article_list.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) { 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 (*bar1.get()); doc.add_side_bar (*bar2.get()); - doc.output_to_html (filepath); + doc.output_to_html (templatedir, filepath); } - index.get()->set_content (this->summary + article_list.to_html()); + index.get()->set_content (this->summary + article_list.to_html(templatedir)); - index.get()->output_to_html (filepath); + index.get()->output_to_html (templatedir, filepath); // create index for children for (DocumentTree tree : this->children) - tree.create_tree_html (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); } }