Changed the rendering code for templating output
[biaweb2.git] / biawebsidebar.hpp
index 4aef44f..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,20 +22,20 @@ 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);
         }
         // 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);
         }
     };
 
@@ -48,13 +49,9 @@ namespace biaweb {
                 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
@@ -67,10 +64,7 @@ namespace biaweb {
             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;
     }
@@ -101,28 +95,19 @@ namespace biaweb {
         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 : this->items) {
-            listitem += item.to_html (templatedir);
+            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; 
     }
 }