Pragmatic Developer

Ali Özgür

Bookmark Blog

Add to Technorati Favorites

Google Talk

Chat with Ali Özgür

Purchase PragmaSQL from

Calendar

«  December 2008  »
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar

Tag Cloud

Don't show

    Authors

    Recent Comments

    Banners




    BlogEngine .NET 1.3 uses www.gravatar.com/avatar.php?gravatar_id=[MD5 Hash] to retreive Gravatar images for the comments. But this url is not supported by Gravatar anymore as far as I understand. We have to replace this url with this one http://en.gravatar.com/avatar/.

    In order correct this url open CommentView.ascx.cs file found in "User controls" folder and replace the source code of the Gravatar method with this one

    001  protected string Gravatar(string email, string name, int size)
    002  {
    003    if (email.Contains("://"))
    004      return
    005          string.Format(
    006              "<img class=\"thumb\" src=\"http://images.websnapr.com/?url={0}&amp;size=t\" alt=\"{1}\" />", name,
    007              email);
    008    //http://www.artviper.net/screenshots/screener.php?&url={0}&h={1}&w={1}
    009    MD5 md5 = new MD5CryptoServiceProvider();
    010    byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(email));
    011
    012    StringBuilder hash = new StringBuilder();
    013    for (int i = 0; i < result.Length; i++)
    014      hash.Append(result[i].ToString("x2"));
    015
    016    StringBuilder image = new StringBuilder();
    017    image.Append("<img src=\"");
    018        
    019    //Change the url
    020    image.Append("http://en.gravatar.com/avatar/");
    021        
    022    //Change the MD5 hash appending code
    023    image.Append(hash.ToString());
    024    
    025    image.Append("&amp;rating=G");
    026    image.Append("&amp;size=" + size);
    027    image.Append("&amp;default=");
    028    image.Append(Server.UrlEncode(Utils.AbsoluteWebRoot + "themes/" + BlogSettings.Instance.Theme + "/noavatar.jpg"));
    029    image.Append("\" alt=\"\" />");
    030    return image.ToString();
    031  }

    After changing the url in source code you do not nedd to build/recompile BE you simply replace the old file with the modified one. 


    Posted in: BlogEngine.NET  Tags:

    Currently rated 5.0 by 1 people

    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    BlogEngine.NET version 1.3 has a syntax highlighting extension included but is in beta, so I looked around for another syntax highlighter, since this blog heavly uses code snippets. After looking for a while I found this syntax highlighter extension. This extension uses Wilco Bauwer's syntax highlighter library which is impressive. But the main problem with this extension is, it does not handle HTML tags and special HTML characters like &nbsp &lt and &gt very well. Tags and special characters are left as garbage after extension tries to highlight the code. You see something like this

    private&nbsp;voidTest&nbsp{

    <p>&nbsp;private&nbsp;int&nbsp;i=0;

    <p>}

    After inspecting SyntaxHighlightingExtension.cs file I saw that the extension matches the source code with a regular epression and feeds the Wilco's highlighter with the raw html (body). This was kind of incomlete implementation causing the side effect I mentioned above. We need to clean html tags and special characters from the raw html(body). So I changed Highlight method of the extension. The resulting method is something like this.

    001 private string Highlight(HighlightOptions options)
    002 {
    003  string parsed;
    004  uint id = NextCodeID();
    005  string name = options.Language; 
    006
    007
    008  HighlighterBase highlighter = GetHighlighter(name);
    009  if (highlighter != null)
    010  {
    011   name = highlighter.FullName;
    012   highlighter.Parser = htmlParser;
    013   
    014   string body = Regex.Replace(options.Code,@"<(?![!/]?[>\s])[^>]*>",String.Empty,RegexOptions.CultureInvariant| RegexOptions.IgnoreCase | RegexOptions.Singleline);  
    015   body = HttpUtility.HtmlDecode(body);
    016   parsed = highlighter.Parse(body);
    017   highlighter.ForceReset();
    018  }
    019  else
    020  {
    021   name += " (not highlighted)";
    022   parsed = options.Code;
    023  } 
    024
    025
    026  if (options.DisplayLineNumbers)
    027  {
    028   string[] lines = parsed.Split(new char[] { '\n' });
    029   StringBuilder outputBuffer = new StringBuilder(); 
    030
    031
    032   for (int i = 0; i < lines.Length; i++)
    033   {
    034    outputBuffer.AppendFormat(linenumberingTemplate, i+1, lines[i]);
    035   } 
    036
    037
    038   return string.Format(OutputTemplate, id, name, options.Title, outputBuffer);
    039  } 
    040
    041
    042  return string.Format(OutputTemplate, id, name, options.Title, parsed);
    043 }

    The change I made resides starting from line 12 and ending in line 21. I simply stripped out the html tags with a regular expression and then used HttpUtility.Decode method to decode special HTML characters and feed the parser with normalized body text and voila the extension started performing well.

    By the way I want to remind you that if you are using the default editor (TinyMCE) you should copy and paste your source code to a plain text editor like Notepad++ and the copy from Notepad++ and paste to TinyMCE. I think TinyMCE should consider to add something like Paste as PlainText functionality to their editor like FCKEditor.


    Posted in: BlogEngine.NET  Tags:

    Currently rated 5.0 by 2 people

    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5