You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
411 lines
15 KiB
411 lines
15 KiB
'**************************************************
|
|
' FILE : Utils.vb
|
|
' AUTHOR : Paulo Santos
|
|
' CREATION : 4/29/2009 2:14:26 PM
|
|
' COPYRIGHT : Copyright © 2009
|
|
' PJ on Development
|
|
' All Rights Reserved.
|
|
'
|
|
' Description:
|
|
' TODO: Add file description
|
|
'
|
|
' Change log:
|
|
' 0.1 4/29/2009 2:14:26 PM
|
|
' Paulo Santos
|
|
' Created.
|
|
'***************************************************
|
|
|
|
Imports System.Text.RegularExpressions
|
|
Imports System.Diagnostics.CodeAnalysis
|
|
|
|
Friend Module Utils
|
|
|
|
Public Const STR_BBCodeSchemaNamespace As String = "" '= "http://pjondevelopment.50webs.com/schema/bbcode"
|
|
Public Const STR_BBCodeDictionaryXmlElement As String = "Dictionary"
|
|
Public Const STR_BBCodeElementTypesXmlElement As String = "ElementTypes"
|
|
Public Const STR_BBCodeConfigurationXmlElement As String = "Configuration"
|
|
|
|
''' <summary>
|
|
''' Returns the specified text between quotes.
|
|
''' </summary>
|
|
''' <param name="text">The text to be placed between quotes.</param>
|
|
''' <returns>The text between quotes.</returns>
|
|
Public Function Quote(ByVal text As String) As String
|
|
Return "'" & text.Replace("'", "''") & "'"
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Unquotes the specified text.
|
|
''' </summary>
|
|
''' <param name="text">The text to be unquoted.</param>
|
|
''' <returns>The unquoted text.</returns>
|
|
Public Function UnQuote(ByVal text As String) As String
|
|
Dim rx As New Text.RegularExpressions.Regex("^(?<quote>'(?<text>(?:''|[^'])*)')|(?<doubleQuote>""(?<text>(?:""""|[^""])*)"")$")
|
|
Dim m = rx.Match(text)
|
|
If (Not m.Success) Then
|
|
Return text
|
|
End If
|
|
If (m.Groups("quote").Success) Then
|
|
Return m.Groups("text").Value.Replace("''", "'")
|
|
End If
|
|
Return m.Groups("text").Value.Replace("""""", """")
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Validates the specified tag name.
|
|
''' </summary>
|
|
''' <param name="tagName">The tagname to be validated.</param>
|
|
Public Sub ValidateTagName(ByVal tagName As String)
|
|
If (String.IsNullOrEmpty(tagName)) Then
|
|
Throw New ArgumentNullException("tagName")
|
|
End If
|
|
If (tagName.IndexOf("=", StringComparison.Ordinal) <> -1) OrElse (tagName.IndexOf("[", StringComparison.Ordinal) <> -1) OrElse (tagName.IndexOf("]", StringComparison.Ordinal) <> -1) Then
|
|
Throw New ArgumentException("Invalid tag name. The tag name cannot contain '=' '[' or ']'.", "tagName")
|
|
End If
|
|
End Sub
|
|
|
|
''' <summary>
|
|
''' Validates the specified type to ensure that it is a subclass of <see cref="BBCodeElement"/>.
|
|
''' </summary>
|
|
''' <param name="value">The <see cref="Type"/> to be validated.</param>
|
|
Public Sub ValidateBBCodeElementType(Of TContext As Class)(ByVal value As Type)
|
|
|
|
'*
|
|
'* Validate is nothing
|
|
'*
|
|
If (value Is Nothing) Then
|
|
Throw New ArgumentNullException("value")
|
|
End If
|
|
|
|
'*
|
|
'* Validates the BBCodeElement itself
|
|
'*
|
|
Dim bbcodeType = GetType(BBCodeElement(Of TContext))
|
|
If (value.Equals(bbcodeType)) Then
|
|
Exit Sub
|
|
End If
|
|
|
|
'*
|
|
'* Validate subclass
|
|
'*
|
|
If Not bbcodeType.IsAssignableFrom(value) Then
|
|
Throw New InvalidOperationException("The type " & value.FullName & " must be a assingable to BBCodeElement.")
|
|
End If
|
|
|
|
'*
|
|
'* Validate default constructor
|
|
'*
|
|
If (value.GetConstructor(New Type() {}) Is Nothing) Then
|
|
Throw New InvalidOperationException("The type " & value.FullName & " does not provide a public default constructor.")
|
|
End If
|
|
|
|
End Sub
|
|
|
|
''' <summary>
|
|
''' Encodes the specified text as HTML.
|
|
''' </summary>
|
|
''' <param name="text">The text to be encoded.</param>
|
|
''' <returns>The encoded HTML.</returns>
|
|
<SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode", Justification:="This methos is simple a list of substuition character by its HTML representation.")> _
|
|
Public Function HtmlEncode(ByVal text As String) As String
|
|
Dim sb As New Text.StringBuilder(text)
|
|
sb.Replace(ChrW(&H26), "&")
|
|
sb.Replace(ChrW(&H22), """)
|
|
sb.Replace(ChrW(&H27), "'")
|
|
sb.Replace(ChrW(&H3C), "<")
|
|
sb.Replace(ChrW(&H3E), ">")
|
|
sb.Replace(ChrW(&HA0), " ")
|
|
sb.Replace(ChrW(&HA1), "¡")
|
|
sb.Replace(ChrW(&HA2), "¢")
|
|
sb.Replace(ChrW(&HA3), "£")
|
|
sb.Replace(ChrW(&HA4), "¤")
|
|
sb.Replace(ChrW(&HA5), "¥")
|
|
sb.Replace(ChrW(&HA6), "¦")
|
|
sb.Replace(ChrW(&HA7), "§")
|
|
sb.Replace(ChrW(&HA8), "¨")
|
|
sb.Replace(ChrW(&HA9), "©")
|
|
sb.Replace(ChrW(&HAA), "ª")
|
|
sb.Replace(ChrW(&HAB), "«")
|
|
sb.Replace(ChrW(&HAC), "¬")
|
|
sb.Replace(ChrW(&HAD), "­")
|
|
sb.Replace(ChrW(&HAE), "®")
|
|
sb.Replace(ChrW(&HAF), "¯")
|
|
sb.Replace(ChrW(&HB0), "°")
|
|
sb.Replace(ChrW(&HB1), "±")
|
|
sb.Replace(ChrW(&HB2), "²")
|
|
sb.Replace(ChrW(&HB3), "³")
|
|
sb.Replace(ChrW(&HB4), "´")
|
|
sb.Replace(ChrW(&HB5), "µ")
|
|
sb.Replace(ChrW(&HB6), "¶")
|
|
sb.Replace(ChrW(&HB7), "·")
|
|
sb.Replace(ChrW(&HB8), "¸")
|
|
sb.Replace(ChrW(&HB9), "¹")
|
|
sb.Replace(ChrW(&HBA), "º")
|
|
sb.Replace(ChrW(&HBB), "»")
|
|
sb.Replace(ChrW(&HBC), "¼")
|
|
sb.Replace(ChrW(&HBD), "½")
|
|
sb.Replace(ChrW(&HBE), "¾")
|
|
sb.Replace(ChrW(&HBF), "¿")
|
|
sb.Replace(ChrW(&HC0), "À")
|
|
sb.Replace(ChrW(&HC1), "Á")
|
|
sb.Replace(ChrW(&HC2), "Â")
|
|
sb.Replace(ChrW(&HC3), "Ã")
|
|
sb.Replace(ChrW(&HC4), "Ä")
|
|
sb.Replace(ChrW(&HC5), "Å")
|
|
sb.Replace(ChrW(&HC6), "Æ")
|
|
sb.Replace(ChrW(&HC7), "Ç")
|
|
sb.Replace(ChrW(&HC8), "È")
|
|
sb.Replace(ChrW(&HC9), "É")
|
|
sb.Replace(ChrW(&HCA), "Ê")
|
|
sb.Replace(ChrW(&HCB), "Ë")
|
|
sb.Replace(ChrW(&HCC), "Ì")
|
|
sb.Replace(ChrW(&HCD), "Í")
|
|
sb.Replace(ChrW(&HCE), "Î")
|
|
sb.Replace(ChrW(&HCF), "Ï")
|
|
sb.Replace(ChrW(&HD0), "Ð")
|
|
sb.Replace(ChrW(&HD1), "Ñ")
|
|
sb.Replace(ChrW(&HD2), "Ò")
|
|
sb.Replace(ChrW(&HD3), "Ó")
|
|
sb.Replace(ChrW(&HD4), "Ô")
|
|
sb.Replace(ChrW(&HD5), "Õ")
|
|
sb.Replace(ChrW(&HD6), "Ö")
|
|
sb.Replace(ChrW(&HD7), "×")
|
|
sb.Replace(ChrW(&HD8), "Ø")
|
|
sb.Replace(ChrW(&HD9), "Ù")
|
|
sb.Replace(ChrW(&HDA), "Ú")
|
|
sb.Replace(ChrW(&HDB), "Û")
|
|
sb.Replace(ChrW(&HDC), "Ü")
|
|
sb.Replace(ChrW(&HDD), "Ý")
|
|
sb.Replace(ChrW(&HDE), "Þ")
|
|
sb.Replace(ChrW(&HDF), "ß")
|
|
sb.Replace(ChrW(&HE0), "à")
|
|
sb.Replace(ChrW(&HE1), "á")
|
|
sb.Replace(ChrW(&HE2), "â")
|
|
sb.Replace(ChrW(&HE3), "ã")
|
|
sb.Replace(ChrW(&HE4), "ä")
|
|
sb.Replace(ChrW(&HE5), "å")
|
|
sb.Replace(ChrW(&HE6), "æ")
|
|
sb.Replace(ChrW(&HE7), "ç")
|
|
sb.Replace(ChrW(&HE8), "è")
|
|
sb.Replace(ChrW(&HE9), "é")
|
|
sb.Replace(ChrW(&HEA), "ê")
|
|
sb.Replace(ChrW(&HEB), "ë")
|
|
sb.Replace(ChrW(&HEC), "ì")
|
|
sb.Replace(ChrW(&HED), "í")
|
|
sb.Replace(ChrW(&HEE), "î")
|
|
sb.Replace(ChrW(&HEF), "ï")
|
|
sb.Replace(ChrW(&HF0), "ð")
|
|
sb.Replace(ChrW(&HF1), "ñ")
|
|
sb.Replace(ChrW(&HF2), "ò")
|
|
sb.Replace(ChrW(&HF3), "ó")
|
|
sb.Replace(ChrW(&HF4), "ô")
|
|
sb.Replace(ChrW(&HF5), "õ")
|
|
sb.Replace(ChrW(&HF6), "ö")
|
|
sb.Replace(ChrW(&HF7), "÷")
|
|
sb.Replace(ChrW(&HF8), "ø")
|
|
sb.Replace(ChrW(&HF9), "ù")
|
|
sb.Replace(ChrW(&HFA), "ú")
|
|
sb.Replace(ChrW(&HFB), "û")
|
|
sb.Replace(ChrW(&HFC), "ü")
|
|
sb.Replace(ChrW(&HFD), "ý")
|
|
sb.Replace(ChrW(&HFE), "þ")
|
|
sb.Replace(ChrW(&HFF), "ÿ")
|
|
sb.Replace(ChrW(&H152), "Œ")
|
|
sb.Replace(ChrW(&H153), "œ")
|
|
sb.Replace(ChrW(&H160), "Š")
|
|
sb.Replace(ChrW(&H161), "š")
|
|
sb.Replace(ChrW(&H178), "Ÿ")
|
|
sb.Replace(ChrW(&H192), "ƒ")
|
|
sb.Replace(ChrW(&H2C6), "ˆ")
|
|
sb.Replace(ChrW(&H2DC), "˜")
|
|
sb.Replace(ChrW(&H391), "Α")
|
|
sb.Replace(ChrW(&H392), "Β")
|
|
sb.Replace(ChrW(&H393), "Γ")
|
|
sb.Replace(ChrW(&H394), "Δ")
|
|
sb.Replace(ChrW(&H395), "Ε")
|
|
sb.Replace(ChrW(&H396), "Ζ")
|
|
sb.Replace(ChrW(&H397), "Η")
|
|
sb.Replace(ChrW(&H398), "Θ")
|
|
sb.Replace(ChrW(&H399), "Ι")
|
|
sb.Replace(ChrW(&H39A), "Κ")
|
|
sb.Replace(ChrW(&H39B), "Λ")
|
|
sb.Replace(ChrW(&H39C), "Μ")
|
|
sb.Replace(ChrW(&H39D), "Ν")
|
|
sb.Replace(ChrW(&H39E), "Ξ")
|
|
sb.Replace(ChrW(&H39F), "Ο")
|
|
sb.Replace(ChrW(&H3A0), "Π")
|
|
sb.Replace(ChrW(&H3A1), "Ρ")
|
|
sb.Replace(ChrW(&H3A3), "Σ")
|
|
sb.Replace(ChrW(&H3A4), "Τ")
|
|
sb.Replace(ChrW(&H3A5), "Υ")
|
|
sb.Replace(ChrW(&H3A6), "Φ")
|
|
sb.Replace(ChrW(&H3A7), "Χ")
|
|
sb.Replace(ChrW(&H3A8), "Ψ")
|
|
sb.Replace(ChrW(&H3A9), "Ω")
|
|
sb.Replace(ChrW(&H3B1), "α")
|
|
sb.Replace(ChrW(&H3B2), "β")
|
|
sb.Replace(ChrW(&H3B3), "γ")
|
|
sb.Replace(ChrW(&H3B4), "δ")
|
|
sb.Replace(ChrW(&H3B5), "ε")
|
|
sb.Replace(ChrW(&H3B6), "ζ")
|
|
sb.Replace(ChrW(&H3B7), "η")
|
|
sb.Replace(ChrW(&H3B8), "θ")
|
|
sb.Replace(ChrW(&H3B9), "ι")
|
|
sb.Replace(ChrW(&H3BA), "κ")
|
|
sb.Replace(ChrW(&H3BB), "λ")
|
|
sb.Replace(ChrW(&H3BC), "μ")
|
|
sb.Replace(ChrW(&H3BD), "ν")
|
|
sb.Replace(ChrW(&H3BE), "ξ")
|
|
sb.Replace(ChrW(&H3BF), "ο")
|
|
sb.Replace(ChrW(&H3C0), "π")
|
|
sb.Replace(ChrW(&H3C1), "ρ")
|
|
sb.Replace(ChrW(&H3C2), "ς")
|
|
sb.Replace(ChrW(&H3C3), "σ")
|
|
sb.Replace(ChrW(&H3C4), "τ")
|
|
sb.Replace(ChrW(&H3C5), "υ")
|
|
sb.Replace(ChrW(&H3C6), "φ")
|
|
sb.Replace(ChrW(&H3C7), "χ")
|
|
sb.Replace(ChrW(&H3C8), "ψ")
|
|
sb.Replace(ChrW(&H3C9), "ω")
|
|
sb.Replace(ChrW(&H3D1), "ϑ")
|
|
sb.Replace(ChrW(&H3D2), "ϒ")
|
|
sb.Replace(ChrW(&H3D6), "ϖ")
|
|
sb.Replace(ChrW(&H2002), " ")
|
|
sb.Replace(ChrW(&H2003), " ")
|
|
sb.Replace(ChrW(&H2009), " ")
|
|
sb.Replace(ChrW(&H200C), "‌")
|
|
sb.Replace(ChrW(&H200D), "‍")
|
|
sb.Replace(ChrW(&H200E), "‎")
|
|
sb.Replace(ChrW(&H200F), "‏")
|
|
sb.Replace(ChrW(&H2013), "–")
|
|
sb.Replace(ChrW(&H2014), "—")
|
|
sb.Replace(ChrW(&H2018), "‘")
|
|
sb.Replace(ChrW(&H2019), "’")
|
|
sb.Replace(ChrW(&H201A), "‚")
|
|
sb.Replace(ChrW(&H201C), "“")
|
|
sb.Replace(ChrW(&H201D), "”")
|
|
sb.Replace(ChrW(&H201E), "„")
|
|
sb.Replace(ChrW(&H2020), "†")
|
|
sb.Replace(ChrW(&H2021), "‡")
|
|
sb.Replace(ChrW(&H2022), "•")
|
|
sb.Replace(ChrW(&H2026), "…")
|
|
sb.Replace(ChrW(&H2030), "‰")
|
|
sb.Replace(ChrW(&H2032), "′")
|
|
sb.Replace(ChrW(&H2033), "″")
|
|
sb.Replace(ChrW(&H2039), "‹")
|
|
sb.Replace(ChrW(&H203A), "›")
|
|
sb.Replace(ChrW(&H203E), "‾")
|
|
sb.Replace(ChrW(&H2044), "⁄")
|
|
sb.Replace(ChrW(&H20AC), "€")
|
|
sb.Replace(ChrW(&H2111), "ℑ")
|
|
sb.Replace(ChrW(&H2118), "℘")
|
|
sb.Replace(ChrW(&H211C), "ℜ")
|
|
sb.Replace(ChrW(&H2122), "™")
|
|
sb.Replace(ChrW(&H2135), "ℵ")
|
|
sb.Replace(ChrW(&H2190), "←")
|
|
sb.Replace(ChrW(&H2191), "↑")
|
|
sb.Replace(ChrW(&H2192), "→")
|
|
sb.Replace(ChrW(&H2193), "↓")
|
|
sb.Replace(ChrW(&H2194), "↔")
|
|
sb.Replace(ChrW(&H21B5), "↵")
|
|
sb.Replace(ChrW(&H21D0), "⇐")
|
|
sb.Replace(ChrW(&H21D1), "⇑")
|
|
sb.Replace(ChrW(&H21D2), "⇒")
|
|
sb.Replace(ChrW(&H21D3), "⇓")
|
|
sb.Replace(ChrW(&H21D4), "⇔")
|
|
sb.Replace(ChrW(&H2200), "∀")
|
|
sb.Replace(ChrW(&H2202), "∂")
|
|
sb.Replace(ChrW(&H2203), "∃")
|
|
sb.Replace(ChrW(&H2205), "∅")
|
|
sb.Replace(ChrW(&H2207), "∇")
|
|
sb.Replace(ChrW(&H2208), "∈")
|
|
sb.Replace(ChrW(&H2209), "∉")
|
|
sb.Replace(ChrW(&H220B), "∋")
|
|
sb.Replace(ChrW(&H220F), "∏")
|
|
sb.Replace(ChrW(&H2211), "∑")
|
|
sb.Replace(ChrW(&H2212), "−")
|
|
sb.Replace(ChrW(&H2217), "∗")
|
|
sb.Replace(ChrW(&H221A), "√")
|
|
sb.Replace(ChrW(&H221D), "∝")
|
|
sb.Replace(ChrW(&H221E), "∞")
|
|
sb.Replace(ChrW(&H2220), "∠")
|
|
sb.Replace(ChrW(&H2227), "∧")
|
|
sb.Replace(ChrW(&H2228), "∨")
|
|
sb.Replace(ChrW(&H2229), "∩")
|
|
sb.Replace(ChrW(&H222A), "∪")
|
|
sb.Replace(ChrW(&H222B), "∫")
|
|
sb.Replace(ChrW(&H2234), "∴")
|
|
sb.Replace(ChrW(&H223C), "∼")
|
|
sb.Replace(ChrW(&H2245), "≅")
|
|
sb.Replace(ChrW(&H2248), "≈")
|
|
sb.Replace(ChrW(&H2260), "≠")
|
|
sb.Replace(ChrW(&H2261), "≡")
|
|
sb.Replace(ChrW(&H2264), "≤")
|
|
sb.Replace(ChrW(&H2265), "≥")
|
|
sb.Replace(ChrW(&H2282), "⊂")
|
|
sb.Replace(ChrW(&H2283), "⊃")
|
|
sb.Replace(ChrW(&H2284), "⊄")
|
|
sb.Replace(ChrW(&H2286), "⊆")
|
|
sb.Replace(ChrW(&H2287), "⊇")
|
|
sb.Replace(ChrW(&H2295), "⊕")
|
|
sb.Replace(ChrW(&H2297), "⊗")
|
|
sb.Replace(ChrW(&H22A5), "⊥")
|
|
sb.Replace(ChrW(&H22C5), "⋅")
|
|
sb.Replace(ChrW(&H2308), "⌈")
|
|
sb.Replace(ChrW(&H2309), "⌉")
|
|
sb.Replace(ChrW(&H230A), "⌊")
|
|
sb.Replace(ChrW(&H230B), "⌋")
|
|
sb.Replace(ChrW(&H2329), "⟨")
|
|
sb.Replace(ChrW(&H232A), "⟩")
|
|
sb.Replace(ChrW(&H25CA), "◊")
|
|
sb.Replace(ChrW(&H2660), "♠")
|
|
sb.Replace(ChrW(&H2663), "♣")
|
|
sb.Replace(ChrW(&H2665), "♥")
|
|
sb.Replace(ChrW(&H2666), "♦")
|
|
sb.Replace(" ", " ")
|
|
Return sb.ToString()
|
|
End Function
|
|
|
|
Sub AddTokenizerBaseMacros(ByVal tokenizer As Tokenization.Tokenizer)
|
|
|
|
'*
|
|
'* Prepares the BBCode Grammar
|
|
'*
|
|
With tokenizer
|
|
.Options = RegexOptions.Singleline Or RegexOptions.IgnoreCase
|
|
|
|
'*
|
|
'* Define the grammar macros
|
|
'*
|
|
.AddMacro("h", "[0-9a-f]")
|
|
.AddMacro("nl", "\n|\r\n|\r|\f")
|
|
.AddMacro("space", "[ \t\r\n\f]+")
|
|
.AddMacro("s", "[ \t\r\n\f]*")
|
|
.AddMacro("w", "{s}?")
|
|
.AddMacro("nonascii", "[\u0080-\uffff]")
|
|
.AddMacro("unicode", "\\{h}{1,6}(\r\n|[ \t\r\n\f])?")
|
|
.AddMacro("escape", "{unicode}|\\[^\r\n\f0-9a-f]")
|
|
.AddMacro("nmstart", "[_a-z]|{nonascii}|{escape}")
|
|
.AddMacro("nmchar", "[_a-z0-9-]|{nonascii}|{escape}")
|
|
.AddMacro("string1", "\""([^\n\r\f\\""]|\\{nl}|{escape})*\""")
|
|
.AddMacro("string2", "\'([^\n\r\f\\']|\\{nl}|{escape})*\'")
|
|
.AddMacro("invalid1", "\""([^\n\r\f\\""]|\\{nl}|{escape})*")
|
|
.AddMacro("invalid2", "\'([^\n\r\f\\']|\\{nl}|{escape})*")
|
|
.AddMacro("name", "{nmchar}+")
|
|
.AddMacro("num", "[0-9]+|[0-9]*\.[0-9]+")
|
|
.AddMacro("string", "{string1}|{string2}")
|
|
.AddMacro("invalid", "{invalid1}|{invalid2}")
|
|
.AddMacro("url", "(ftp|https?://)" & _
|
|
"(([0-9a-z_!~*'().&=+$%-]+):([0-9a-z_!~*'().&=+$%-]+@))?" & _
|
|
"(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})" & _
|
|
"(:[0-9]{1,4})?" & _
|
|
"((/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?|(/?))")
|
|
.AddMacro("mail", "(?:mailto:)?[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}")
|
|
.AddMacro("value", "{string}|{mail}|{url}|{num}|{name}")
|
|
.AddMacro("param", "{name}{w}={w}{value}")
|
|
.AddMacro("params", "({space}({param}|{name}))+")
|
|
|
|
End With
|
|
End Sub
|
|
|
|
End Module
|
|
|