BlogEngine.NET Extension to Clean Up UserControl Tags

Saturday, 28 March 2009 01:18 PM
by Coose

When using the [ usercontrol: …] tag in BlogEngine.NET posts, it really annoys me that they are rendered as literals in the feed "location".  So I wrote this little extension (my first extension, actually), to replace the [ usercontrol: …] tags with something a little better for feeds:

image

(darn you Windows Live Writer!  Why are you continually messing up my thumbnail?)

 

Configured to replace my Media Gallery User Control with the text "View article for this Media Gallery."  It will also accept any other user control file names, and replace with pleasing text.

image 

 

Not very fascinating code:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using BlogEngine.Core.Web.Controls;

using BlogEngine.Core;

using System.Text.RegularExpressions;

using System.Data;

 

[Extension("Fixes [ usercontrol:...] tag in Feeds for configured user controls", "1.0", "Funkymule")]

public class UserControlFeedFixup

{

    private static ExtensionSettings _settings;   

 

    static UserControlFeedFixup()

    {

        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);

 

        ExtensionSettings settings = new ExtensionSettings("UserControlFeedFixup");

        settings.AddParameter("UserControlName", "User Control File Name", 255, true);

        settings.AddParameter("ReplacementText", "Replace with", 255, true);

 

        ExtensionManager.ImportSettings(settings);

        _settings = ExtensionManager.GetSettings("UserControlFeedFixup");

    }

 

    private static void Post_Serving(object sender, ServingEventArgs e)

    {

        if (e.Location == ServingLocation.Feed)

        {

            foreach (DataRow row in _settings.GetDataTable().Rows)

            {

                string pattern = @"\[ usercontrol:.*?/User controls/" + row["UserControlName"] + @".*?\]";

                e.Body = Regex.Replace(e.Body, pattern,

                    row["ReplacementText"].ToString(), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

            }

        }

    }

}

Note: In order to use the above code, you have to remove the space between the opening square bracket and the word "usercontrol:".  If I spelled it correctly, BlogEngine would try to load a user control here in the code…and we don't want that!

Comment on this
Development

Comments (1) -

Torrent Articles | Reply
4/27/2009 4:19:45 PM #
Just wanna say thank you for the codes that you have included in this site!

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading