search and replace bad html code

search and replace bad html code

5
NewbieNewbie
5

    Nov 15, 2005#1

    I am looking for some help on creating a macro for cleaning up html code. I want to be able to change all the html code tags to lower case. example: <P>

    Maybe somebody has one already that help clean up their code. I am trying to get the code as clean as possible for compliant issues.

    I started my macro with this: (however there may be an easier way)
    InsertMode
    ColumnModeOff
    HexOff
    Top
    Key Ctrl+HOME
    Find "FONT"
    Replace All "font"
    Top
    Key HOME
    Find "H1"
    Replace All "h1"
    Top
    Key HOME
    Find "H2"
    Replace All "h2"
    Top
    Key HOME
    Find "H3"
    Replace All "h3"
    Top
    Key HOME
    Find "H4"
    Replace All "h4"
    Top
    Key HOME
    Find "<A>"
    Replace All "<a>"

    When I get down to the <a> it does not work so am thinking it needs a more specific command for the brackets.

    Also, I want it to do a number of files open in Ultra Edit so I have included the "NextDocument" command at the end of the macro, but what else is needed for it to start over and do each page - a loop?

    Thanks for any help you can offer.

    Rox

    6,675585
    Grand MasterGrand Master
    6,675585

      Nov 15, 2005#2

      Your approach is a little bit dangerous. Improved version of your macro code:

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      Top
      Find MatchWord "<FONT"
      Replace All "<font"
      Find  MatchWord "<H1"
      Replace All "<h1"
      Find  MatchWord "<H2"
      Replace All "<h2"
      Find MatchWord "<H3"
      Replace All "<h3"
      Find MatchWord "<H4"
      Replace All "<h4"
      Find MatchWord "<A"
      Replace All "<a"
      :
      :
      Command Top always sets cursor to column 1 of line 1. Key HOME is not necessary anymore after Top. A Replace All does not change cursor position. One Top is enough.

      For running a macro on all open files, see: And some more issues about that can be found in the macro and find forum because I (author: Mofi) have written some more solutions.

      Maybe it's better for you to use the ReplInFiles command, if you always want to do the replace on several files at once.
      Best regards from an UC/UE/UES for Windows user from Austria

      5
      NewbieNewbie
      5

        Nov 16, 2005#3

        Thanks for your help. I will try this. Would there be any way to make all the html tags <HTML> lowercase? I am having a big problem as a lot of the code is in uppercase.

        6,675585
        Grand MasterGrand Master
        6,675585

          Nov 16, 2005#4

          This macro should convert all html elements and most attributes to lower case. But I have not tested it, so be careful!

          First loop converts the elements to lower case, the second loop converts the attributes. Attributes are hardly to identify.

          The attribute NOWRAP in <td height=25 NOWRAP colspan="2"> for example will not be found as html attribute. Also NOWRAP at the end of a line will not be found. Single attributes without = following could be searched and replaced with a replace all after last loop. There are not many attributes without =.

          Mixed uppercase/lowercase elements and attributes are not found by this macro. You can remove the 2 MatchCase options, if you also want to convert such elements/attributes.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          Top
          Loop
          Find MatchCase RegExp "<[A-Z][A-Z0-9]+"
          IfFound
          ToLower
          Else
          ExitLoop
          EndIf
          EndLoop
          Top
          Loop
          Find MatchCase RegExp "[ ^t^p][A-Z]+[=>]"
          IfFound
          ToLower
          EndSelect
          Key LEFT ARROW
          Else
          ExitLoop
          EndIf
          EndLoop
          Top
          UnixReOn

          Remove the last red command, if you use regular expression in UltraEdit style by default instead of Unix style.
          For UltraEdit v11.10c and lower see Advanced - Configuration - Find - Unix style Regular Expressions.
          For UltraEdit v11.20 and higher see Advanced - Configuration - Searching - Unix style Regular Expressions.
          Macro commands UnixReOn/UnixReOff modifies this setting.
          Best regards from an UC/UE/UES for Windows user from Austria

          5
          NewbieNewbie
          5

            Nov 16, 2005#5

            Thanks so much! I'll keep this in my documentation. I actually figured out a macro that seems to work as far as making all the html code (wthin the html tags) small case. For anybody else out there that might need the same thing I'll post it:

            InsertMode
            ColumnModeOff
            HexOff
            UnixReOff
            Loop
            Find RegExp "<*>"
            IfFound
            ToLower
            Else
            ExitLoop
            EndIf
            EndLoop

            Thanks again for all your help! I'm sure I'll be using these macros from now on for many things.

            6,675585
            Grand MasterGrand Master
            6,675585

              Nov 16, 2005#6

              Your macro is not very good. Html elements spread over multiply lines are not converted to lower case because * does only search to end of current line. Also IDs and class names are case-sensitive (according to HTML and CSS specifications). If you convert it all to lower case, the html page could fail to display correctly. And you convert also everything inside <!-- --> comments.
              Best regards from an UC/UE/UES for Windows user from Austria

              5
              NewbieNewbie
              5

                Nov 16, 2005#7

                Thanks for the tip! The program that created my html code actually writes all the tags within the same line so no issues there. I am converting all the css styles to lowercase as well. I tested a page and looks perfect.

                I tested the macro you have above and it does a good job. (still some capitalization within the tags)

                Rox

                  Nov 18, 2005#8

                  Mofi,

                  Just wanted to let you know I ended up using your code for my macro. You are right it is better. I added the extra things into the macro that were still showing up and it's working perfect. Made a batch macro and does all the work for me. Thanks so much for your help!

                  By the way, I just love UltraEdit!

                  Rox