Readme.md - added Section for customization and notes
[biaweb2.git] / biawebrss.hpp
1 #ifndef __BIAWEBRSSFEED__
2 #define __BIAWEBRSSFEED__
3
4 #include "biawebutil.hpp"
5 #include "biawebtemplate.hpp"
6 #include <ctime>
7 #include <list>
8 #include "fmt/format.h"
9
10 namespace biaweb {
11 // class to define and implement an RSS feed item
12 class RSSFeedItem {
13 protected:
14 std::string title;
15 std::string description;
16 std::string url;
17 std::time_t pub_date;
18 public:
19 // Main Constructor
20 RSSFeedItem (std::string title, std::string desc,
21 std::string url, std::time_t pub) {
22 this->title = escape_html (title);
23 this->description = escape_html (desc);
24 this->url = url;
25 this->pub_date = pub;
26 }
27
28 // generate HTML using specified template
29 std::string to_html (Template *t);
30
31 std::string get_title() {
32 return this->title;
33 }
34 void set_title(std::string title) {
35 this->title = escape_html (title);
36 }
37 std::string get_description() {
38 return this->description;
39 }
40 void set_description(std::string description) {
41 this->description = escape_html (description);
42 }
43 std::string get_url() {
44 return this->url;
45 }
46 void set_url(std::string url) {
47 this->url = url;
48 }
49 std::time_t get_pub_date() {
50 return this->pub_date;
51 }
52 void set_pub_date(std::time_t pub_date) {
53 this->pub_date = pub_date;
54 }
55 };
56 std::string RSSFeedItem::to_html (Template *t) {
57 std::string item_tpl = t->get_rss_item_tpl ();
58
59 std::tm ct = *std::localtime (&this->pub_date);
60
61 std::string output = fmt::format (item_tpl,
62 fmt::arg ("title", this->title),
63 fmt::arg ("description", this->description),
64 fmt::arg ("url", this->url),
65 fmt::arg ("pubdate", ct));
66 return output;
67 }
68
69 // class to implement an RSS feed
70 class RSSFeed {
71 protected:
72 std::string title;
73 std::time_t pub_date;
74 std::list<RSSFeedItem> items;
75 public:
76 // add a feed item
77 void add_rss_item (RSSFeedItem item) {
78 this->items.insert (items.cend(), item);
79 }
80
81 // number of items
82 std::size_t get_num_items () {
83 return this->items.size ();
84 }
85
86 std::string get_title () {
87 return this->title;
88 }
89 void set_title (std::string title) {
90 this->title = escape_html (title);
91 }
92
93 std::time_t get_pub_date() {
94 return this->pub_date;
95 }
96 void set_pub_date(std::time_t pub_date) {
97 this->pub_date = pub_date;
98 }
99
100 // output the RSS feed file as feed.xml
101 void output_to_html (Template *t, std::string path) {
102 std::string templ_str = t->get_rss_tpl ();
103 // only if there are feed items, output the feed.xml file
104 if (this->items.size () != 0) {
105 std::string items_str ;
106 // get the items
107 for (auto item : this->items)
108 items_str += item.to_html (t);
109
110 std::tm ct = *std::localtime (&this->pub_date);
111
112 std::string output = fmt::format (templ_str,
113 fmt::arg ("pubdate", ct),
114 fmt::arg ("items", items_str),
115 fmt::arg ("title", this->title));
116 std::ofstream f (path + "/feed.xml");
117 f << output;
118 f.close ();
119 }
120 }
121 };
122 }
123
124 #endif