search and replace bad html code

Find, replace, find in files, replace in files, regular expressions

search and replace bad html code

Postby rwehrly » Tue Nov 15, 2005 12:58 am

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
User avatar
rwehrly
Newbie
 
Posts: 5
Joined: Mon Nov 14, 2005 12:00 am

Re: search and replace bad html code

Postby Mofi » Tue Nov 15, 2005 8:03 am

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

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, so Key HOME is not necessary anymore after Top. A Replace All does not change cursor position, so 1 Top is enough.

Run a macro on all open files see
viewtopic.php?t=1044
viewtopic.php?t=493

And some more issues about that can be found in the macro and find forum, because I (author: Mofi) have written some more solutions. Search for it by yourself.

Maybe it's better for you to use the ReplInFiles command, if you always want to do the replace on several files at once.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: search and replace bad html code

Postby rwehrly » Tue Nov 15, 2005 9:40 pm

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.
User avatar
rwehrly
Newbie
 
Posts: 5
Joined: Mon Nov 14, 2005 12:00 am

Re: search and replace bad html code

Postby Mofi » Wed Nov 16, 2005 7:26 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: search and replace bad html code

Postby rwehrly » Wed Nov 16, 2005 3:25 pm

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.
User avatar
rwehrly
Newbie
 
Posts: 5
Joined: Mon Nov 14, 2005 12:00 am

Re: search and replace bad html code

Postby Mofi » Wed Nov 16, 2005 4:44 pm

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: search and replace bad html code

Postby rwehrly » Wed Nov 16, 2005 6:00 pm

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
User avatar
rwehrly
Newbie
 
Posts: 5
Joined: Mon Nov 14, 2005 12:00 am

Re: search and replace bad html code

Postby rwehrly » Fri Nov 18, 2005 2:20 am

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 Ultra Edit!

Rox
User avatar
rwehrly
Newbie
 
Posts: 5
Joined: Mon Nov 14, 2005 12:00 am


Return to Find/Replace/Regular Expressions