Re: forcing paragraphs with one line of text

  •  07-13-2004, 2:51 PM

    Re: forcing paragraphs with one line of text

    If anyone is interested, here's the fix I've built. It's not the prettiest thing, but works. We're calling this function on the SUBMIT button before the text is stored in the database. I hope it is of some use to someone...
     

    function checkForBlockTagWrappers (ByVal textToParse as String) as String
        ' set s as string that we'll return
        dim s as String
       
        ' check to confirm that there is actual text to parse
        ' (as opposed to a blank field)
        if textToParse <> "" then

        ' create a Regex to check for leading spaces and lines:   
        dim regexLeadingSpace as New Regex( _
            "(?<leadingSpace>(^(\s|\n)+))" & _
            "(?<EverythingElse>((.|\n)*))", RegexOptions.IgnoreCase)
        ' set each matched group to a string
        dim matchLeadingSpace as Match = regexLeadingSpace.Match(textToParse)
        dim strLeadingSpace as String = matchLeadingSpace.Groups("leadingSpace").Value.toString
        dim strEverythingElse as String = matchLeadingSpace.Groups("EverythingElse").Value.toString
        ' if there is a match for the leading space, then just use the second match (the text)
        if (strLeadingSpace > "") then
            ' check to make sure there is text after the leading spaces. If so, then
            ' use that text as the string to parse
            if strEverythingElse > ""
                textToParse = strEverythingElse
            ' otherwise, return nothing
            else
                s = ""
                return s
            end if
        else
        ' there are no leading spaces, so continue on
        end if

        ' create a Regex to check for the first word/tag. If a tag, it will return
        ' the opening bracket and the tag name. Ex: <table
        dim regexFirstWord as new regex( _
            "(?<firstWord>(\S[^\s/>]*))", RegexOptions.IgnoreCase)
        ' set the match as a string
        dim m1 as Match = regexFirstWord.Match(textToParse)
        dim firstWord as String = m1.Groups("firstWord").Value.toString
        ' set the first word to all lowercase (for comparing to tags)
        firstWord = microsoft.VisualBasic.LCase(firstWord)
        ' see if the first word is a block-level tag
        if  firstWord = "<address" or _
            firstWord = "<blockquote" or _
              firstWord = "<center" or _
            firstWord = "<dir" or _
            firstWord = "<div" or _
            firstWord = "<dl" or _
            firstWord = "<fieldset" or _
            firstWord = "<form" or _
            firstWord = "<h1" or _
            firstWord = "<h2" or _
            firstWord = "<h3" or _
            firstWord = "<h4" or _
            firstWord = "<h5" or _
            firstWord = "<h6" or _
            firstWord = "<hr" or _
            firstWord = "<isindex" or _
            firstWord = "<menu" or _
            firstWord = "<noframes" or _
            firstWord = "<noscript" or _
            firstWord = "<ol" or _
            firstWord = "<p" or _
            firstWord = "<pre" or _
            firstWord = "<table" or _
            firstWord = "<ul" or _
            firstWord = "<dd" or _
            firstWord = "<dt" or _
            firstWord = "<frameset" or _
            firstWord = "<li" or _
            firstWord = "<tbody" or _
            firstWord = "<td" or _
            firstWord = "<tfoot" or _
            firstWord = "<th" or _
            firstWord = "<thead" or _
            firstWord = "<tr" then
                ' if so, do nothing as it *appears* to be wrapped in a proper block-level tag
                s = textToParse
            else
                ' add default P tag wrappers
                s = "<p>" & textToParse & "</p>"
                ' if field contained spaces, then just delete it all
        end if
        return s

        ' else, if the field is empty, don't do anything
        else
        end if

    end function

View Complete Thread