Readme.md - added Section for customization and notes
[biaweb2.git] / biawebsidebar.hpp
index 932aa2c..226f51a 100644 (file)
@@ -6,65 +6,57 @@
 #include <fstream>
 #include <fmt/format.h>
 #include "biawebutil.hpp"
+#include "biawebtemplate.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);
+        // output to HTML using a template specified
+        std::string to_html (Template *t);
     
-        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 (Template *t) {
+        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::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
-                                                (std::istreambuf_iterator<char> ()));
-                tpl_linkitem.close ();
+                std::string tpl_linkitem_str = t->get_genericlinklistitem_tpl ();
                 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::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
-                                        (std::istreambuf_iterator<char> ()));
-            tpl_item.close ();
-            html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->sidebar_text) );
+            std::string tpl_item_str = t->get_genericlistitem_tpl ();
+            html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->itemtext) );
         } 
         return html;
     }
@@ -74,31 +66,29 @@ namespace biaweb {
     class SideBar {
       protected:
         std::string sidebar_title;
-        std::list <SideBarItem> items;
+        std::list <GenericLinkItem> 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);
         }
 
-        // render the sidebar using the template directory specified
-        std::string to_html (std::string templatedir) ;
+        // render the sidebar using the template specified
+        std::string to_html (Template *t) ;
     };
 
     // render the sidebar to HTML representation from the template.
-    std::string SideBar::to_html (std::string templatedir) {
-        std::ifstream sidetpl (templatedir + "/sidebar.tpl.html");
-        std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) , 
-                                       (std::istreambuf_iterator<char> ()));
-        sidetpl.close ();
+    std::string SideBar::to_html (Template *t) {
+        std::string sidetpl_str = t->get_sidebar_tpl ();
+
         std::string listitems;
         // first get the sidebar items and render them to HTML
-        for (SideBarItem item : this->items) {
-            listitems += item.to_html (templatedir);
+        for (GenericLinkItem item : this->items) {
+            listitems += item.to_html (t);
         }
         
         std::string html = "";