From a302a25538bc69491c4844ef065c6b50e098387a Mon Sep 17 00:00:00 2001 From: Harishankar Date: Wed, 20 May 2020 21:26:31 +0530 Subject: [PATCH] Added Navigation bit to the top of documents for the categories Added navigation bit for easier navigation to parent categories from any page --- biawebdocument.hpp | 13 +++- biawebdocumenttree.hpp | 37 ++++++++--- biawebnavigationbit.hpp | 46 ++++++++++++++ biawebsidebar.hpp | 63 +++++++++---------- biawebstrings.hpp | 2 + biawebutil.hpp | 5 +- ....tpl.html => genericlinklistitem.tpl.html} | 0 ...item.tpl.html => genericlistitem.tpl.html} | 0 templates/main.tpl.html | 15 ++--- templates/navigationbit.tpl.html | 6 ++ templates/style.tpl.css | 29 +++++++-- 11 files changed, 160 insertions(+), 56 deletions(-) create mode 100644 biawebnavigationbit.hpp rename templates/{sidebarlinkitem.tpl.html => genericlinklistitem.tpl.html} (100%) rename templates/{sidebaritem.tpl.html => genericlistitem.tpl.html} (100%) create mode 100644 templates/navigationbit.tpl.html diff --git a/biawebdocument.hpp b/biawebdocument.hpp index 221bc4f..3fc15e1 100644 --- a/biawebdocument.hpp +++ b/biawebdocument.hpp @@ -12,6 +12,7 @@ #include "biawebutil.hpp" #include "biawebsidebar.hpp" #include "biawebstrings.hpp" +#include "biawebnavigationbit.hpp" // class to represent a biaweb document which can have a file name, title, description, // keywords, content and sidebar items @@ -24,7 +25,8 @@ namespace biaweb { std::string meta_desc; std::string meta_keywords; std::string content; - std::list sidebars; + std::list sidebars; + NavigationBit navbit; std::time_t cdate; std::time_t mdate; bool is_index; @@ -57,6 +59,11 @@ namespace biaweb { // fourth line onwards: Markdown contents Document (std::istream &file) ; + // set the navigation bit + void set_navigation_bit (NavigationBit navbit) { + this->navbit = navbit; + } + // set whether this is the index document void set_index (bool index = true) { this->is_index = index; @@ -206,6 +213,9 @@ namespace biaweb { sidebartext += bar.to_html (templatedir); } + // render the navigation bit + std::string navbit_str = this->navbit.to_html (templatedir); + // time of creation and modification struct std::tm c, m; c = *std::localtime (&this->cdate); @@ -219,6 +229,7 @@ namespace biaweb { fmt::arg ("description", this->meta_desc), fmt::arg ("cdate", c), fmt::arg ("mdate", m), + fmt::arg ("navbit", navbit_str), fmt::arg ("contents", this->content), fmt::arg ("sidebar", sidebartext) ); diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index c58a534..c070131 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -181,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 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()); @@ -200,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); } @@ -215,16 +216,36 @@ 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 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)); diff --git a/biawebnavigationbit.hpp b/biawebnavigationbit.hpp new file mode 100644 index 0000000..6be5b40 --- /dev/null +++ b/biawebnavigationbit.hpp @@ -0,0 +1,46 @@ +#ifndef __BIAWEBNAVIGATIONBIT__ +#define __BIAWEBNAVIGATIONBIT__ + +#include +#include +#include +#include "biawebutil.hpp" +#include "biawebsidebar.hpp" + +namespace biaweb { + // class to represent a navigation bit like this in html + // > Parent > Child > Subchild + // etc. + class NavigationBit { + protected: + std::list items; + public: + // add a generic link item - this should add to beginning of the list + void add_link_item (GenericLinkItem item) { + items.insert (items.cbegin(), item); + } + + // render using the given template directory + std::string to_html (std::string templatedir) ; + }; + + // render using the given template + std::string NavigationBit::to_html (std::string templatedir) { + std::ifstream templ (templatedir + "/navigationbit.tpl.html"); + std::string templ_str ((std::istreambuf_iterator (templ)), + (std::istreambuf_iterator ())); + + + std::string output_html = ""; + std::string items_str = ""; + for (GenericLinkItem item : this->items) + items_str += item.to_html (templatedir); + + if (this->items.size () > 0) + output_html = fmt::format (templ_str, fmt::arg ("items", items_str)); + + return output_html; + } +} + +#endif \ No newline at end of file diff --git a/biawebsidebar.hpp b/biawebsidebar.hpp index 932aa2c..6f419fa 100644 --- a/biawebsidebar.hpp +++ b/biawebsidebar.hpp @@ -7,64 +7,63 @@ #include #include "biawebutil.hpp" -// classes to describe the a list of items and sidebar item containers which form part of +// classes to describe the a list of (link) items and sidebar containers which form part of // main document namespace biaweb { - // class to represent a sidebar item which can contain a text and link or only + // class to represent a generic link item which can contain a text and link or only // text - class SideBarItem { + class GenericLinkItem { protected: // sidebar text and url - std::string sidebar_text; - std::string sidebar_url; + std::string itemtext; + std::string itemurl; public: - std::string get_sidebar_text () { - return this->sidebar_text; + std::string get_item_text () { + return this->itemtext; } - void set_sidebar_text (std::string text) { - this->sidebar_text = escape_html (text); + void set_item_text (std::string text) { + this->itemtext = escape_html (text); } - std::string get_sidebar_url () { - return this->sidebar_url; + std::string get_item_url () { + return this->itemurl; } - void set_sidebar_url (std::string url) { - this->sidebar_url = escape_html (url); + void set_item_url (std::string url) { + this->itemurl = escape_html (url); } // output to HTML using a template directory specified std::string to_html (std::string templatedir); - SideBarItem (std::string text = "", std::string url = "") { - this->sidebar_text = escape_html (text); - this->sidebar_url = escape_html (url); + GenericLinkItem (std::string text = "", std::string url = "") { + this->itemtext = escape_html (text); + this->itemurl = escape_html (url); } }; - std::string SideBarItem::to_html (std::string templatedir) { - std::string html; + std::string GenericLinkItem::to_html (std::string templatedir) { + std::string html = ""; // if url is not empty it is a link item so load the sidebar link template - if (! this->sidebar_url.empty ()) { - if (!this->sidebar_text.empty ()) + if (! this->itemurl.empty ()) { + if (!this->itemtext.empty ()) { - std::ifstream tpl_linkitem (templatedir + "/sidebarlinkitem.tpl.html"); + std::ifstream tpl_linkitem (templatedir + "/genericlinklistitem.tpl.html"); + std::string tpl_linkitem_str ( (std::istreambuf_iterator (tpl_linkitem)), (std::istreambuf_iterator ())); tpl_linkitem.close (); html = fmt::format (tpl_linkitem_str, - fmt::arg ("itemurl", this->sidebar_url), - fmt::arg ("itemtext", this->sidebar_text)); + fmt::arg ("itemurl", this->itemurl), + fmt::arg ("itemtext", this->itemtext)); } - // no text or url - item is empty - so it should be blank - else - html = ""; } // Non link item. Load the normal sidebar item template. - else + else if (! this->itemtext.empty ()) { - std::ifstream tpl_item (templatedir + "/sidebaritem.tpl.html"); + std::ifstream tpl_item (templatedir + "/genericlistitem.tpl.html"); + std::string tpl_item_str ( (std::istreambuf_iterator (tpl_item)), (std::istreambuf_iterator ())); tpl_item.close (); - html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->sidebar_text) ); + html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->itemtext) ); } return html; } @@ -74,14 +73,14 @@ namespace biaweb { class SideBar { protected: std::string sidebar_title; - std::list items; + std::list items; public: void set_title (std::string title) { this->sidebar_title = title; } - void add_sidebar_item (SideBarItem item) { + void add_sidebar_item (GenericLinkItem item) { this->items.insert (this->items.cend(), item); } @@ -97,7 +96,7 @@ namespace biaweb { sidetpl.close (); std::string listitems; // first get the sidebar items and render them to HTML - for (SideBarItem item : this->items) { + for (GenericLinkItem item : this->items) { listitems += item.to_html (templatedir); } diff --git a/biawebstrings.hpp b/biawebstrings.hpp index 76c4f2b..1e15e57 100644 --- a/biawebstrings.hpp +++ b/biawebstrings.hpp @@ -7,7 +7,9 @@ namespace biaweb { const char* SUB_CAT = "Sub categories: "; const char* ART_LIST = "List of Articles"; const char* INDEX = "Index Page"; + const char* NAVIGATE = "Navigation"; const char* WARNING_PARSE_FAILED = "Warning: date parse failed on " ; + const char* HOME = "Home"; // DATE FORMAT const char* DATE_IN_FORMAT = "%Y-%m-%d %H:%M %Z"; } diff --git a/biawebutil.hpp b/biawebutil.hpp index 6a87827..c8603ea 100644 --- a/biawebutil.hpp +++ b/biawebutil.hpp @@ -25,8 +25,8 @@ namespace biaweb { doc = mkd_string (str.c_str(), str.size(), 0); char tempfile[20]; strcpy (tempfile, "/tmp/biawebXXXXXX"); - mkstemp (tempfile); - FILE *f = fopen (tempfile, "w"); + int fd = mkstemp (tempfile); + FILE *f = fdopen (fd, "w"); markdown (doc, f, 0); fclose (f); std::ifstream ftmp (tempfile); @@ -35,6 +35,7 @@ namespace biaweb { ); ftmp.close (); mkd_cleanup (doc); + remove (tempfile); return tmpl; } diff --git a/templates/sidebarlinkitem.tpl.html b/templates/genericlinklistitem.tpl.html similarity index 100% rename from templates/sidebarlinkitem.tpl.html rename to templates/genericlinklistitem.tpl.html diff --git a/templates/sidebaritem.tpl.html b/templates/genericlistitem.tpl.html similarity index 100% rename from templates/sidebaritem.tpl.html rename to templates/genericlistitem.tpl.html diff --git a/templates/main.tpl.html b/templates/main.tpl.html index 804d24c..198a3c5 100644 --- a/templates/main.tpl.html +++ b/templates/main.tpl.html @@ -11,17 +11,18 @@ - -
+
+
Created on: {cdate:%d %b %Y, %H:%M %Z}, last modified: {mdate:%d %b %Y, %H:%M %Z}
+ {navbit} {contents} -
- - + diff --git a/templates/navigationbit.tpl.html b/templates/navigationbit.tpl.html new file mode 100644 index 0000000..51278e8 --- /dev/null +++ b/templates/navigationbit.tpl.html @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/templates/style.tpl.css b/templates/style.tpl.css index 4cb611d..3b3358e 100644 --- a/templates/style.tpl.css +++ b/templates/style.tpl.css @@ -9,27 +9,44 @@ body { h1, h2, h3, h4, h5, h6 { font-family: Georgia, 'Times New Roman', Times, serif; } -div#header { +header { width: 100%; color: white; background-color:#a1a0c0; height: 50px; float: left; } + +.inlinelist { + display: inline-block; + padding: 0em; + margin: 0em; +} + +.inlinelist li { + display: inline-block; + margin-right: 0.9em; +} + div#modification { width: 100%; - color: darkslateblue; - font-size: 0.9em; + color:darkgray; + font-size: 0.8em; float:left; } -div#footer { +div#navbit { + width: 100%; + float: left; + font-size: 0.8em; +} +footer { width: 100%; text-align: center; background-color: #a1a0c0; color: white; float:left; } -div#sidebar { +nav#sidebar { float :right; background-color: #f0f0f0; color: black; @@ -37,7 +54,7 @@ div#sidebar { padding: 1%; } -div#content { +section#content { width: 63%; margin-right:1%; margin-left: 1%; -- 2.20.1