Separated the strings used for document tree generation from code
[biaweb2.git] / biawebsidebar.hpp
index 932aa2c..6f419fa 100644 (file)
@@ -7,64 +7,63 @@
 #include <fmt/format.h>
 #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<char> (tpl_linkitem)),
                                                 (std::istreambuf_iterator<char> ()));
                 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<char> (tpl_item)),
                                         (std::istreambuf_iterator<char> ()));
             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 <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);
         }
 
@@ -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);
         }