Jeroen Swart

.NET Architect

BlogEngine.NET code-formatting issues

With a previous post, I had some trouble with the code-formatting in BlogEngine.NET. I didn't have time to investigate, so I quickly fixed it with some inline styling. But planning to post some more code in the future, I decided to look into it today. Quickly I found my problem: I had forgotten to put the language into the code-element. So in that particular case I added ':js' and it all worked fine.

Now I had code-formatting working, but I wasn't very pleased with the results. Having looked at the code, I found the options for adding line-numbers and alternating line-styles. A first issue was that the line-numbers didn't align correctly. I thought it would be a minor styling issue. But then, adding more lines of code, my whole page layout got messed up. After a quick look at the code I found the problem: an alternating row is started with a p-tag, but is ended with a div-tag. This is fixed with a small modification as demonstrated below:

  • Open the SourceFormat.cs file in the App_Code/Extensions/CodeFormatter folder
  • Locate the FormatCode method (the one with parameters source, lineNumbers, alternate, embedStyleSheet & subCode); mine is at line 186
  • In this method, locate the following code (it's near the top of the while-loop):
    if (alternate && ((i % 2) == 1))
    {
        sb.Append("<p class="alt">");
    }
    else
    {
        sb.Append("<div>");
    }
  • Change the p in <p class="alt"> into div, so the code will look like:
    if (alternate && ((i % 2) == 1))
    {
        sb.Append("<div class="alt">");
    }
    else
    {
        sb.Append("<div>");
    }
    
  • Save the changes and upload the file

Another thing I noticed, was that the tabs or spaces in front of a line didn't show. This happens because browsers render multiple spaces as a single space. The code formatter changes a tab into a number of spaces, which I decided to change as follows:

  • In the same SourceFormat.cs, locate the following line (almost at the top of the method):
    sb.Replace("\t", string.Empty.PadRight(_tabSpaces));
    
  • Replace it with the following code:
    StringBuilder tabSpaces = new StringBuilder();
    for (int tabIndex = 0; tabIndex < _tabSpaces; tabIndex++)
    {
        tabSpaces.Append(" ");
    }
    sb.Replace("\t", tabSpaces.ToString());
  • Again, save the changes and upload the file

This only correctly replaces tabs in the code, not spaces. I guess a better solution would be to leave the original Replace, and then replace all leading spaces with &nbsp;. But that's for another time ;-).

BlogEngine.NET tag casing

I'm not sure why, but BlogEngine.NET converts all tags to lowercase. I like my tags to appear as I entered them (like ASP.NET & LINQ), so I decided to change the way my BlogEngine.NET installation handles tags. Luckily, the only needed modification is in the code that stores the tags:

  • Open the Add_entry.aspx.cs file in the admin/Pages folder
  • Locate the btnSave_Click method
  • In this method, locate the following line:
    post.Tags.Add(tag.Trim().ToLowerInvariant());
  • In this line remove the ToLowerInvariant part, changing it into:
    post.Tags.Add(tag.Trim());
  • Save the changes and upload the page
  • Since the tags were stored as lowercase when you added or updated your posts and pages, you need to manually update the tags in the existing posts and pages