Readme.md - added Section for customization and notes
[biaweb2.git] / biawebsidebar.hpp
index 75761da..226f51a 100644 (file)
@@ -4,72 +4,59 @@
 #include <list>
 #include <iostream>
 #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 = 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 = url;
+        void set_item_url (std::string url) {
+            this->itemurl = escape_html (url);
         }
-        std::string to_html ();
+        // output to HTML using a template specified
+        std::string to_html (Template *t);
     
-        SideBarItem (std::string text = "", std::string url = "") {
-            this->sidebar_text = text;
-            this->sidebar_url = 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 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 ("templates/sidebarlinkitem.tpl.html");
-                std::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
-                                                (std::istreambuf_iterator<char> ()));
-                tpl_linkitem.close ();
-                std::unique_ptr<char[]> linktxt (new char[tpl_linkitem_str.size() 
-                                                        + this->sidebar_text.size ()
-                                                        + this->sidebar_url.size ()] );
-                std::sprintf (linktxt.get(), tpl_linkitem_str.c_str (),
-                                             this->sidebar_url.c_str (),
-                                            this->sidebar_text.c_str ());
-                html.append (linktxt.get ());
+                std::string tpl_linkitem_str = t->get_genericlinklistitem_tpl ();
+                html = fmt::format (tpl_linkitem_str, 
+                                        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 ("templates/sidebaritem.tpl.html");
-            std::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
-                                        (std::istreambuf_iterator<char> ()));
-            tpl_item.close ();
-            std::unique_ptr<char[]> txt (new char [tpl_item_str.size () +
-                                                    this->sidebar_text.size ()]);
-            std::sprintf (txt.get (), tpl_item_str.c_str(), this->sidebar_text.c_str());
-            html.append (txt.get ());
+            std::string tpl_item_str = t->get_genericlistitem_tpl ();
+            html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->itemtext) );
         } 
         return html;
     }
@@ -79,49 +66,38 @@ 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) {
-            items.insert (items.cend(), item);
+        void add_sidebar_item (GenericLinkItem item) {
+            this->items.insert (this->items.cend(), item);
         }
 
-        // render the sidebar
-        std::string to_html () ;
+        // 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::ifstream sidetpl ("templates/sidebar.tpl.html");
-        std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) , 
-                                       (std::istreambuf_iterator<char> ()));
-        sidetpl.close ();
-        std::string listitem;
+    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 : items) {
-            listitem += item.to_html ();
+        for (GenericLinkItem item : this->items) {
+            listitems += item.to_html (t);
         }
-
-        std::unique_ptr<char[]> tpl_final (new char[sidetpl_str.size() + 
-                                                this->sidebar_title.size () +
-                                                listitem.size () ]);
         
-        std::string html;
-        // if there are items, sidebar should be rendered
+        std::string html = "";
+        // if there are items, sidebar should be rendered otherwise not needed
         if (items.size () > 0)
-        {        
-            std::sprintf (tpl_final.get (), sidetpl_str.c_str(), 
-                                        this->sidebar_title.c_str(), listitem.c_str()) ;
-            html.append ( tpl_final.get ());
-        }
-        // no items in the sidebar, render as empty string (even if it has a heading)
-        // since heading becomes meaningless without items
-        else 
-            html = "";
+            html = fmt::format (sidetpl_str, 
+                                        fmt::arg ("title", this->sidebar_title),
+                                        fmt::arg ("items", listitems));
+
         return html; 
     }
 }