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 <vladimir@thecybershadow.net> 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; 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 void startFeed(string feedUrl, string title, SysTime updated) 41 { 42 xml.startDocument(); 43 44 xml.startTagWithAttributes!"feed"(); 45 xml.addAttribute!"xmlns"("http://www.w3.org/2005/Atom"); 46 xml.endAttributes(); 47 48 xml.startTagWithAttributes!"link"(); 49 xml.addAttribute!"rel"("self"); 50 xml.addAttribute!"type"("application/atom+xml"); 51 xml.addAttribute!"href"(feedUrl); 52 xml.endAttributesAndTag(); 53 54 putTag!"id"(feedUrl); 55 putTag!"title"(title); 56 putTimeTag!"updated"(updated); 57 } 58 59 void putEntry(string url, string title, string authorName, SysTime time, string contentHtml, string link=null) 60 { 61 xml.startTag!"entry"(); 62 63 putTag!"id"(url); 64 putTag!"title"(title); 65 putTimeTag!"published"(time); 66 putTimeTag!"updated"(time); 67 68 xml.startTag!"author"(); 69 putTag!"name"(authorName); 70 xml.endTag!"author"(); 71 72 if (link) 73 { 74 xml.startTagWithAttributes!"link"(); 75 xml.addAttribute!"href"(link); 76 xml.endAttributesAndTag(); 77 } 78 79 xml.startTagWithAttributes!"content"(); 80 xml.addAttribute!"type"("html"); 81 xml.endAttributes(); 82 xml.text(contentHtml); 83 xml.endTag!"content"(); 84 85 xml.endTag!"entry"(); 86 } 87 88 void endFeed() 89 { 90 xml.endTag!"feed"(); 91 } 92 }