Instantly share code, notes, and snippets.

@Drinu82 /Adsense.cs
Last active Sep 16, 2015

What would you like to do?
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core.Web.Extensions;
using Microsoft.SqlServer.Server;
namespace BlogEngine.NET.Custom.Extensions
[Extension("Twitter card auto generator", "1.0.0", "Adrian Cini")]
public class TwitterCard
public TwitterCard()
if (!ExtensionManager.ExtensionEnabled("TwitterCard"))
Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
/*Sample of Twitter card
* <meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@flickr" />
<meta name="twitter:title" content="Small Island Developing States Photo Submission" />
<meta name="twitter:description" content="View the album on Flickr." />
<meta name="twitter:image" content="" />
public void Post_Serving(object sender, ServingEventArgs e)
//during the loading of a full post
if (e.Location == ServingLocation.SinglePost)
//On page seo for posts only
var post = sender as BlogEngine.Core.Post;
if (post != null)
HttpContext context = HttpContext.Current;
if (context != null)
System.Web.UI.Page page = context.CurrentHandler as System.Web.UI.Page;
if (page != null)
//Add twitter card schema to the page html header
HtmlMeta TwitterCardSummary = new HtmlMeta();
TwitterCardSummary.Attributes.Add("name", "twitter:card");
TwitterCardSummary.Attributes.Add("content", "summary");
HtmlMeta TwitterCardSite = new HtmlMeta();
TwitterCardSite.Attributes.Add("name", "twitter:site");
TwitterCardSite.Attributes.Add("content", GetUserName());
HtmlMeta TwitterCardTitle = new HtmlMeta();
TwitterCardTitle.Attributes.Add("name", "twitter:title");
TwitterCardTitle.Attributes.Add("content", GetSummary(post.Title, 70));
HtmlMeta TwitterCardDescription = new HtmlMeta();
TwitterCardDescription.Attributes.Add("name", "twitter:description");
TwitterCardDescription.Attributes.Add("content", GetSummary(post.Description, 200));
HtmlMeta TwitterCardImage = new HtmlMeta();
TwitterCardImage.Attributes.Add("name", "twitter:image");
TwitterCardImage.Attributes.Add("content", GetFirstImageInArticle(post));
/// <summary>
/// The Twitter @username the card should be attributed to. Required for Twitter Card analytics.
/// </summary>
/// <returns></returns>
private string GetUserName()
//Todo store in config or in extension settings
return "@drinu82";
private string GetSummary(string text, int length)
if (text.Length < length)
return text;
string summary = text.Substring(0, length);
var pos = summary.LastIndexOf(' ');
if (pos >= 0)
summary = summary.Substring(0, pos);
return summary;
private string GetFirstImageInArticle(BlogEngine.Core.Post thisPost)
var htmlString = thisPost.Content;
//<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1
string pattern = @"<\s*img [^\>]*src\s*=\s*([";
pattern += "\"\\'])(.*?)\\1" ;
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(htmlString);
if (matches.Count > 0)
var firstImage = matches[0].ToString();
string[] tmp = firstImage.Split('"');
string relPath = tmp[1].Trim();
string absPath = HttpContext.Current.Server.MapPath(relPath);
return absPath;
return "";

This comment has been minimized.

Show comment
Hide comment

BonganiMoyo Sep 16, 2015

This is not Adsenes Its TwitterCard

BonganiMoyo commented Sep 16, 2015

This is not Adsenes Its TwitterCard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment