1 /**
2  * RSS/ATOM feed generation
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.utils.feed;
15 
16 import std.datetime;
17 
18 import ae.utils.xmlwriter;
19 import ae.utils.time;
20 
21 /// ATOM writer
22 struct AtomFeedWriter
23 {
24 	XmlWriter xml; /// Target XML writer.
25 
26 	private void putTag(string name)(string content)
27 	{
28 		xml.startTag!name();
29 		xml.text(content);
30 		xml.endTag!name();
31 	}
32 
33 	private void putTimeTag(string name)(SysTime time)
34 	{
35 		xml.startTag!name();
36 		.putTime!(TimeFormats.ATOM)(xml.output, time);
37 		xml.endTag!name();
38 	}
39 
40 	/// Start writing.
41 	void startFeed(string feedUrl, string title, SysTime updated)
42 	{
43 		xml.startDocument();
44 
45 		xml.startTagWithAttributes!"feed"();
46 		xml.addAttribute!"xmlns"("http://www.w3.org/2005/Atom");
47 		xml.endAttributes();
48 
49 		xml.startTagWithAttributes!"link"();
50 		xml.addAttribute!"rel"("self");
51 		xml.addAttribute!"type"("application/atom+xml");
52 		xml.addAttribute!"href"(feedUrl);
53 		xml.endAttributesAndTag();
54 
55 		putTag!"id"(feedUrl);
56 		putTag!"title"(title);
57 		putTimeTag!"updated"(updated);
58 	}
59 
60 	/// Add an entry.
61 	void putEntry(string url, string title, string authorName, SysTime time, string contentHtml, string link=null)
62 	{
63 		xml.startTag!"entry"();
64 
65 		putTag!"id"(url);
66 		putTag!"title"(title);
67 		putTimeTag!"published"(time);
68 		putTimeTag!"updated"(time);
69 
70 		xml.startTag!"author"();
71 		putTag!"name"(authorName);
72 		xml.endTag!"author"();
73 
74 		if (link)
75 		{
76 			xml.startTagWithAttributes!"link"();
77 			xml.addAttribute!"href"(link);
78 			xml.endAttributesAndTag();
79 		}
80 
81 		xml.startTagWithAttributes!"content"();
82 		xml.addAttribute!"type"("html");
83 		xml.endAttributes();
84 		xml.text(contentHtml);
85 		xml.endTag!"content"();
86 
87 		xml.endTag!"entry"();
88 	}
89 
90 	/// Finish writing.
91 	void endFeed()
92 	{
93 		xml.endTag!"feed"();
94 	}
95 }