Changed the rendering code for templating output
[biaweb2.git] / biawebsidebar.hpp
index 75761da..932aa2c 100644 (file)
@@ -4,6 +4,7 @@
 #include <list>
 #include <iostream>
 #include <fstream>
+#include <fmt/format.h>
 #include "biawebutil.hpp"
 
 // classes to describe the a list of items and sidebar item containers which form part of 
@@ -21,39 +22,36 @@ namespace biaweb {
             return this->sidebar_text;
         }
         void set_sidebar_text (std::string text) {
-            this->sidebar_text = text;
+            this->sidebar_text = escape_html (text);
         }
         std::string get_sidebar_url () {
             return this->sidebar_url;
         }
         void set_sidebar_url (std::string url) {
-            this->sidebar_url = url;
+            this->sidebar_url = escape_html (url);
         }
-        std::string to_html ();
+        // 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 = text;
-            this->sidebar_url = url;
+            this->sidebar_text = escape_html (text);
+            this->sidebar_url = escape_html (url);
         }
     };
 
-    std::string SideBarItem::to_html () {
+    std::string SideBarItem::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 ())
             {
-                std::ifstream tpl_linkitem ("templates/sidebarlinkitem.tpl.html");
+                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::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 ());
+                html = fmt::format (tpl_linkitem_str, 
+                                        fmt::arg ("itemurl", this->sidebar_url),
+                                        fmt::arg ("itemtext", this->sidebar_text));
             }
             // no text or url - item is empty - so it should be blank
             else
@@ -62,14 +60,11 @@ namespace biaweb {
         // Non link item. Load the normal sidebar item template.
         else
         {
-            std::ifstream tpl_item ("templates/sidebaritem.tpl.html");
+            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 ();
-            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 ());
+            html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->sidebar_text) );
         } 
         return html;
     }
@@ -87,41 +82,32 @@ namespace biaweb {
         }
 
         void add_sidebar_item (SideBarItem item) {
-            items.insert (items.cend(), item);
+            this->items.insert (this->items.cend(), item);
         }
 
-        // render the sidebar
-        std::string to_html () ;
+        // render the sidebar using the template directory specified
+        std::string to_html (std::string templatedir) ;
     };
 
     // render the sidebar to HTML representation from the template.
-    std::string SideBar::to_html () {
-        std::ifstream sidetpl ("templates/sidebar.tpl.html");
+    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 listitem;
+        std::string listitems;
         // first get the sidebar items and render them to HTML
-        for (SideBarItem item : items) {
-            listitem += item.to_html ();
+        for (SideBarItem item : this->items) {
+            listitems += item.to_html (templatedir);
         }
-
-        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; 
     }
 }