X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebsidebar.hpp;h=932aa2ce2ef82c0b0fcc2198035c3270be23a8e1;hp=4aef44f1a687588496b383e3d30461d896f18020;hb=fe9a0fef0b31ee3b94b3e73b7e319087a49a3054;hpb=b6425f960a30045333fb2ef531832a0b5858200d diff --git a/biawebsidebar.hpp b/biawebsidebar.hpp index 4aef44f..932aa2c 100644 --- a/biawebsidebar.hpp +++ b/biawebsidebar.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #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 (tpl_linkitem)), (std::istreambuf_iterator ())); tpl_linkitem.close (); - std::unique_ptr 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 (tpl_item)), (std::istreambuf_iterator ())); tpl_item.close (); - std::unique_ptr 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 (sidetpl)) , (std::istreambuf_iterator ())); 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 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; } }