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
73 std::list<RSSFeedItem> items;
76 void add_rss_item (RSSFeedItem item) {
77 this->items.insert (items.cend(), item);
81 std::size_t get_num_items () {
82 return this->items.size ();
85 std::time_t get_pub_date() {
86 return this->pub_date;
88 void set_pub_date(std::time_t pub_date) {
89 this->pub_date = pub_date;
92 // output the RSS feed file as feed.xml
93 void output_to_html (Template *t, std::string path) {
94 std::string templ_str = t->get_rss_tpl ();
95 // only if there are feed items, output the feed.xml file
96 if (this->items.size () != 0) {
97 std::string items_str ;
99 for (auto item : this->items)
100 items_str += item.to_html (t);
102 std::tm ct = *std::localtime (&this->pub_date);
104 std::string output = fmt::format (templ_str,
105 fmt::arg ("pubdate", ct),
106 fmt::arg ("items", items_str) );
107 std::ofstream f (path + "/feed.xml");