public class MyTest{
//private member variables
int mHowMany = 1;
//public properties
public int HowMany {
get { return mHowMany; }
set { mHowMany = value; }
}
}
As you can see from the code snippet above, My code display here in this blog has just taken a step for the better! It all due to the following two links (and a little modification on my part).
SyntaxHighlighter 2.0 SyntaxHighlighter 2.0 (old home)
SyntaxHighlighter for Windows Live Writer
Notice on the top line there is some simple links that will allow you to view the source in a standalone windows all formatting removed, or copy it directly to your clipboard, or print it out.
So SyntaxHighlighter is a script library that allows you to put your code in a <pre> tag, all simple in your source. Then you fill in the class attribute for the type of code it should be formatted, and finally add a Name attribute that is set to “code”, then (with the proper modifications to your display page) at page load javascript will come in and format your code (with the nice action links for you) on the user’s browser. Something like this:
<PRE style="DISPLAY: none" class="c#" name="code">public class MyTest{
//private member variables
int mHowMany = 1;
//public properties
public int HowMany {
get { return mHowMany; }
set { mHowMany = value; }
}
}</PRE>
Of course, out of the box, this won’t work with a SharePoint hosted blog (of course not). The reason why? well SharePoint says “if an attribute for a tag is not part of the tags required or optional attributes, I’m stripping it out!”, and the Pre tag doesn’t support an attribute of “name”. But as you can see from the spec, it DOES allow title, so the fix was easy I just had to modify the source for the SyntaxHighlighter scripts to look for name OR title attribute on a Pre tag, and if set to “code” do it’s thing. Next I had to modify the wlwSyntaxHighlighter plug-in to put in “title” instead of “name”. (Oh yes, I also had to fix the error where the author of the wlw plug-in forget to include a file in his project source code when he refactored it).
So here are the corrections. In the shCore.js modify as follows
Original
function FindTagsByName(list, name, tagName) {
var tags = document.getElementsByTagName(tagName);
for (var i = 0; i < tags.length; i++)
if (tags[i].getAttribute('name') == name)
list.push(tags[i]);
}
New
function FindTagsByName(list, name, tagName) {
var tags = document.getElementsByTagName(tagName);
for (var i = 0; i < tags.length; i++)
if (tags[i].getAttribute('name') == name
|| tags[i].getAttribute('title') == name)
list.push(tags[i]);
}
Then in the wlwSyntaxHighlighter, add the following code as a class called NativeMethods.vb
Public Class NativeMethods
<System.Runtime.InteropServices.DllImport("User32.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal msg As Integer, ByVal wParam As Integer, _
ByVal lParam() As Integer) As IntPtr
End Function
End Class
and finally modify the SyntaxHighlighter.vb’s last four lines in the following manner (please note the ellipses is a placeholder for other necessary code in the function)
Original
Private Function GetPublishHtml(ByVal content As WindowsLive.Writer.Api.ISmartContent) As String
...
' Creating pre tag
Dim code = content.Properties.GetCode.Replace("&", "&").Replace("<", "<").Replace(">", ">")
Dim html = String.Format("<pre name=""code"" class=""{0}"">{1}</pre>", classValue.ToString, code)
Return html
End Function
Modification
Private Function GetPublishHtml(ByVal content As WindowsLive.Writer.Api.ISmartContent) As String
...
' Creating pre tag
Dim code = content.Properties.GetCode.Replace("&", "&").Replace("<", "<").Replace(">", ">")
Dim html = String.Format("<pre title=""code"" class=""{0}"">{1}</pre>", classValue.ToString, code)
Return html
End Function