Esempio n. 1
0
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            XDocument rss             = new XDocument(new XDeclaration("1.0", "UTF-8", null));
            var       rssAbsolutePath = PathHelper.ToLink(System.IO.Path.Combine(_siteRoot, _outputRssFilePath));

            var rssRoot = new XElement("rss",
                                       new XAttribute("version", "2.0"),
                                       new XAttribute(XNamespace.Xmlns + "atom", AtomNamespace)
                                       );

            var channel = new XElement("channel",
                                       new XElement("title", _feedTitle),
                                       new XElement("description", _feedDescription),
                                       new XElement("link", _siteRoot),
                                       new XElement(AtomNamespace + "link",
                                                    new XAttribute("href", rssAbsolutePath),
                                                    new XAttribute("rel", "self")
                                                    ),
                                       new XElement("lastBuildDate", DateTimeHelper.ToRssDate(DateTime.Now))
                                       );

            if (_language != null)
            {
                channel.Add(new XElement("language", _language));
            }

            foreach (var input in inputs)
            {
                var item = new XElement("item");

                object title;
                bool   hasTitle = input.TryGetValue(_titleMetaKey, out title);
                item.Add(new XElement("title", hasTitle ? title : "Untitled"));

                string link = null;
                {
                    object linkTemp = null;
                    if (input.TryGetValue("RssItemUrl", out linkTemp))
                    {
                        link = PathHelper.ToRootLink((string)linkTemp);
                    }
                    else
                    {
                        linkTemp = null;
                        if (input.TryGetValue("RelativeFilePath", out linkTemp))
                        {
                            link = PathHelper.ToRootLink((string)linkTemp);
                            if (_linkCustomizerDelegate != null)
                            {
                                link = _linkCustomizerDelegate(link);
                            }
                        }
                    }
                }

                //foreach (var m in input.Metadata)
                //{
                //    Console.WriteLine("{0}: {1}", m.Key, m.Value);
                //}

                if (string.IsNullOrWhiteSpace(link))
                {
                    throw new ArgumentException("Required RssItemUrl or RelativeFilePath was not found in metadata in document " + input.Source);
                }

                item.Add(new XElement("link", PathHelper.ToLink(_siteRoot + link)));

                object description    = null;
                bool   hasDescription = input.TryGetValue(_descriptionMetaKey, out description);
                item.Add(new XElement("description", hasDescription ? description : "No description"));

                object pubDate = null;
                if (input.TryGetValue(_pubDateMetaKey, out pubDate))
                {
                    item.Add(new XElement("pubDate", DateTimeHelper.ToRssDate(DateTime.Parse((string)pubDate))));
                }

                if (_appendGuid)
                {
                    object guid;
                    bool   hasGuid = input.TryGetValue(_guidMetaKey, out guid);

                    if (hasGuid)
                    {
                        item.Add(new XElement("guid", guid, new XAttribute("isPermaLink", false)));
                    }
                    else
                    {
                        if (_assumePermalinks)
                        {
                            item.Add(new XElement("guid", link));
                        }
                        else if (string.IsNullOrWhiteSpace(input.Source))
                        {
                            Trace.Warning("Cannot generate RSS item guid for document " + input.Source + " because document Source is not valid.");
                        }
                        else
                        {
                            item.Add(new XElement("guid", GuidHelper.ToGuid(input.Source).ToString(), new XAttribute("isPermaLink", false)));
                        }
                    }
                }

                channel.Add(item);
            }

            rss.Add(rssRoot);
            rssRoot.Add(channel);

            // rss.ToString() doesn't return XML declaration
            var outText = new StringBuilder();

            using (var stream = new UTF8StringWriter(outText))
            {
                rss.Save(stream);
            }

            return(new IDocument[] { context.GetDocument(outText.ToString(), new KeyValuePair <string, object>[] {
                    new KeyValuePair <string, object>("IsRssFeed", true),
                    new KeyValuePair <string, object>("RelativeFilePath", _outputRssFilePath),
                    new KeyValuePair <string, object>("WritePath", _outputRssFilePath)
                }) });
        }
Esempio n. 2
0
        public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
        {
            XDocument rss = new XDocument(new XDeclaration("1.0", "UTF-8", null));
            var rssAbsolutePath = PathHelper.ToLink(System.IO.Path.Combine(_siteRoot, _outputRssFilePath));

            var rssRoot = new XElement("rss",
                new XAttribute("version", "2.0"),
                new XAttribute(XNamespace.Xmlns + "atom", AtomNamespace)
            );

            var channel = new XElement("channel",
                new XElement("title", _feedTitle),
                new XElement("description", _feedDescription),
                new XElement("link", _siteRoot),
                new XElement(AtomNamespace + "link",
                    new XAttribute("href", rssAbsolutePath),
                    new XAttribute("rel", "self")
                ),
                new XElement("lastBuildDate", DateTimeHelper.ToRssDate(DateTime.Now))
            );

            if (_language != null)
            {
                channel.Add(new XElement("language", _language));
            }

            foreach (var input in inputs)
            {
                var item = new XElement("item");

                object title;
                bool hasTitle = input.TryGetValue(_titleMetaKey, out title);
                item.Add(new XElement("title", hasTitle ? title : "Untitled"));

                string link = null;
                {
                    object linkTemp = null;
                    if (input.TryGetValue("RssItemUrl", out linkTemp))
                    {
                        link = PathHelper.ToRootLink((string)linkTemp);
                    }
                    else
                    {
                        linkTemp = null;
                        if (input.TryGetValue("RelativeFilePath", out linkTemp))
                        {
                            link = PathHelper.ToRootLink((string)linkTemp);
                            if (_linkCustomizerDelegate != null)
                                link = _linkCustomizerDelegate(link);
                        }
                    }
                }

                //foreach (var m in input.Metadata)
                //{
                //    Console.WriteLine("{0}: {1}", m.Key, m.Value);
                //}

                if (string.IsNullOrWhiteSpace(link))
                {
                    throw new ArgumentException("Required RssItemUrl or RelativeFilePath was not found in metadata in document " + input.Source);
                }

                item.Add(new XElement("link", PathHelper.ToLink(_siteRoot + link)));

                object description = null;
                bool hasDescription = input.TryGetValue(_descriptionMetaKey, out description);
                item.Add(new XElement("description", hasDescription ? description : "No description"));

                object pubDate = null;
                if (input.TryGetValue(_pubDateMetaKey, out pubDate))
                {
                    item.Add(new XElement("pubDate", DateTimeHelper.ToRssDate(DateTime.Parse((string)pubDate))));
                }

                if (_appendGuid)
                {

                    object guid;
                    bool hasGuid = input.TryGetValue(_guidMetaKey, out guid);

                    if (hasGuid)
                    {
                        item.Add(new XElement("guid", guid, new XAttribute("isPermaLink", false)));
                    }
                    else
                    {
                        if (_assumePermalinks)
                        {
                            item.Add(new XElement("guid", link));
                        }
                        else if (string.IsNullOrWhiteSpace(input.Source))
                        {
                            Trace.Warning("Cannot generate RSS item guid for document " + input.Source + " because document Source is not valid.");
                        }
                        else
                        {
                            item.Add(new XElement("guid", GuidHelper.ToGuid(input.Source).ToString(), new XAttribute("isPermaLink", false)));
                        }
                    }
                }

                channel.Add(item);
            }

            rss.Add(rssRoot);
            rssRoot.Add(channel);

            // rss.ToString() doesn't return XML declaration
            var outText = new StringBuilder();
            using (var stream = new UTF8StringWriter(outText))
            {
                rss.Save(stream);
            }

            return new IDocument[] { context.GetDocument(outText.ToString(), new KeyValuePair<string, object>[] {
                new KeyValuePair<string, object>("IsRssFeed", true),
                new KeyValuePair<string, object>("RelativeFilePath", _outputRssFilePath),
                new KeyValuePair<string, object>("WritePath", _outputRssFilePath)
            })};
        }
