remwharead  0.8.5
Public Member Functions | List of all members
remwharead::Export::RSS Class Reference

Export as RSS feed. More...

#include <remwharead/export/rss.hpp>

Inheritance diagram for remwharead::Export::RSS:
remwharead::Export::ExportBase

Public Member Functions

void print () const override
 Print output to std::ostream. More...
 
 ExportBase (const list< Database::entry > &entries, ostream &out=cout)
 Export list of Database::entry. More...
 
 ExportBase (const ExportBase &)=delete
 
 ExportBase (ExportBase &&)=delete
 

Additional Inherited Members

- Protected Member Functions inherited from remwharead::Export::ExportBase
list< Database::entrysort_entries (list< Database::entry > entries) const
 Sort entries from newest to oldest and remove duplicates. More...
 
 ExportBase (const list< Database::entry > &entries, ostream &out=cout)
 Export list of Database::entry. More...
 
 ExportBase (const ExportBase &)=delete
 
ExportBaseoperator= (const ExportBase &)=delete
 
 ExportBase (ExportBase &&)=delete
 
ExportBaseoperator= (ExportBase &&)=delete
 
- Protected Attributes inherited from remwharead::Export::ExportBase
const list< Database::entry_entries
 
ostream & _out
 

Detailed Description

Export as RSS feed.

Since
0.8.0

Member Function Documentation

◆ ExportBase()

remwharead::Export::ExportBase::ExportBase
explicit

Export list of Database::entry.

Parameters
entriesList of Database::entry to export.
outOutput stream.
23  : _entries(sort_entries(entries))
24  , _out(out)
25 {}
list< Database::entry > sort_entries(list< Database::entry > entries) const
Sort entries from newest to oldest and remove duplicates.
Definition: export.cpp:28

◆ print()

void remwharead::Export::RSS::print ( ) const
overridevirtual

Print output to std::ostream.

Implements remwharead::Export::ExportBase.

40 {
41  try
42  {
43  XMLWriter writer(_out, XMLWriter::CANONICAL);
44  AttributesImpl attrs_rss;
45  AttributesImpl attrs_guid;
46  constexpr char timefmt_rfc822[] = "%w, %d %b %Y %H:%M:%S %Z";
47 
48  attrs_rss.addAttribute("", "", "version", "", "2.0");
49  attrs_rss.addAttribute("", "", "xmlns:atom", "",
50  "http://www.w3.org/2005/Atom");
51  attrs_guid.addAttribute("", "", "isPermaLink", "", "false");
52 
53  writer.startDocument();
54  writer.startElement("", "", "rss", attrs_rss);
55  writer.startElement("", "", "channel");
56 
57  writer.startElement("", "", "title");
58  writer.characters("Visited things");
59  writer.endElement("", "", "title");
60 
61  writer.startElement("", "", "link");
62  writer.endElement("", "", "link");
63 
64  writer.startElement("", "", "description");
65  writer.characters("Export from remwharead.");
66  writer.endElement("", "", "description");
67 
68  writer.startElement("", "", "generator");
69  writer.characters(string("remwharead ") + global::version);
70  writer.endElement("", "", "generator");
71 
72  const string now = DateTimeFormatter::format(DateTime(),
73  timefmt_rfc822);
74  writer.startElement("", "", "lastBuildDate");
75  writer.characters(now);
76  writer.endElement("", "", "lastBuildDate");
77 
78  for (const Database::entry &entry : _entries)
79  {
80  writer.startElement("", "", "item");
81 
82  writer.startElement("", "", "title");
83  if (!entry.title.empty())
84  {
85  writer.characters(entry.title);
86  }
87  else
88  {
89  constexpr std::uint8_t maxlen = 100;
90  string title = entry.description.substr(0, maxlen);
91  if (entry.description.length() > maxlen)
92  {
93  title += " […]";
94  }
95  writer.characters(title);
96  }
97  writer.endElement("", "", "title");
98 
99  writer.startElement("", "", "link");
100  writer.characters(entry.uri);
101  writer.endElement("", "", "link");
102 
103  writer.startElement("", "", "guid", attrs_guid);
104  writer.characters(entry.uri + " at "
105  + timepoint_to_string(entry.datetime));
106  writer.endElement("", "", "guid");
107 
108  const time_t time = system_clock::to_time_t(entry.datetime);
109  const string time_visited = DateTimeFormatter::format(
110  Timestamp::fromEpochTime(time), timefmt_rfc822);
111  writer.startElement("", "", "pubDate");
112  writer.characters(time_visited);
113  writer.endElement("", "", "pubDate");
114 
115  string description = entry.description;
116  if (!description.empty())
117  {
118  description = "<p>" + description.append("</p>");
119  }
120  if (!entry.tags.empty())
121  {
122  description += "<p><strong>Tags:</strong> ";
123  for (const string &tag : entry.tags)
124  {
125  description += tag;
126  if (tag != *(entry.tags.rbegin()))
127  {
128  description += ", ";
129  }
130  }
131  description += "</p>";
132  }
133  if (!entry.archive_uri.empty())
134  {
135  description += "<p><strong>Archived version:</strong> "
136  "<a href=\"" + entry.archive_uri + "\">"
137  + entry.archive_uri + "</a></p>";
138  }
139  writer.startElement("", "", "description");
140  writer.characters(description);
141  writer.endElement("", "", "description");
142 
143  writer.endElement("", "", "item");
144  }
145 
146  writer.endElement("", "", "channel");
147  writer.endElement("", "", "rss");
148  writer.endDocument();
149  _out << endl;
150  }
151  catch (std::exception &e)
152  {
153  cerr << "Error in " << __func__ << ": " << e.what() << endl;
154  }
155 }

The documentation for this class was generated from the following files: