1 #ifndef __BIAWEBRSSFEED__
2 #define __BIAWEBRSSFEED__
4 #include "biawebutil.hpp"
5 #include "biawebtemplate.hpp"
8 #include "fmt/format.h"
11 // class to define and implement an RSS feed item
15 std::string description;
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);
28 // generate HTML using specified template
29 std::string to_html (Template *t);
31 std::string get_title() {
34 void set_title(std::string title) {
35 this->title = escape_html (title);
37 std::string get_description() {
38 return this->description;
40 void set_description(std::string description) {
41 this->description = escape_html (description);
43 std::string get_url() {
46 void set_url(std::string url) {
49 std::time_t get_pub_date() {
50 return this->pub_date;
52 void set_pub_date(std::time_t pub_date) {
53 this->pub_date = pub_date;
56 std::string RSSFeedItem::to_html (Template *t) {
57 std::string item_tpl = t->get_rss_item_tpl ();
59 std::tm ct = *std::localtime (&this->pub_date);
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));
69 // class to implement an RSS feed
74 std::list<RSSFeedItem> items;
77 void add_rss_item (RSSFeedItem item) {
78 this->items.insert (items.cend(), item);
82 std::size_t get_num_items () {
83 return this->items.size ();
86 std::string get_title () {
89 void set_title (std::string title) {
90 this->title = escape_html (title);
93 std::time_t get_pub_date() {
94 return this->pub_date;
96 void set_pub_date(std::time_t pub_date) {
97 this->pub_date = pub_date;
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 ;
107 for (auto item : this->items)
108 items_str += item.to_html (t);
110 std::tm ct = *std::localtime (&this->pub_date);
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");