Separated the strings used for document tree generation from code
[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::tm c, m;
73 c = *std::localtime (&this->ctime);
74 m = *std::localtime (&this->mtime);
75
76 std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url),
77 fmt::arg("doctitle", this->title),
78 fmt::arg("cdate", c),
79 fmt::arg("mdate", m));
80
81 return outputhtml;
82 }
83
84 // to implement a document list (or table)
85 class DocList {
86 protected:
87 std::string title;
88 std::list<DocListItem> items;
89 public:
90 void set_title (std::string title) {
91 this->title = escape_html (title);
92 }
93 // add a document item
94 void add_document_item (DocListItem docitem) {
95 this->items.insert (this->items.cend(), docitem);
96 }
97 // render to HTML from a template
98 std::string to_html (std::string templatedir);
99 };
100
101 std::string DocList::to_html (std::string templatedir) {
102 std::ifstream templ (templatedir + "/doclist.tpl.html");
103 std::string templstr ( (std::istreambuf_iterator<char> (templ) ),
104 (std::istreambuf_iterator<char> ()));
105
106 templ.close ();
107
108 std::string outputhtml = "";
109 // if the number of elements is non zero
110 if (this->items.size () != 0) {
111 std::string docitems = "";
112 for (DocListItem item : this->items)
113 docitems += item.to_html (templatedir);
114
115 outputhtml = fmt::format (templstr,
116 fmt::arg ("title", this->title),
117 fmt::arg ("docitems", docitems));
118 }
119 return outputhtml;
120 }
121 }
122
123 #endif