Skip to content

Instantly share code, notes, and snippets.

@Drinu82
Last active August 29, 2015 14:24
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/7579665601389a54cd51 to your computer and use it in GitHub Desktop.
Save Drinu82/7579665601389a54cd51 to your computer and use it in GitHub Desktop.
Twitter card auto generator
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;
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", post.Description);
page.Header.Controls.Add(TwitterCardSummary);
HtmlMeta TwitterCardSite = new HtmlMeta();
TwitterCardSite.Attributes.Add("name", "twitter:site");
TwitterCardSite.Attributes.Add("content", post.AbsoluteLink.ToString());
page.Header.Controls.Add(TwitterCardSite);
HtmlMeta TwitterCardTitle = new HtmlMeta();
TwitterCardTitle.Attributes.Add("name", "twitter:title");
TwitterCardTitle.Attributes.Add("content", post.Title);
page.Header.Controls.Add(TwitterCardTitle);
HtmlMeta TwitterCardDescription = new HtmlMeta();
TwitterCardDescription.Attributes.Add("name", "twitter:description");
TwitterCardDescription.Attributes.Add("content", post.Description);
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);
}
}
}
}
}
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 "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment