Dave Burke : Freelance .NET Web Developer specializing in Online Communities

CSBlogs : Displaying Code in posts [updated]

A nerd needs to easily display code fragments in his or her posts, and I've found that problematic in CSBlogs.  In dotText I simply highlighted the text in FTB and selected Gray from the color dropdown.  Done.  This doesn't work in CSBlogs, as the post content first goes through the CommunityServer.Components HTMLScrubber.  I love that name.  "HTMLScrubber!"  Like Underdog!!!...or something.

The HTMLScrubber (as its fun name implies) looks through the HTML for acceptable and non-acceptable tags and attributes, based both on hard-coded values in a local (cached) NameValueCollection and in the MarkUp area of the communityserver.config file.  There is a bug in the soup when processing the attributes in the ValidateAttributes() method, because  although FTB passes the correctedly encoded HTML, by the time it passes through the Formatter StripAllTags() process and becomes a post.body string, those quotes are removed. 

The ValidateAttributes() then wants to walk through an attribute string to validate its attributes. It does so by first splitting the string by double-quotes.  D'oh!  No double quotes.  So an input string of FONT face=arial color=#000000 becomes FONT.  Attributes are voted off the island.

So I added some code to put the double-quotes back in before the string is split. 

// DBVT fix.  Looking for DOUBLEQUOTE
if (tagName == "FONT")   // updated.  Other tags like IMG and A were crapping out
{
tagAttributes = tagAttributes.Replace("=","=\"");
tagAttributes = tagAttributes.Replace(" ","\" ");
tagAttributes = tagAttributes + "\"";
}

string[] sa = tagAttributes.Split('"');

There is another way to display code as well if you don't want to update the source.  CODE is an acceptable tag, so you can enclose your code in, um, CODE HTML tags and add it as a page element to your .CSS.  That's what I did here, actually.  I added the source fix after I started writing this post, knowing that there had to be an easy fix I could make to continue to use my FTB color dropdown method of displaying code.

Comments (12) | Post RSS RSS comment feed

Posted on 4/6/2005 9:52:00 AM by Dave Burke
Categories: Community Server
Tags:

Related posts

Comments (12) -

4/6/2005 3:27:59 PM Permalink

Maybe I'm a bigger nerd but just changing all the code to a different color (grey in your case) is not enough for me ;) I think it should be colorized similar to VS. Take a look at CodeHTMLER (http://puzzleware.net/codehtmler/) it will convert your code into colorized html.

I also don't like using the standard Web FTB to post to my blog I use PostXing (projectdistributor.net/.../Project.aspx) which has CodeHTMLer built-in for easy code highlighting. When I get time I may actually add CodeHTMLer as a extension to community server so that we can use special tags to highlight code.

Also on a side note if I'm not mistaken I think there are settings in communityserver.config that control the Markup (i.e which attributes and tags are allowed).

Wes Haggard |

4/6/2005 6:09:19 PM Permalink

Wes, you're right and I hear ya.  I thought of CodeHTMLER which I've used before in dotText (or something similar), and it would have been an option from PostXing, just as you're doing.  In CSBlogs (current build) there's still the double-quote stripping issue as I described when checking the attributes to allow them or not, so it doesn't matter if they're in communityserver.config or not.  I tested a little with using PRE tags, but didn't like the look.  If CodeHTMLER uses PRE, maybe its an option.

I enjoyed your comments a lot.  Maybe you are a bigger nerd than I am, since I'm quite happy with the grey. Smile

daveburke |

4/6/2005 6:39:27 PM Permalink

Careful with the Metablog support in CS. You've got to do a little hacking (which I know you don't mind) to get it working properly. This was a showstopper for me, so I outlined what I did to get it working on my blog, dvbt.com style ;)

Also, if you go with the default configuration for CodeHTMLer you have to enable the