From: Harishankar Date: Tue, 19 May 2020 07:07:57 +0000 (+0530) Subject: Functionality for generating website completed X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=b6425f960a30045333fb2ef531832a0b5858200d;p=biaweb2.git Functionality for generating website completed TODO: add command line option for the tool instead of hard-coded strings TODO: add RSS feed XML template and classes to generate the RSS feed for the site --- diff --git a/biaweb.cpp b/biaweb.cpp index 0422995..22e4121 100644 --- a/biaweb.cpp +++ b/biaweb.cpp @@ -37,9 +37,9 @@ int main (int argc, char *argv[]) { // // std::cout << "Usage: " << argv[0] << "
" << std::endl; DocumentTree tree (""); - tree.document_tree_builder ("/home/hari/Projects/BiaWeb2/Test/In"); + tree.document_tree_builder ("Test/Reviews"); tree.visualize_tree (); - tree.create_tree_html ("/home/hari/Projects/BiaWeb2/templates", + tree.create_tree_html ("templates", "Test/Out"); // // } diff --git a/biawebdoclist.hpp b/biawebdoclist.hpp new file mode 100644 index 0000000..6082b39 --- /dev/null +++ b/biawebdoclist.hpp @@ -0,0 +1,135 @@ +#ifndef __BIAWEBDOCITEMLIST__ +#define __BIAWEBDOCITEMLIST__ +#include +#include +#include +#include +#include +#include "biawebstrings.hpp" +#include "biawebdocument.hpp" + +// to implement a list of items (documents) with creation/modified date/time display +namespace biaweb { + // to implement a single document item + class DocListItem { + protected: + // Just the required fields to build the item + std::string title; + std::string url; + std::time_t ctime; + std::time_t mtime; + public: + DocListItem (std::string title, std::string url, + std::time_t ctime, std::time_t mtime ) { + this->title = title; + this->url = url; + this->ctime = ctime; + this->mtime = mtime; + } + + std::time_t get_mtime() { + return this->mtime; + } + void set_mtime(std::time_t mtime) { + this->mtime = mtime; + } + std::time_t get_ctime() { + return this->ctime; + } + void set_ctime(std::time_t ctime) { + this->ctime = ctime; + } + + std::string get_url() { + return this->url; + } + + void set_url(std::string url) { + this->url = url; + } + + std::string get_title() { + return this->title; + } + void set_title(std::string title) { + this->title = title; + } + + // output to HTML + std::string to_html (std::string templatedir); + }; + + // output to HTML vide the template + std::string DocListItem::to_html (std::string templatedir) { + std::ifstream templ (templatedir + "/doclistitem.tpl.html"); + std::string templstr ((std::istreambuf_iterator (templ)), + (std::istreambuf_iterator ()) ); + templ.close (); + + // Allocate enough size for the output buffer + std::unique_ptr outputstr (new char [templstr.size() + + this->title.size() + + this->url.size () + + 200]); + + char ctm_str[100]; + char mtm_str[100]; + strftime (ctm_str, 100, DATE_FORMAT, std::localtime (&this->ctime)); + strftime (mtm_str, 100, DATE_FORMAT, std::localtime (&this->mtime)); + + sprintf (outputstr.get(), templstr.c_str(), + this->url.c_str(), + this->title.c_str(), + ctm_str, mtm_str); + + std::string outputhtml; + outputhtml.append (outputstr.get()); + return outputhtml; + } + + // to implement a document list (or table) + class DocList { + protected: + std::string title; + std::list items; + public: + void set_title (std::string title) { + this->title = title; + } + // add a document item + void add_document_item (DocListItem docitem) { + this->items.insert (this->items.cend(), docitem); + } + // render to HTML from a template + std::string to_html (std::string templatedir); + }; + + std::string DocList::to_html (std::string templatedir) { + std::ifstream templ (templatedir + "/doclist.tpl.html"); + std::string templstr ( (std::istreambuf_iterator (templ) ), + (std::istreambuf_iterator ())); + + std::string docitems = ""; + std::string outputhtml = ""; + // if the number of elements is non zero + if (this->items.size () != 0) { + for (DocListItem item : this->items) + docitems += item.to_html (templatedir); + + // Allocate space for output buffer + std::unique_ptr outputstr (new char [ + templstr.size () + + docitems.size () + + this->title.size () + ]); + sprintf (outputstr.get(), templstr.c_str(), + this->title.c_str(), + docitems.c_str() ); + + outputhtml.append (outputstr.get()); + } + return outputhtml; + } +} + +#endif \ No newline at end of file diff --git a/biawebdocument.hpp b/biawebdocument.hpp index edfed7e..9829bcd 100644 --- a/biawebdocument.hpp +++ b/biawebdocument.hpp @@ -8,6 +8,7 @@ #include #include "biawebutil.hpp" #include "biawebsidebar.hpp" +#include "biawebstrings.hpp" // class to represent a biaweb document which can have a file name, title, description, // keywords, content and sidebar items @@ -137,7 +138,7 @@ namespace biaweb { void Document::output_to_html (std::string templatedir, std::string path) { std::ifstream tpl; - tpl.open (templatedir + "/main.tpl.html", std::ios_base::openmode::_S_in); + tpl.open (templatedir + "/main.tpl.html"); std::string main_tpl ( (std::istreambuf_iterator (tpl)), (std::istreambuf_iterator ()) ); tpl.close (); @@ -151,9 +152,9 @@ namespace biaweb { std::time_t creat = this->cdate; std::time_t modif = this->cdate; std::strftime (ctm_str, sizeof (ctm_str), - "%d %b %Y, %H:%M", std::localtime (&creat)); + DATE_FORMAT, std::localtime (&creat)); std::strftime (mtm_str, sizeof (mtm_str), - "%d %b %Y, %H:%M", std::localtime (&modif)); + DATE_FORMAT, std::localtime (&modif)); // Allocate enough space for the output buffer std::unique_ptr final_templ( @@ -165,6 +166,7 @@ namespace biaweb { 200 + sidebartext.size()]); std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(), + this->meta_keywords.c_str(), this->meta_desc.c_str (), ctm_str, mtm_str, this->content.c_str(), sidebartext.c_str()); diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index 608b702..7c44634 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -8,8 +8,9 @@ #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: @@ -208,29 +209,27 @@ namespace biaweb { std::filesystem::create_directories (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); + 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) { - SideBarItem item; - item.set_sidebar_text (doc.get_title()); - item.set_sidebar_url (urlpath + doc.get_filename() + ".html"); - article_list.add_sidebar_item (item); + 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.get()->set_content (this->summary + article_list.to_html(templatedir)); + // 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); - // create index for children + // recursively create index for children for (DocumentTree tree : this->children) tree.create_tree_html (templatedir, destdir); } diff --git a/biawebsidebar.hpp b/biawebsidebar.hpp index a5ccef6..4aef44f 100644 --- a/biawebsidebar.hpp +++ b/biawebsidebar.hpp @@ -88,7 +88,7 @@ namespace biaweb { } void add_sidebar_item (SideBarItem item) { - items.insert (items.cend(), item); + this->items.insert (this->items.cend(), item); } // render the sidebar using the template directory specified @@ -103,7 +103,7 @@ namespace biaweb { sidetpl.close (); std::string listitem; // first get the sidebar items and render them to HTML - for (SideBarItem item : items) { + for (SideBarItem item : this->items) { listitem += item.to_html (templatedir); } diff --git a/biawebstrings.hpp b/biawebstrings.hpp index df9af51..1e073f0 100644 --- a/biawebstrings.hpp +++ b/biawebstrings.hpp @@ -2,10 +2,13 @@ #define __BIAWEBSTRINGS__ namespace biaweb { + // COMMON STRINGS const char* GO_UP = "Go up"; const char* SUB_CAT = "Sub categories: "; const char* ART_LIST = "List of Articles"; const char* INDEX = "Index Page"; + // DATE FORMAT + const char* DATE_FORMAT = "%d %b %Y, %H:%M"; } #endif diff --git a/templates/doclist.tpl.html b/templates/doclist.tpl.html new file mode 100644 index 0000000..54bf4d4 --- /dev/null +++ b/templates/doclist.tpl.html @@ -0,0 +1,13 @@ +

%s

+ + + + + + + + + + %s + +
Document titleCreated onLast modified
\ No newline at end of file diff --git a/templates/doclistitem.tpl.html b/templates/doclistitem.tpl.html new file mode 100644 index 0000000..4f16239 --- /dev/null +++ b/templates/doclistitem.tpl.html @@ -0,0 +1,5 @@ + + %s + %s + %s + \ No newline at end of file diff --git a/templates/main.tpl.html b/templates/main.tpl.html index b506bc9..0073bfc 100644 --- a/templates/main.tpl.html +++ b/templates/main.tpl.html @@ -2,6 +2,8 @@ My Site - %s + + -
Created on: %s, last modified: %s
@@ -69,7 +68,6 @@ -