love to code .net

blogging mostly about one of my favorite subjects, programming

Month List


BlogEngine.NET code formatter extension shows code tag

While evaluating BlogEngine.NET, I noticed the code tag was shown on the first and last line of the formatted code block.  At first, I thought I was using incorrect syntax.  After I checked my syntax and found this post, where you can see the open and close code tag, I realized it was an problem.  So I looked around and found the cause of the problem.  Here's the method that causes the problem.

1: private string CodeEvaluator(Match match)
2: {
3:     if (!match.Success)
4:         return match.Value;
5:
6:     HighlightOptions options = new HighlightOptions();
7:
8:     options.Language = match.Groups["lang"].Value;
9:     options.Code = match.Groups["code"].Value;
10:     options.DisplayLineNumbers = match.Groups["linenumbers"].Value == "on" ? true : false;
11:     options.Title = match.Groups["title"].Value;
12:     options.AlternateLineNumbers = match.Groups["altlinenumbers"].Value == "on" ? true : false;
13:
14:     return Highlight(options, match.Value);
15: }

The problem is caused by line 14.  match.Value is all of the text including the code extension tag declaration.  Since we only want the code we have to remove the code extension start and end tags.  Luckily this has already been done and the value is stored in options.Code.  The solution is to change match.Value to options.Code.  Here is the corrected method.

1: private string CodeEvaluator(Match match)
2: {
3:     if (!match.Success)
4:         return match.Value;
5:
6:     HighlightOptions options = new HighlightOptions();
7:
8:     options.Language = match.Groups["lang"].Value;
9:     options.Code = match.Groups["code"].Value;
10:     options.DisplayLineNumbers = match.Groups["linenumbers"].Value == "on" ? true : false;
11:     options.Title = match.Groups["title"].Value;
12:     options.AlternateLineNumbers = match.Groups["altlinenumbers"].Value == "on" ? true : false;
13:
14:     return Highlight(options, options.Code);
15: }

Here is the updated file for download.  The code in this post is released under the Microsoft Reciprocal License, which is the license BlogEngine.NET uses.

kick it on DotNetKicks.com

Comments are closed