Pasting code into a Blog.

One reason that I often hear for the apparent inability of certain people to blog is that there is no way for Community Server to display code. It isn’t actually that hard. The obvious choice is to use Phil Factor’s prettifier. It is the reason he wrote it. If you need to show exactly what is in Visual Studio, or SSMS, there is another way.

The problem you have is that Community Server is usually set to only allow FONT tags to enhance text. This makes life difficult, though it ensures that a blogger can’t cause too much havoc with surrounding parts of the page.

Paste your code into Word. Make sure that it has pasted it in, using the ‘keep source formatting’ option. Then, save it as HTML. Alternatively, you can use Outlook. (I set the format of the message to be HTML. I then send the code to myself, right-click the results and select ‘view source code’.)

I then take the HTML code into a nice text editor, ‘top and tail’ it (take out the HTML, BODY tags and the DIVs, leaving just the Ps in place)  and do the following substitutions with the objective of taking out the SPANs (no need really as they don’t get rendered anyway) and changing the Ps into BRs

FROM TO Result
style=’font-size:11.0pt;font-family:”Courier New”‘ Deleted all occurences
</p>rn<p class=MsoNormal <BR (this is RegEx) correct the spacing between lines. replace all paragraph tags with line break tabs

You may find that the font has been styled for a different size, depending on the setting in Visual Studio. This means that yo will need to remove style=’font-size:10.0pt;font-family:”Courier New”‘ rather than 11.0pt.

Then create, or edit, the blog. Paste the modified code into the HTML pane (the tab is on the bottom-right of the panel in Community server) Select the code and, in the design pane, make the size of the code 2 rather than 3. Select the code and specify that it should be Courier

All that we’re actually doing here is trying to remove all the Word silliness of trying to be all things to all men. All you actually need is the colour information and the non-breaking spaces to do the indentation. The final substitution makes the paragraph tag render as you want it to.


ALTER Function [dbo].[ufsRemoveDelimited]
    (
      @String VARCHAR(MAX),
      @OpeningDelimiter CHAR(1),
      @ClosingDelimiter CHAR(1)
    )
RETURNS VARCHAR(8000)
AS BEGIN
    DECLARE @newString VARCHAR(8000)
    IF @OpeningDelimiter = @ClosingDelimiter
        BEGIN
            RETURN NULL
        END
    IF @OpeningDelimiter + @ClosingDelimiter + @String IS NULL
        BEGIN
            RETURN @String
        END
    SELECT  @NewString = ”
    SELECT  @newString = @newString + SUBSTRING(@String, number, 1)
    FROM    numbers
    WHERE   number <= LEN(REPLACE(@string, ‘ ‘, ‘|’))
            AND CHARINDEX(@OpeningDelimiter, @string + @OpeningDelimiter,
                          number) < CHARINDEX(@ClosingDelimiter,
                                              @string + ‘ ‘
                                              + @closingDelimiter, number)
            AND number <> CHARINDEX(@OpeningDelimiter, @string, number)
    RETURN @NewString
   END


Here is some C#, copied out of Visual Studio 2005


// Marshal.cs
using System;
using System.Runtime.InteropServices;
 
class PlatformInvokeTest
{
    [DllImport(“msvcrt.dll”)]
    public static extern int puts(
        [MarshalAs(UnmanagedType.LPStr)]
        string m);
    [DllImport(“msvcrt.dll”)]
    internal static extern int _flushall();
 
 
    public static void Main()
    {
        puts(“Hello World!”);
        _flushall();
    }
}


Here is a small sample of VB code


Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
 
    ‘ Stores the return value.
    Dim RetVal As Integer
    RetVal = MBox(0, “Declare DLL Test”, “Windows API MessageBox”, _
        MB_ICONQUESTION Or MB_YESNO)
 
    ‘ Check the return value.
    If RetVal = IDYES Then
        MsgBox(“You chose Yes”)
    Else
        MsgBox(“You chose No”)
    End If
End Sub