remwharead  0.8.0
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

virtual 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...
 

Additional Inherited Members

- Protected Member Functions inherited from remwharead::Export::ExportBase
const list< Database::entrysort_entries (list< Database::entry > entries) const
 Sort entries from newest to oldest. More...
 
 ExportBase (const list< Database::entry > &entries, ostream &out=cout)
 Export list of Database::entry. More...
 
- 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.
25  : _entries(sort_entries(entries))
26  , _out(out)
27  {}
const list< Database::entry > sort_entries(list< Database::entry > entries) const
Sort entries from newest to oldest.
Definition: export.cpp:30

◆ print()

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

Print output to std::ostream.

Implements remwharead::Export::ExportBase.

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

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