Imports System.Globalization

Namespace PCSolution

    ''' <summary>
    ''' Práce s dokumenty
    ''' </summary>
    ''' 
    Public Class Document

#Region "  ODSTRANIT DIAKRITIKU  "

        ''' <summary>
        ''' Ze zadaného řetězce bude odstraněna diakritika
        ''' </summary>
        ''' <param name="sOddelovac">oddelovac, ktery bude pouzit misto prazdne mezery</param>
        Function OdstranitDiakritiku(ByVal str As String, ByVal sOddelovac As String) As String

            str = str.Normalize(System.Text.NormalizationForm.FormD)
            Dim sb As New System.Text.StringBuilder
            Dim i As Integer
            For i = 0 To str.Length - 1
                If Not CharUnicodeInfo.GetUnicodeCategory(str(i)) = UnicodeCategory.NonSpacingMark Then
                    sb.Append(str(i))
                End If
            Next

            sb = sb.Replace(" ", sOddelovac)
            sb = sb.Replace("|", sOddelovac)
            'sb = sb.Replace(".", "")
            sb = sb.Replace("[", "")
            sb = sb.Replace("]", "")
            sb = sb.Replace("(", "")
            sb = sb.Replace(")", "")
            sb = sb.Replace("?", "")
            sb = sb.Replace("&", "")
            sb = sb.Replace("%", "")
			sb = sb.Replace(",", "")
            sb = sb.Replace(":", "-")
            sb = sb.Replace("+", "")
			sb = sb.Replace("%", "")
			sb = sb.Replace("#", "")
			sb = sb.Replace("""", "")
			sb = sb.Replace("'", "")
			Try
                sb = sb.Replace(sOddelovac & sOddelovac, "")
                sb = sb.Replace(sOddelovac & sOddelovac, "")
                sb = sb.Replace(sOddelovac & sOddelovac, "")
                sb = sb.Replace(sOddelovac & sOddelovac, "")
            Catch
            End Try

            sb = sb.Replace("<strong>", "")
            sb = sb.Replace("</strong>", "")

            sb = sb.Replace("<", "")
            sb = sb.Replace(">", "")
            sb = sb.Replace("/", "")
            sb = sb.Replace("\", "")

            Return sb.ToString

        End Function

        ''' <summary>
        ''' Ze zadaného řetězce bude odstraněna diakritika pro email
        ''' </summary>
        Function OdstranitDiakritikuEmail(ByVal str As String) As String

            str = str.Normalize(System.Text.NormalizationForm.FormD)
            Dim sb As New System.Text.StringBuilder
            Dim i As Integer
            For i = 0 To str.Length - 1
                If Not CharUnicodeInfo.GetUnicodeCategory(str(i)) = UnicodeCategory.LetterNumber Then
                    sb.Append(str(i))
                End If
            Next

            Return sb.ToString

        End Function

#End Region

#Region "  JEDNOZNAKOVE PREDLOZKY  "

        Function JednoznakovePredlozky(str As String) As String

            ' s ", " a ", " o ", " v ", " i ", " za ", " pred ", " nad ", " pri 
            str = str.Replace("  ", " ")
            str = str.Replace("s ", "s&nbsp;")
            str = str.Replace("S ", "S&nbsp;")
            str = str.Replace("a ", "a&nbsp;")
            str = str.Replace("A ", "A&nbsp;")
            str = str.Replace("o ", "o&nbsp;")
            str = str.Replace("O ", "O&nbsp;")
            str = str.Replace("v ", "v&nbsp;")
            str = str.Replace("V ", "V&nbsp;")
            str = str.Replace("i ", "i&nbsp;")
            str = str.Replace("I ", "I&nbsp;")

            '# Odstranit apostrofy a uvozovky z textu
            'str = str.Replace("""", "&#34;")
            str = str.Replace("'", "''")

            Return str

        End Function

#End Region

#Region "  PREVOD TEXTU NA BEZPECNY NAZEV SOUBORU  "

        Function PrevodNaBezpecnyNazevSouboru(s As String) As String

            s = OdstranitDiakritiku(s, "-")

            s = s.Replace(" ", "-")
            s = s.Replace("+", "")
            s = s.Replace("/", "")
            s = s.Replace("?", "")
            s = s.Replace(".", "")
            s = s.Replace("=", "")
            s = s.Replace("?", "")
            s = s.Replace("!", "")

            Return s

        End Function

#End Region

#Region "  PRACE S HTML  "

#Region "  ODSTRANIT HTML Z TEXTU  "

        Function OdstranitHTMLzTXT(ByVal str As String) As String
            'Odstrani HTML tagy z retezce
            Dim cA As Char
            Dim strA As String = ""
            Dim bNezapisovat As Boolean = False

            For Each cA In str
                If cA.ToString = "<" Then bNezapisovat = True 'Jestlize jsme v tagu, tak nezapisovat do retezce
                If bNezapisovat = False Then strA = strA + cA.ToString 'Retezec
                If cA.ToString = ">" Then bNezapisovat = False 'Jestlize tag skonci, je povoleno zapisovat
            Next
            Return strA
        End Function

#End Region

#Region "  ODSTRANIT PISMO Z HTML  "

        Function OdstranitFontZHTML(ByVal str As String) As String
            Dim s As String
            Dim n As Integer
            Dim iDelStart, iDelEnd As Integer 'Od jakeho do jakeho znaku se ma vymazat text

            Do
                s = str.ToLower
                n = s.IndexOf("font-family:")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = """" Or c = ";" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = ";" Then
                    iDelEnd = n + 1
                Else
                    iDelEnd = n
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            Do
                s = str.ToLower
                n = s.IndexOf("<font face")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = ">" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = ">" Then
                    iDelEnd = n + 1
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            Do
                s = str.ToLower
                n = s.IndexOf("<font size")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = ">" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = ">" Then
                    iDelEnd = n + 1
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            str.Replace("</font>", "")

            Do
                s = str.ToLower
                n = s.IndexOf("@font-face")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = "}" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = "}" Then
                    iDelEnd = n + 1
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            Return str

        End Function

#End Region

#End Region

    End Class

End Namespace

#Region "  MODUL PRO PRACI S BEZPECNYM TEXTEM  "

Public Module Docs

#Region "  BEZPECNY TEXT  "

    Function _PrevestTextNaBezpecny(sText As String) As String

        Return sText.Replace("'", "''")

    End Function

    Function _PrevestTextZBezpecneho(sText As String) As String

        Return HttpUtility.HtmlDecode(sText)

    End Function

    Function _FirstLetterUpper(sText As String) As String

        Return sText.Substring(0, 1).ToUpper & sText.Substring(1)

    End Function

#End Region

End Module

#End Region