/// /// Copyright © 2003-2008 JetBrains s.r.o. /// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder). /// using System.IO; using System.Text; using System.Web; using JetBrains.Omea.Base; using JetBrains.Omea.OpenAPI; namespace JetBrains.Omea.RSSPlugin { internal abstract class BodyWriter { public abstract void AppendText( string text ); } internal class StringBuilderDecor : BodyWriter { private readonly StringBuilder _htmlText; public StringBuilderDecor( string htmlText ) { _htmlText = new StringBuilder( htmlText ); } public override void AppendText( string text ) { _htmlText.Append( text ); } public override string ToString() { return _htmlText.ToString(); } } internal class TextWriterDecor : BodyWriter { private readonly TextWriter _writer; public TextWriterDecor( TextWriter writer ) { Guard.NullArgument( writer, "writer" ); _writer = writer; } public override void AppendText( string text ) { _writer.Write( text ); } } internal abstract class RssBodyConstructor { public static void ConstructSummary( IResource rssItem, string summaryStyle, string content, BodyWriter decor ) { string summary = rssItem.GetPropText( Props.Summary ); bool showSummary = (summary.Length > 0) && (summary.Length != content.Length); if( showSummary ) { decor.AppendText( ""); decor.AppendText( "
" ); decor.AppendText( summary ); decor.AppendText( "
" ); } } public static void AppendLink( IResource item, BodyWriter decor ) { AppendLink( item, decor, null ); } public static void AppendLink( IResource item, BodyWriter decor, string alias ) { if ( item.HasProp( Props.Link ) ) { bool visibleAlias = !string.IsNullOrEmpty( alias ); string link = HttpUtility.HtmlEncode( item.GetStringProp( Props.Link ) ); decor.AppendText( "" ); decor.AppendText( visibleAlias ? HttpUtility.HtmlEncode( alias ) : link ); decor.AppendText( "" ); } } public static void AppendSourceTag( IResource item, BodyWriter decor ) { if ( item.HasProp( Props.RSSSourceTag ) ) { decor.AppendText( "

Source: " ); decor.AppendText( item.GetPropText( Props.RSSSourceTag ) ); decor.AppendText( "

" ); } } public static void AppendCommentsTag( IResource item, BodyWriter decor ) { if ( item.HasProp( Props.CommentURL ) ) { decor.AppendText( "

Comments" ); if ( item.HasProp( Props.CommentCount ) ) { decor.AppendText( " (" ); decor.AppendText( item.GetProp( Props.CommentCount ).ToString() ); decor.AppendText( ")" ); } decor.AppendText( "" ); } } public static void AppendEnclosure( IResource item, BodyWriter decor ) { if ( item.HasProp( Props.EnclosureURL ) ) { decor.AppendText( "

Enclosure: " ); decor.AppendText( enclosureUrl ); if ( item.HasProp( Props.EnclosureSize ) ) { decor.AppendText( " (" ); decor.AppendText( Utils.SizeToString( item.GetIntProp( Props.EnclosureSize ) ) ); decor.AppendText( ")" ); } decor.AppendText( "

" ); } } public static void AppendRelatedPosts( IResource item, BodyWriter decor, bool verbose ) { IResourceList relatedPosts = item.GetLinksOfType( Props.RSSLinkedPostResource, Props.LinkedPost ); if ( relatedPosts.Count > 0 ) { decor.AppendText( "

Related readings:

" ); } } private static void AppendLinkText( IResource res, BodyWriter decor, bool verbose ) { string url = res.GetPropText( Props.URL ); string text = res.GetPropText( Core.Props.Name ); decor.AppendText( "" ); decor.AppendText( verbose ? url : text ); decor.AppendText( "" ); } } }