Technology, Smartphones, Games


Social Media Share buttons - BlogEngine.NET Extension

Social Media Share buttons - BlogEngine.NET Extension

We have just completed the Upgrading of our blogging platform to the latest BlogEngine.NEt version 2.7.0.0.

I just updated an Extension which I wrote earlier to display the share buttons for Google Plus, Twitter and Facebook to include the Pinterest. So now the extension will display 4 buttons below your post just like the above page.

So if anyone want this extension, just leave a comment, or Save the following code as a class (.cs) file and move it to \App_Code\Extensions

Don’t forget to change the twitterUser  string to your twitter username

using System;
using System.Text;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

/// <summary>
/// Summary description for retweet
/// </summary>
///
[Extension("Inserts Tweet, Facebook, Google Plus and Pinterest share buttons", "1.1", "Geekiest.Net")]
public class retweet
{
   
    private static string _Html;  
    private static string twitterUser = "XXXXXXXXXXXXX"; // Your twitter user name will be used in the retweet

    /// <summary>
    /// Gets the HTML used to inserts the AdSense code.
    /// </summary>
    public static string Html
    {
        get
        {
            if (_Html == null)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(getstr());
                _Html = sb.ToString();
            }
            return _Html;
        }
    }

    public retweet()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(Serving);
        Page.Serving += new EventHandler<ServingEventArgs>(Serving);
    }

    /// <summary>
    /// Serving AdSense code in post and page
    /// </summary>
    private static void Serving(object sender, ServingEventArgs e)
    {
        if ((e.Location == ServingLocation.SinglePost))
        {
            string _adScript = Html;
            Post post = (Post)sender;
            e.Body += _adScript;
            string longUrl=System.Web.HttpUtility.UrlEncode(post.AbsoluteLink.ToString());
            e.Body = e.Body.Replace("[PostURL]",longUrl);
            e.Body = e.Body.Replace("[PostDescription]", post.Title);
            string img = getImage(e.Body);
            e.Body = e.Body.Replace("[PostImage]", System.Web.HttpUtility.UrlEncode(img));
           
        }
        if (e.Location == ServingLocation.SinglePage)
        {
            string _adScript = Html;
            Page page = (Page)sender;
            e.Body += _adScript;
            string longUrl = System.Web.HttpUtility.UrlEncode(page.AbsoluteLink.ToString());
            e.Body = e.Body.Replace("[PostURL]", longUrl);
            e.Body = e.Body.Replace("[PostDescription]", page.Title);
            string img = getImage(e.Body);
            e.Body = e.Body.Replace("[PostImage]", System.Web.HttpUtility.UrlEncode(img));
        }
    }

    private static string getstr()
    {
        string strr = "<div style=\"clear: both;\"></div>"; 
        strr += "<div style=\"float:left\"><script type=\"text/javascript\" src=\"http://apis.google.com/js/plusone.js\"></script> <g:plusone></g:plusone></div>";
        strr += "<div style=\"float:left\"><a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-count=\"horizontal\" data-via=\""+twitterUser+"\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script></div>";
        strr += "<div style=\"float:left;width:80px\"><a href=\"http://pinterest.com/pin/create/button/?url=[PostURL]&media=[PostImage]&description=[PostDescription]\" class=\"pin-it-button\" count-layout=\"horizontal\"><img border=\"0\" src=\"//assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></a></div>";
        strr += "<div style=\"float:left\"><iframe src=\"http://www.facebook.com/plugins/like.php?app_id=226294280715030&amp;href=[PostURL]&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:100px; height:21px;\" allowTransparency=\"true\"></iframe></div>";
         //strr += "</tr></table>";
        strr += "<div style=\"clear: both;\"></div>";
        strr = strr.Replace("\r\n", "");
        strr = strr.Replace("\n", "");
        return (strr);

    }

 

    private static string getImage(string input)
    {

        if (input == null)
            return "";

        string pain = input;
        string pattern = @"<img(.|\n)+?>";

        System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, pattern,

        System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);

        if (m.Success)
        {
            string src = getSrc(m.Value);
            src=src.Replace("src=", "").Replace("\"", "").Replace("\\", "");
            return src;
        }
        else
        {
            return "";
        }
    }

 

    private static string getSrc(string input)
    {
        string pattern = "src=[\'|\"](.+?)[\'|\"]";

        System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern,
        System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);

        System.Text.RegularExpressions.Match mImg = reImg.Match(input);

        if (mImg.Success)
        {
            string a = mImg.Value; 
            return a;
        }

        return "";
    }


}

You are free to Edit/Share with or with out credits Smile