Inside the tent....

Occasional Editorial announcements.

Pasting code into a Blog.

Published Tuesday, July 15, 2008 8:45 AM

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>\r\n<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

Comments

 

Jason Haley said:

July 16, 2008 8:31 AM
You need to sign in to comment on this blog

















<July 2008>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Using Powershell to Generate Table-Creation Scripts
 For all of us who learn best by trying out examples, Bob Sheldon produces a PowerShell script file for... Read more...

Configuring Exchange Server 2007 to Support Information Rights Management
 In Exchange Server 2007, Information Rights management is easy to set up once you have set up the... Read more...

SQL Response: The dim sum interview
 Richard Morris met David and Nigel of the SQL Response team, in a dim sum Restaurant in Cambridge. They... Read more...

Why This SQL Server DBA is Learning Powershell
 Ron describes how he decided to study Powershell as a single scripting system to automate all the... Read more...

Using Covering Indexes to Improve Query Performance
 Designers of database systems will often assume that the use of a clustered index is always the best... Read more...