Click here to monitor SSC

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
 

Pretty code samples in blogger.com blogs. | Michael J. Swart said:

November 16, 2009 8:11 AM
You need to sign in to comment on this blog
<July 2008>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across, and start getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...