Changed the rendering code for templating output
[biaweb2.git] / biawebdoclist.hpp
1 #ifndef __BIAWEBDOCITEMLIST__
2 #define __BIAWEBDOCITEMLIST__
3 #include <ctime>
4 #include <string>
5 #include <fstream>
6 #include <iostream>
7 #include <iomanip>
8 #include <list>
9 #include <fmt/format.h>
10 #include <fmt/chrono.h>
11 #include "biawebstrings.hpp"
12 #include "biawebdocument.hpp"
13
14 // to implement a list of items (documents) with creation/modified date/time display
15 namespace biaweb {
16 // to implement a single document item
17 class DocListItem {
18 protected:
19 // Just the required fields to build the item
20 std::string title;
21 std::string url;
22 std::time_t ctime;
23 std::time_t mtime;
24 public:
25 DocListItem (std::string title, std::string url,
26 std::time_t ctime, std::time_t mtime ) {
27 this->title = escape_html (title);
28 this->url = url;
29 this->ctime = ctime;
30 this->mtime = mtime;
31 }
32
33 std::time_t get_mtime() {
34 return this->mtime;
35 }
36 void set_mtime(std::time_t mtime) {
37 this->mtime = mtime;
38 }
39 std::time_t get_ctime() {
40 return this->ctime;
41 }
42 void set_ctime(std::time_t ctime) {
43 this->ctime = ctime;
44 }
45
46 std::string get_url() {
47 return this->url;
48 }
49
50 void set_url(std::string url) {
51 this->url = url;
52 }
53
54 std::string get_title() {
55 return this->title;
56 }
57 void set_title(std::string title) {
58 this->title = escape_html (title);
59 }
60
61 // output to HTML vide the template
62 std::string to_html (std::string templatedir);
63 };
64
65 // output to HTML vide the template
66 std::string DocListItem::to_html (std::string templatedir) {
67 std::ifstream templ (templatedir + "/doclistitem.tpl.html");
68 std::string templstr ((std::istreambuf_iterator<char> (templ)),
69 (std::istreambuf_iterator<char> ()) );
70 templ.close ();
71
72 std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url),
73 fmt::arg("doctitle", this->title),
74 fmt::arg("cdate", *std::localtime (&this->ctime)),
75 fmt::arg("mdate", *std::localtime (&this->mtime)));
76
77 return outputhtml;
78 }
79
80 // to implement a document list (or table)
81 class DocList {
82 protected:
83 std::string title;
84 std::list<DocListItem> items;
85 public:
86 void set_title (std::string title) {
87 this->title = escape_html (title);
88 }
89 // add a document item
90 void add_document_item (DocListItem docitem) {
91 this->items.insert (this->items.cend(), docitem);
92 }
93 // render to HTML from a template
94 std::string to_html (std::string templatedir);
95 };
96
97 std::string DocList::to_html (std::string templatedir) {
98 std::ifstream templ (templatedir + "/doclist.tpl.html");
99 std::string templstr ( (std::istreambuf_iterator<char> (templ) ),
100 (std::istreambuf_iterator<char> ()));
101
102 templ.close ();
103
104 std::string outputhtml = "";
105 // if the number of elements is non zero
106 if (this->items.size () != 0) {
107 std::string docitems = "";
108 for (DocListItem item : this->items)
109 docitems += item.to_html (templatedir);
110
111 outputhtml = fmt::format (templstr,
112 fmt::arg ("title", this->title),
113 fmt::arg ("docitems", docitems));
114 }
115 return outputhtml;
116 }
117 }
118
119 #endif