remwharead  0.8.4
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 and remove duplicates. 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 and remove duplicates.
Definition: export.cpp:30

◆ 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, attrs_guid;
45  constexpr char timefmt_rfc822[] = "%w, %d %b %Y %H:%M:%S %Z";
46 
47  attrs_rss.addAttribute("", "", "version", "", "2.0");
48  attrs_rss.addAttribute("", "", "xmlns:atom", "",
49  "http://www.w3.org/2005/Atom");
50  attrs_guid.addAttribute("", "", "isPermaLink", "", "false");
51 
52  writer.startDocument();
53  writer.startElement("", "", "rss", attrs_rss);
54  writer.startElement("", "", "channel");
55 
56  writer.startElement("", "", "title");
57  writer.characters("Visited things");
58  writer.endElement("", "", "title");
59 
60  writer.startElement("", "", "link");
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  if (!entry.title.empty())
83  {
84  writer.characters(entry.title);
85  }
86  else
87  {
88  constexpr std::uint8_t maxlen = 100;
89  string title = entry.description.substr(0, maxlen);
90  if (entry.description.length() > maxlen)
91  {
92  title += " […]";
93  }
94  writer.characters(title);
95  }
96  writer.endElement("", "", "title");
97 
98  writer.startElement("", "", "link");
99  writer.characters(entry.uri);
100  writer.endElement("", "", "link");
101 
102  writer.startElement("", "", "guid", attrs_guid);
103  writer.characters(entry.uri + " at " +
104  timepoint_to_string(entry.datetime));
105  writer.endElement("", "", "guid");
106 
107  const time_t time = system_clock::to_time_t(entry.datetime);
108  const string time_visited = DateTimeFormatter::format(
109  Timestamp::fromEpochTime(time), timefmt_rfc822);
110  writer.startElement("", "", "pubDate");
111  writer.characters(time_visited);
112  writer.endElement("", "", "pubDate");
113 
114  string description = entry.description;
115  if (!description.empty())
116  {
117  description = "<p>" + description + "</p>";
118  }
119  if (!entry.tags.empty())
120  {
121  description += "<p><strong>Tags:</strong> ";
122  for (const string &tag : entry.tags)
123  {
124  description += tag;
125  if (tag != *(entry.tags.rbegin()))
126  {
127  description += ", ";
128  }
129  }
130  description += "</p>";
131  }
132  if (!entry.archive_uri.empty())
133  {
134  description += "<p><strong>Archived version:</strong> "
135  "<a href=\"" + entry.archive_uri + "\">"
136  + entry.archive_uri + "</a>";
137  }
138  writer.startElement("", "", "description");
139  writer.characters(description);
140  writer.endElement("", "", "description");
141 
142  writer.endElement("", "", "item");
143  }
144 
145  writer.endElement("", "", "channel");
146  writer.endElement("", "", "rss");
147  writer.endDocument();
148  _out << endl;
149  }
150  catch (std::exception &e)
151  {
152  cerr << "Error in " << __func__ << ": " << e.what() << endl;
153  }
154  }
struct remwharead::Database::entry entry
Describes a database entry.

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