'Replaces tags in text and returns the result.
Private Function replacetags(ByVal text As String, ByVal dict As Dictionary(Of String, String)) As String
Dim parsedtext As New StringBuilder
Dim indexst, indexgt, curindex As Integer
Dim tag As String
'Reset current progress and get the location of first "<"c
curindex = 0
indexst = text.IndexOf("<"c)
While indexst > 0
'Append the text before the "<"c to the result.
parsedtext.Append(text.Substring(curindex, indexst - curindex))
'Find the first ">"c after the found "<"c
indexgt = text.IndexOf(">"c, indexst)
'If no ">"c is found, we're done.
If indexgt < 0 Then
'Append the rest of the text to the result
parsedtext.Append(text.Substring(indexst))
Return parsedtext.ToString
End If
'Retrieve the text between "<"c and ">"c
tag = text.Substring(indexst + 1, indexgt - indexst - 1)
'Is the tag in the dictionary?
If dict.ContainsKey(tag) Then
'Append the text associated with the tag to the result.
parsedtext.Append(dict(tag))
Else
'Append <tag> to the result
parsedtext.Append(text.Substring(indexst, indexgt - indexst + 1))
End If
'Update the current progress within the string
curindex = indexgt + 1
'Find index of "<"c after the current index
indexst = text.IndexOf("<"c, curindex)
End While
'Append the last bit of text.
parsedtext.Append(text.Substring(curindex))
Return parsedtext.ToString
End Function
'Reads tags and descriptions from file and organizes them into a dictionary ordered by the tag.
Private Function readtags(ByVal filename As String) As Dictionary(Of String, String)
Static parse As Regex = New Regex("^\<(?<tag>[^\>]+)\>\s*=\s*(?<desc>.+)$", RegexOptions.Compiled)
Dim lines() As String = File.ReadAllLines(filename)
Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim m As Match
For Each line As String In lines
'Try to validate the line.
m = parse.Match(line)
'Was line valid?
If m.Success Then
'Get tag and description
Dim tag As String = m.Groups(1).Captures(0).Value
Dim desc As String = m.Groups(2).Captures(0).Value
'Add the entry to the dictionary, unless an entry with tht tag is already present.
If Not dict.ContainsKey(tag) Then dict.Add(tag, desc)
End If
Next
Return dict
End Function