Skip to content

Instantly share code, notes, and snippets.

@Drinu82
Last active September 16, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drinu82/830d1c9e5acc746ca652 to your computer and use it in GitHub Desktop.
Save Drinu82/830d1c9e5acc746ca652 to your computer and use it in GitHub Desktop.
BlogEngine.NET.Custom.Extensions.Adsense
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"))
return;
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="https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg" />
*/
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");
page.Header.Controls.Add(TwitterCardSummary);
HtmlMeta TwitterCardSite = new HtmlMeta();
TwitterCardSite.Attributes.Add("name", "twitter:site");
TwitterCardSite.Attributes.Add("content", GetUserName());
page.Header.Controls.Add(TwitterCardSite);
HtmlMeta TwitterCardTitle = new HtmlMeta();
TwitterCardTitle.Attributes.Add("name", "twitter:title");
TwitterCardTitle.Attributes.Add("content", GetSummary(post.Title, 70));
page.Header.Controls.Add(TwitterCardTitle);
HtmlMeta TwitterCardDescription = new HtmlMeta();
TwitterCardDescription.Attributes.Add("name", "twitter:description");
TwitterCardDescription.Attributes.Add("content", GetSummary(post.Description, 200));
page.Header.Controls.Add(TwitterCardDescription);
HtmlMeta TwitterCardImage = new HtmlMeta();
TwitterCardImage.Attributes.Add("name", "twitter:image");
TwitterCardImage.Attributes.Add("content", GetFirstImageInArticle(post));
page.Header.Controls.Add(TwitterCardImage);
}
}
}
}
}
/// <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;
else
{
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 "";
}
}
}
@BonganiMoyo
Copy link

This is not Adsenes Its TwitterCard

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