X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebdoclist.hpp;h=8995c3c09f0d30c865beff327f5fe2151391410e;hp=6082b398f1a6564fd82529ee93a8b9301d52fddf;hb=HEAD;hpb=b6425f960a30045333fb2ef531832a0b5858200d diff --git a/biawebdoclist.hpp b/biawebdoclist.hpp index 6082b39..8995c3c 100644 --- a/biawebdoclist.hpp +++ b/biawebdoclist.hpp @@ -4,9 +4,13 @@ #include #include #include +#include #include +#include +#include #include "biawebstrings.hpp" #include "biawebdocument.hpp" +#include "biawebtemplate.hpp" // to implement a list of items (documents) with creation/modified date/time display namespace biaweb { @@ -16,74 +20,55 @@ namespace biaweb { // Just the required fields to build the item std::string title; std::string url; + std::string desc; std::time_t ctime; std::time_t mtime; public: - DocListItem (std::string title, std::string url, - std::time_t ctime, std::time_t mtime ) { - this->title = title; - this->url = url; - this->ctime = ctime; - this->mtime = mtime; + DocListItem (Document *doc, std::string urlpath) { + this->title = doc->get_title (); + this->url = urlpath + doc->get_filename() + ".html"; + this->ctime = doc->get_creation_date (); + this->mtime = doc->get_modified_date (); + this->desc = doc->get_meta_desc (); + } + + std::string get_desc () { + return this->desc; } std::time_t get_mtime() { return this->mtime; } - void set_mtime(std::time_t mtime) { - this->mtime = mtime; - } + std::time_t get_ctime() { return this->ctime; } - void set_ctime(std::time_t ctime) { - this->ctime = ctime; - } std::string get_url() { return this->url; } - void set_url(std::string url) { - this->url = url; - } - std::string get_title() { return this->title; } - void set_title(std::string title) { - this->title = title; - } - // output to HTML - std::string to_html (std::string templatedir); + // output to HTML vide the template + std::string to_html (Template *t); }; // output to HTML vide the template - std::string DocListItem::to_html (std::string templatedir) { - std::ifstream templ (templatedir + "/doclistitem.tpl.html"); - std::string templstr ((std::istreambuf_iterator (templ)), - (std::istreambuf_iterator ()) ); - templ.close (); + std::string DocListItem::to_html (Template *t) { + std::string templstr = t->get_doclistitem_tpl (); + std::tm c, m; + c = *std::localtime (&this->ctime); + m = *std::localtime (&this->mtime); - // Allocate enough size for the output buffer - std::unique_ptr outputstr (new char [templstr.size() + - this->title.size() + - this->url.size () + - 200]); + std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url), + fmt::arg("doctitle", this->title), + fmt::arg("desc", this->desc), + fmt::arg("cdate", c), + fmt::arg("mdate", m)); - char ctm_str[100]; - char mtm_str[100]; - strftime (ctm_str, 100, DATE_FORMAT, std::localtime (&this->ctime)); - strftime (mtm_str, 100, DATE_FORMAT, std::localtime (&this->mtime)); - - sprintf (outputstr.get(), templstr.c_str(), - this->url.c_str(), - this->title.c_str(), - ctm_str, mtm_str); - - std::string outputhtml; - outputhtml.append (outputstr.get()); return outputhtml; } @@ -101,32 +86,22 @@ namespace biaweb { this->items.insert (this->items.cend(), docitem); } // render to HTML from a template - std::string to_html (std::string templatedir); + std::string to_html (Template *t); }; - std::string DocList::to_html (std::string templatedir) { - std::ifstream templ (templatedir + "/doclist.tpl.html"); - std::string templstr ( (std::istreambuf_iterator (templ) ), - (std::istreambuf_iterator ())); + std::string DocList::to_html (Template *t) { + std::string templstr = t->get_doclist_tpl (); - std::string docitems = ""; std::string outputhtml = ""; // if the number of elements is non zero if (this->items.size () != 0) { + std::string docitems = ""; for (DocListItem item : this->items) - docitems += item.to_html (templatedir); - - // Allocate space for output buffer - std::unique_ptr outputstr (new char [ - templstr.size () + - docitems.size () + - this->title.size () - ]); - sprintf (outputstr.get(), templstr.c_str(), - this->title.c_str(), - docitems.c_str() ); - - outputhtml.append (outputstr.get()); + docitems += item.to_html (t); + + outputhtml = fmt::format (templstr, + fmt::arg ("title", this->title), + fmt::arg ("docitems", docitems)); } return outputhtml; }