Readme.md - added Section for customization and notes
[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 #include "biawebtemplate.hpp"
14
15 // to implement a list of items (documents) with creation/modified date/time display
16 namespace biaweb {
17 // to implement a single document item
18 class DocListItem {
19 protected:
20 // Just the required fields to build the item
21 std::string title;
22 std::string url;
23 std::string desc;
24 std::time_t ctime;
25 std::time_t mtime;
26 public:
27 DocListItem (Document *doc, std::string urlpath) {
28 this->title = doc->get_title ();
29 this->url = urlpath + doc->get_filename() + ".html";
30 this->ctime = doc->get_creation_date ();
31 this->mtime = doc->get_modified_date ();
32 this->desc = doc->get_meta_desc ();
33 }
34
35 std::string get_desc () {
36 return this->desc;
37 }
38
39 std::time_t get_mtime() {
40 return this->mtime;
41 }
42
43 std::time_t get_ctime() {
44 return this->ctime;
45 }
46
47 std::string get_url() {
48 return this->url;
49 }
50
51 std::string get_title() {
52 return this->title;
53 }
54
55 // output to HTML vide the template
56 std::string to_html (Template *t);
57 };
58
59 // output to HTML vide the template
60 std::string DocListItem::to_html (Template *t) {
61 std::string templstr = t->get_doclistitem_tpl ();
62 std::tm c, m;
63 c = *std::localtime (&this->ctime);
64 m = *std::localtime (&this->mtime);
65
66 std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url),
67 fmt::arg("doctitle", this->title),
68 fmt::arg("desc", this->desc),
69 fmt::arg("cdate", c),
70 fmt::arg("mdate", m));
71
72 return outputhtml;
73 }
74
75 // to implement a document list (or table)
76 class DocList {
77 protected:
78 std::string title;
79 std::list<DocListItem> items;
80 public:
81 void set_title (std::string title) {
82 this->title = title;
83 }
84 // add a document item
85 void add_document_item (DocListItem docitem) {
86 this->items.insert (this->items.cend(), docitem);
87 }
88 // render to HTML from a template
89 std::string to_html (Template *t);
90 };
91
92 std::string DocList::to_html (Template *t) {
93 std::string templstr = t->get_doclist_tpl ();
94
95 std::string outputhtml = "";
96 // if the number of elements is non zero
97 if (this->items.size () != 0) {
98 std::string docitems = "";
99 for (DocListItem item : this->items)
100 docitems += item.to_html (t);
101
102 outputhtml = fmt::format (templstr,
103 fmt::arg ("title", this->title),
104 fmt::arg ("docitems", docitems));
105 }
106 return outputhtml;
107 }
108 }
109
110 #endif