Introduction:
x:Light is a highlighting engine that takes source code and applies BB-Code to it so that vBulletin based forums can mark up this code with colour. Currently Xaml is only available. This engine only requires the 2.0 framework to run. I also opted to create a custom data type to make the engine cleaner to upgrade as I have plans to remove hard coded regex queries from the engine.
Note:
This engine is still very much in BETA. If you have any suggestions or find any bugs please reply to this thread. Thanks!
Download:
From My SkyDrive
Code:
BBEngine:
VB Code:
Imports xLightEngine.CustomTypes
Imports System.Text.RegularExpressions
Public Class BBEngine
Private mRegexQuery As RegexItem
Private Property RegexQuery() As RegexItem
Get
Return mRegexQuery
End Get
Set(ByVal value As RegexItem)
mRegexQuery = value
End Set
End Property
Public Function Run(ByVal Text As String) As String
Const SourceType As String = "XAML"
Return HighlightCode(Text, SourceType)
End Function
Private Function HighlightCode(ByVal Source As String, ByVal SourceType As String) As String
Dim QueryList As New List(Of RegexItem)
Dim Evaluate As New System.Text.RegularExpressions.MatchEvaluator(AddressOf ElementReplacement)
QueryList.AddRange(GetRegexList(SourceType))
For Each item In QueryList
Me.RegexQuery = item
Source = Regex.Replace(Source, item.Query, Evaluate)
Next
Return "[code]" & Source & "[/code]"
End Function
Private Function GetRegexList(ByVal SourceType As String) As List(Of RegexItem)
Dim RegexList As New List(Of RegexItem)
If SourceType = "XAML" Then
'This is not how the final version of this statement will work, these values will be pulled
'from an XML file and not hard coded into the engine.
Dim GetTags As New RegexItem("</|<|>|/>", "[color=blue]", "[/color]")
Dim GetPropVal As New RegexItem("=""[\w#,. =:/-]+""|=""{|\w+.[\w:-]+(?=\}"")}""", "[color=blue]", "[/color]")
Dim GetPropName As New RegexItem("\b[\w:.]+(?=\="")", "[color=red]", "[/color]")
Dim GetObjName As New RegexItem("(?<=</|<|{)[\w:]+|\w+(?=>)", "[color=darkred]", "[/color]")
RegexList.Add(GetObjName)
RegexList.Add(GetTags)
RegexList.Add(GetPropName)
RegexList.Add(GetPropVal)
End If
Return RegexList
End Function
Public Function ElementReplacement(ByVal m As Text.RegularExpressions.Match) As String
Return Me.RegexQuery.TagFront & m.Value & RegexQuery.TagBehind
End Function
End Class
RegexItem:
VB Code:
Imports System.Text.RegularExpressions
Namespace CustomTypes
Public Class RegexItem
Private mQuery As Regex
''' <summary>
''' Gets or Sets a RegEx query as a string object
''' </summary>
Public Property Query() As String
Get
Return mQuery.ToString
End Get
Set(ByVal value As String)
mQuery = New Regex(value)
End Set
End Property
Private mTagFront As String
''' <summary>
''' Gets or Sets the BB-Code tag that preceeds a text element
''' </summary>
Public Property TagFront() As String
Get
Return mTagFront
End Get
Set(ByVal value As String)
mTagFront = value
End Set
End Property
Private mTagBehind As String
''' <summary>
''' Gets or Sets the BB-Code tag that preceeds a text element
''' </summary>
Public Property TagBehind()
Get
Return mTagBehind
End Get
Set(ByVal value)
mTagBehind = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal GetRegex As String, ByVal GetTagFront As String, ByVal GetTagBehind As String)
Me.Query = GetRegex
Me.TagFront = GetTagFront
Me.TagBehind = GetTagBehind
End Sub
End Class
End Namespace