Imports Microsoft.VisualBasic
Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Public Module Binarypros

    ' needs fixed from old modBP
    <Extension()>
    Public Function StringFromTo(ByVal InText As String, ByVal FromText As String, ByVal ToText As String, Optional ByVal IncludeFromText As Boolean = True, Optional ByVal CaseSensitive As Boolean = False)

        Dim FromTextPOS As Integer
        Dim ToTextPos As Integer

        Dim WorkText As String

        WorkText = InText

        If CaseSensitive = False Then
            WorkText = LCase(WorkText)
            FromText = LCase(FromText)
            ToText = LCase(ToText)
        End If

        FromTextPOS = InStr(1, WorkText, FromText)

        If FromTextPOS = 0 Then
            Return ""
        End If

        ToTextPos = InStr(FromTextPOS + Len(FromText) + 1, WorkText, ToText)

        If ToTextPos = 0 Then
            Return ""
        End If

        If IncludeFromText = True Then
            Return Mid(InText, FromTextPOS, ToTextPos - FromTextPOS)
        Else
            Return Mid(InText, FromTextPOS + Len(FromText), ToTextPos - FromTextPOS - Len(FromText))
        End If

    End Function

    <Extension()>
    Public Function StripHTMLTags(ByVal sText As String) As String
        'Removes various unneccesary HTML tag from the text provided


        'StringFromTo(sText, ">",)
        'sText = sText.Substring(sText.IndexOf(">") + 1)
        'sText = Replace(sText, "<b>", "", , , vbTextCompare)
        'sText = Replace(sText, "<em>", "", , , vbTextCompare)
        'sText = Replace(sText, "</em>", "", , , vbTextCompare)
        'sText = Replace(sText, "</b>", "", , , vbTextCompare)
        'sText = Replace(sText, "<strong>", "", , , vbTextCompare)
        'sText = Replace(sText, "</strong>", "", , , vbTextCompare)
        sText = Replace(sText, "<br>" + vbCrLf, vbCrLf, , , vbTextCompare)
        sText = Replace(sText, "<br>", vbCrLf, , , vbTextCompare)
        'sText = Replace(sText, "<font size=-1>", "", , , vbTextCompare)
        'sText = Replace(sText, "<font size=+0>", "", , , vbTextCompare)
        'sText = Replace(sText, "</font>", "", , , vbTextCompare)
        'sText = Replace(sText, "</a>", "", , , vbTextCompare)
        'sText = Replace(sText, "<a>", "", , , vbTextCompare)
        sText = Replace(sText, "&nbsp;", " ", , , vbTextCompare)
        sText = Replace(sText, "&amp;", " and ", , , vbTextCompare)

        ' An evil regex copied from another program that should help...
        'Regex.Replace(_webDocument.body.innerHTML, "<.+?>", new MatchEvaluator(lower));
        If sText Is Nothing Then
            Return ""
        End If


        sText = Regex.Replace(sText, "<.+?>", "")

        StripHTMLTags = Trim(sText)

    End Function

    <Extension()>
    Public Function ReplaceExtendedChars(ByVal sText As String) As String
        'Replaces extended HTML characters with their plain text counterpart

        sText = Replace(sText, "&amp;", "&", , , vbTextCompare)
        sText = Replace(sText, "&#39;", "'", , , vbTextCompare)
        sText = Replace(sText, "&nbsp;", " ", , , vbTextCompare)
        sText = Replace(sText, "&quot;", Chr(34), , , CompareMethod.Text)

        ReplaceExtendedChars = Trim(sText)

    End Function

    Private m_report As Reporter
    Public ReadOnly Property Report As Reporter
        Get
            If m_report Is Nothing Then
                m_report = New Reporter()
            End If

            Return m_report
        End Get
    End Property
End Module
