天天看點

在部落格中顯示不走樣的代碼

  在日常的代碼開發中。習慣于在自己的代碼編輯器裡設定自己喜歡的代碼字型及顔色。在寫部落格的過程中,有不可避免的要粘貼一些自己寫的代碼。問題就來了,往往在部落格中提供的代碼顯示工具顯示的代碼格式和之前在代碼編輯器裡就發生了變化。雖不是什麼大問題,但總是覺得有點不舒服。

  本人用的編輯器是VS系列。一次在無意中将代碼複制後,貼到寫字闆中,發現格式沒有發生變化,将檔案儲存後,用記事本打開,發現是儲存為Rtf格式。也就是說,在VS中複制代碼,實際記憶體中儲存着該代碼的Rtf格式,這樣也就是保留了該代碼的格式。那麼接下來要做的事情就是寫一段代碼,将該Rtf格式的代碼改寫成Html格式的代碼。由于兩種格式之間相似度還是比較高的,是以寫代碼也不是一件很難的事。

  要注意的是,由于該代碼用到了HttpUtility類,是以還得手動添加對System.Web的引用,而不僅僅是添加一句“Imports System.Web”。

  在調用的時候,啟用clsFormatCode.CodeRtfToHtml(Clipboard.GetText(System.Windows.Forms.TextDataFormat.Rtf))這句代碼就可以了。

  下面是代碼,用的是VB2005。

Imports System.Web

Public Class clsFormatCode

  Private Shared Function CodeRtfToHtml(ByVal RtfCode As String, ByVal DefaultFont As String)

    Dim tS As New System.Text.StringBuilder

    RtfCode = RtfCode.Replace(vbNewLine, "")

    tS.AppendLine("<style>")

    Dim I As Integer, J As Integer, K As Integer = 1

    I = RtfCode.IndexOf("{\colortbl;") + 11

    Do While RtfCode.Chars(I + 1) <> "}"

      J = RtfCode.IndexOf(";", I + 1)

      tS.AppendLine(".cf" & K & " {color:" & GetColorHex(RtfCode.Substring(I + 1, J - I)) & "}")

      K += 1

      I = J

    Loop

    tS.AppendLine("</style>")

    tS.AppendLine(DefaultFont)

    I = RtfCode.IndexOf("\fs") + 3

    K = I

    Dim CurColor As String = "\cf0"

    J = RtfCode.IndexOf("\", I)

    Do While J <> -1

      If RtfCode.Substring(J, 2) = "\\" Then

        I = J + 2

      ElseIf RtfCode.Substring(J, 2) = "\{" Then

      ElseIf RtfCode.Substring(J, 2) = "\}" Then

      ElseIf RtfCode.Substring(J, 4) = "\par" Then

        tS.Append(GetText(RtfCode.Substring(K, J - K), CurColor))

        I = J + 5

        K = J + 5

        tS.Append("<br />" & vbNewLine)

      ElseIf RtfCode.Substring(J, 3) = "\cf" Then

        K = RtfCode.IndexOf(" ", J + 1) - J

        CurColor = RtfCode.Substring(J, K)

        I = J + K + 1

        K = I

      Else

        I = J + 1

      End If

      J = RtfCode.IndexOf("\", I)

    tS.Append(GetText(RtfCode.Substring(K, RtfCode.Length - K - 1), CurColor))

    tS.AppendLine("</div>")

    Return tS.ToString

  End Function

  Public Shared Function CodeRtfToHtml(ByVal RtfCode As String, ByVal Font As String, ByVal FontSize As String)

    Return CodeRtfToHtml(RtfCode, String.Format("<div style='color:black;font-family:{0};font-size:{1};'>", Font, FontSize))

  Public Shared Function CodeRtfToHtml(ByVal RtfCode As String) As String

    Return CodeRtfToHtml(RtfCode, "<div style='color:black;font-family:Verdana;font-size:12pt;'>")

  Private Shared Function GetText(ByVal Text As String, ByVal CurColor As String) As String

    Text = Text.Replace("\\", "\")

    Text = Text.Replace("\{", "{")

    Text = Text.Replace("\}", "}")

    Text = Text.Replace("  ", " ")

    If CurColor = "\cf0" Then

      Return HttpUtility.HtmlEncode(Text)

    Else

      Return "<span class='" & CurColor.Substring(1) & "'>" & HttpUtility.HtmlEncode(Text) & "</span>"

    End If

  Private Shared Function GetColorHex(ByVal ColorText As String) As String

    Dim SR As Integer = ColorText.IndexOf("\red")

    Dim SG As Integer = ColorText.IndexOf("\green")

    Dim SB As Integer = ColorText.IndexOf("\blue")

    Dim R As Integer = CInt(ColorText.Substring(SR + 4, SG - SR - 4))

    Dim G As Integer = CInt(ColorText.Substring(SG + 6, SB - SG - 6))

    Dim B As Integer = CInt(ColorText.Substring(SB + 5, ColorText.Length - 1 - SB - 5))

    Return "#" & R.ToString("X2") & G.ToString("X2") & B.ToString("X2")

End Class

繼續閱讀