Changed the rendering code for templating output
[biaweb2.git] / biawebdocument.hpp
index 9829bcd..40b0207 100644 (file)
@@ -5,7 +5,10 @@
 #include <string>
 #include <list>
 #include <memory>
+#include <iomanip>
 #include <ctime>
+#include <fmt/format.h>
+#include <fmt/chrono.h>
 #include "biawebutil.hpp"
 #include "biawebsidebar.hpp"
 #include "biawebstrings.hpp"
@@ -32,9 +35,9 @@ namespace biaweb {
                         bool is_index = false, std::time_t cdate= std::time(nullptr),
                             std::time_t mdate = std::time(nullptr))
         {
-            this->title = title;
-            this->meta_desc = meta_desc;
-            this->meta_keywords = meta_keywords;
+            this->title = escape_html (title);
+            this->meta_desc = escape_html (meta_desc);
+            this->meta_keywords = escape_html (meta_keywords);
             this->content = content;
             this->is_index = is_index;
             if (! is_index)
@@ -95,11 +98,11 @@ namespace biaweb {
         void set_markdown_content (std::string str); 
 
         void set_meta_keywords(std::string meta_keywords) {
-            this->meta_keywords = meta_keywords;
+            this->meta_keywords = escape_html (meta_keywords);
         }
         
         void set_meta_desc(std::string meta_desc) {
-            this->meta_desc = meta_desc;
+            this->meta_desc = escape_html (meta_desc);
         }
         
         void set_title(std::string title) {
@@ -137,41 +140,33 @@ namespace biaweb {
     
     void Document::output_to_html (std::string templatedir, std::string path)
     {
-        std::ifstream tpl;
-        tpl.open (templatedir + "/main.tpl.html");
-        std::string main_tpl ( (std::istreambuf_iterator<char> (tpl)),
+        std::ifstream tpl (templatedir + "/main.tpl.html");
+        std::string templstr ( (std::istreambuf_iterator<char> (tpl)),
                                 (std::istreambuf_iterator<char> ()) );
         tpl.close ();
+        std::ifstream style (templatedir + "/style.tpl.css");
+        std::string stylesheet ( (std::istreambuf_iterator<char> (style)),
+                                (std::istreambuf_iterator<char> ()));
+        style.close ();
         // first render the sidebars
         std::string sidebartext;
         for (SideBar bar : sidebars) {
             sidebartext += bar.to_html (templatedir);
         }
 
-        char ctm_str[100], mtm_str[100];
-        std::time_t creat = this->cdate;
-        std::time_t modif = this->cdate;
-        std::strftime (ctm_str, sizeof (ctm_str), 
-                        DATE_FORMAT, std::localtime (&creat));
-        std::strftime (mtm_str, sizeof (mtm_str),
-                        DATE_FORMAT, std::localtime (&modif));
-        
-        // Allocate enough space for the output buffer
-        std::unique_ptr<char[]> final_templ(
-                                    new char[main_tpl.size()+
-                                            this->title.size()+
-                                            this->content.size() +
-                                            this->meta_desc.size() +
-                                            this->meta_keywords.size () +
-                                            200 +
-                                            sidebartext.size()]);
-        std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(), 
-                this->meta_keywords.c_str(), this->meta_desc.c_str (),
-                ctm_str, mtm_str, 
-                this->content.c_str(), sidebartext.c_str());
+        std::string outputhtml = fmt::format (templstr, 
+                                    fmt::arg ("title", this->title),
+                                    fmt::arg ("keywords", this->meta_keywords), 
+                                    fmt::arg ("stylesheet", stylesheet),
+                                    fmt::arg ("description", this->meta_desc),
+                                    fmt::arg ("cdate", *std::localtime (&this->cdate)),
+                                    fmt::arg ("mdate", *std::localtime (&this->mdate)),
+                                    fmt::arg ("contents", this->content),
+                                    fmt::arg ("sidebar", sidebartext)
+                                    );
 
         std::ofstream f (path + "/" + this->filename + ".html");
-        f << final_templ.get ();
+        f << outputhtml;
         f.close ();
     }
 }