PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/Smilies.cs

#
C# | 86 lines | 49 code | 14 blank | 23 comment | 2 complexity | 3e922b9f5fa21553add6c9e6fe139484 MD5 | raw file
 1#region using
 2
 3using BlogEngine.Core;
 4using BlogEngine.Core.Web.Controls;
 5using BlogEngine.Core.Web.Extensions;
 6
 7#endregion
 8
 9/// <summary>
10/// Converts ASCII smilies into real smilies in the comments.
11/// </summary>
12/// <remarks>
13/// Based on the extension by John Knipper - http://www.happytocode.com
14/// </remarks>
15[Extension("Converts ASCII smilies into real smilies in the comments", "1.3", "BlogEngine.NET")]
16public class Smilies
17{
18    #region Constants and Fields
19
20    /// <summary>
21    /// The link string.
22    /// </summary>
23    private const string Link =
24        "<img src=\"{0}editors/tiny_mce_3_4_3_1/plugins/emotions/img/smiley-{1}.gif\" class=\"flag\" alt=\"{2}\" />";
25
26    #endregion
27
28    #region Constructors and Destructors
29
30    /// <summary>
31    /// Initializes static members of the <see cref="Smilies"/> class.
32    /// </summary>
33    static Smilies()
34    {
35        Comment.Serving += PostCommentServing;
36    }
37
38    #endregion
39
40    #region Methods
41
42    /// <summary>
43    /// Formats the anchor and inserts the right smiley image.
44    /// </summary>
45    /// <param name="name">The name of the link.</param>
46    /// <param name="alt">The alternate string.</param>
47    /// <returns>The link image.</returns>
48    private static string Convert(string name, string alt)
49    {
50        return string.Format(Link, Utils.RelativeWebRoot, name, alt);
51    }
52
53    /// <summary>
54    /// Handles the CommentServing event of the Post control.
55    /// </summary>
56    /// <param name="sender">The source of the event.</param>
57    /// <param name="e">The <see cref="BlogEngine.Core.ServingEventArgs"/> instance containing the event data.</param>
58    private static void PostCommentServing(object sender, ServingEventArgs e)
59    {
60        if (!ExtensionManager.ExtensionEnabled("Smilies"))
61            return;
62
63        if (string.IsNullOrEmpty(e.Body))
64            return;
65
66        e.Body = e.Body.Replace("(H)", Convert("cool", "Cool"));
67        e.Body = e.Body.Replace(":'(", Convert("cry", "Cry"));
68        e.Body = e.Body.Replace(":$", Convert("embarassed", "Embarassed"));
69        e.Body = e.Body.Replace(":|", Convert("foot-in-mouth", "Foot"));
70        e.Body = e.Body.Replace(":(", Convert("frown", "Frown"));
71        e.Body = e.Body.Replace("(A)", Convert("innocent", "Innocent"));
72        e.Body = e.Body.Replace("(K)", Convert("kiss", "Kiss"));
73        e.Body = e.Body.Replace(":D", Convert("laughing", "Laughing"));
74        e.Body = e.Body.Replace("($)", Convert("money-mouth", "Money"));
75        e.Body = e.Body.Replace(":-#", Convert("sealed", "Sealed"));
76        e.Body = e.Body.Replace(":)", Convert("smile", "Smile"));
77        e.Body = e.Body.Replace(":-)", Convert("smile", "Smile"));
78        e.Body = e.Body.Replace(":-O", Convert("surprised", "Surprised"));
79        e.Body = e.Body.Replace(":P", Convert("tongue-out", "Tong"));
80        e.Body = e.Body.Replace("*-)", Convert("undecided", "Undecided"));
81        e.Body = e.Body.Replace(";-)", Convert("wink", "Wink"));
82        e.Body = e.Body.Replace("8o|", Convert("yell", "Yell"));
83    }
84
85    #endregion
86}