Esempio n. 3
0
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            XDocument rss     = new XDocument(new XDeclaration("1.0", "UTF-8", null));
            FilePath  rssPath = new DirectoryPath(_siteUri.ToString()).CombineFile(_rssPath);

            XElement rssRoot = new XElement("rss",
                                            new XAttribute("version", "2.0"),
                                            new XAttribute(XNamespace.Xmlns + "atom", AtomNamespace)
                                            );

            XElement channel = new XElement("channel",
                                            new XElement("title", _feedTitle),
                                            new XElement("description", _feedDescription),
                                            new XElement("link", _siteUri.ToString()),
                                            new XElement(AtomNamespace + "link",
                                                         new XAttribute("href", rssPath.FullPath),
                                                         new XAttribute("rel", "self")
                                                         ),
                                            new XElement("lastBuildDate", DateTimeHelper.ToRssDate(DateTime.Now))
                                            );

            if (_language != null)
            {
                channel.Add(new XElement("language", _language));
            }

            foreach (IDocument input in inputs)
            {
                XElement item = new XElement("item");

                object title;
                bool   hasTitle = input.TryGetValue(_titleMetaKey, out title);
                item.Add(new XElement("title", hasTitle ? title : "Untitled"));

                // Get the item link
                FilePath link = input.FilePath(Keys.RssItemUrl)
                                ?? input.FilePath(Keys.RelativeFilePath);
                if (link != null)
                {
                    if (link.IsAbsolute)
                    {
                        link = new DirectoryPath("/").GetRelativePath(link);
                    }
                    if (_linkCustomizerDelegate != null)
                    {
                        link = _linkCustomizerDelegate(link);

                        // Check for absolute one more time in case the customizer returned an absolute path
                        if (link.IsAbsolute)
                        {
                            link = new DirectoryPath("/").GetRelativePath(link);
                        }
                    }
                }
                if (link == null)
                {
                    throw new Exception($"Required metadata keys {Keys.RssItemUrl} or {Keys.RelativeFilePath} were not found in the document {input.SourceString()}");
                }

                FilePath itemLink = new DirectoryPath(_siteUri.ToString()).CombineFile(link);
                item.Add(new XElement("link", itemLink.FullPath));

                object description    = null;
                bool   hasDescription = input.TryGetValue(_descriptionMetaKey, out description);
                item.Add(new XElement("description", hasDescription ? description : "No description"));

                object pubDate = null;
                if (input.TryGetValue(_pubDateMetaKey, out pubDate))
                {
                    item.Add(new XElement("pubDate", DateTimeHelper.ToRssDate(DateTime.Parse((string)pubDate))));
                }

                if (_appendGuid)
                {
                    object guid;
                    bool   hasGuid = input.TryGetValue(_guidMetaKey, out guid);

                    if (hasGuid)
                    {
                        item.Add(new XElement("guid", guid, new XAttribute("isPermaLink", false)));
                    }
                    else
                    {
                        if (_assumePermalinks)
                        {
                            item.Add(new XElement("guid", link));
                        }
                        else if (input.Source == null)
                        {
                            Trace.Warning($"Cannot generate RSS item guid for document {input.SourceString()} because document source is not valid");
                        }
                        else
                        {
                            item.Add(new XElement("guid", GuidHelper.ToGuid(input.Source.FullPath).ToString(), new XAttribute("isPermaLink", false)));
                        }
                    }
                }

                channel.Add(item);
            }

            rss.Add(rssRoot);
            rssRoot.Add(channel);

            // rss.ToString() doesn't return XML declaration
            StringBuilder outText = new StringBuilder();

            using (UTF8StringWriter stream = new UTF8StringWriter(outText))
            {
                rss.Save(stream);
            }

            return(new IDocument[] { context.GetDocument(outText.ToString(), new KeyValuePair <string, object>[] {
                    new KeyValuePair <string, object>(Keys.IsRssFeed, true),
                    new KeyValuePair <string, object>(Keys.RelativeFilePath, _rssPath),
                    new KeyValuePair <string, object>(Keys.WritePath, _rssPath)
                }) });
        }