Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Creating Markup Text in Visual Basic .NET

Wow, what a happy day to come in and see that my Mark-up article has been published!

    http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dv_vstechart/html/vbmarkup.asp

I'd like to thank Scott Mitchell for providing me with a sanity check during the authoring process :-)

There's a cool little app that I wrote with this article that allows you to create colorized code snippets from text, the download link is at the top of the article and comes with full source code :-)  I've been using it for over 6 months to colorize the code for my weblog entries.  It's great because, the tool allows you to emit a stylesheet (see mine here: http://www.flws.com.au/Languages.css ) and produce the marked-up output in Css format.  So, I can change the color, font, etc of all of my code snippets by simply altering that stylesheet!

Here's an example of text that has been marked-up using the tool and referencing that stylesheet:


Passing matches to a MatchEvaluator delegate

    ' important to ensure that we are only tokenizing parts of the snippet that 
    ' haven't as yet been tokenized
    Private m_AvailablePartPattern As String = _
        "(?'available'<available>[^\<]*[\w\W\s\S]*?<\/available>)"

    ' matches the "available" text and hands it off to a delegate for further inspection.
    Public Sub TokenizeWordElements( _
        ByVal patternName As String, _
        ByVal regexString As String, _
        ByVal _caseSensitive As Boolean, _
        ByRef codeSnippet As String _
    )
        m_CaseSensitive = _caseSensitive
        m_Name = patternName
        m_REString = regexString
        Dim _delegate As New MatchEvaluator(AddressOf WordElementMatchHandler)
        Dim r As New Regex(m_AvailablePartPattern, _
                RegexOptions.IgnoreCase Or _
                RegexOptions.Compiled _
            )
        codeSnippet = r.Replace(codeSnippet, _delegate)
    End Sub

    Private Function WordElementMatchHandler(ByVal _match As Match) As String
        ' if, for some reason no group was found... bail out
        If m_Name Is String.Empty Then Return _match.Value
        Dim opts As RegexOptions = RegexOptions.Multiline
        If Not m_CaseSensitive Then
            opts = opts Or RegexOptions.IgnoreCase
        End If
        Dim re As New Regex(m_REString, opts)
        ' tokenize the match
        Return re.Replace(_match.Value, "</available><" & m_Name & ">$1</" & _
            M_Name & "><available>")
    End Function

2 Comments

Comments have been disabled for this content.