Macro for deletion of unnecessary tags in an HTML file at current position of text cursor

Macro for deletion of unnecessary tags in an HTML file at current position of text cursor

6
NewbieNewbie
6

    8:37 - May 02#1

    I would like a macro which deletes tags based at current position of the caret, no matter where is the text cursor:
    • at the beginning of an opening tag;
    • inside an opening tag;
    • between opening and closing tag;
    • at the beginning of a closing tag;
    • inside a closing tag.
    The macro should delete opening and closing tag.

    Here is an example HTML file.

    Code: Select all

    <html>
    <head>
    <title>EXAMPLE</title>
    </head>
    <body>
    <p class="indent">EXAMPLE FILE</p>
    
    <p>===Unnecessary tags==</p>
    
    <p>These are useless tags. Opening and closing tags should be deleted together.</p>
    
    <p><b>Aaa</b>
    should be just
    Aaa</p>
    
    <p><i>Bbb</i>
    should be just
    Bbb</p>
    
    <p><u>Ccc</u>
    should be just
    Ccc</p>
    
    <p class="indent">Example <a id="rref6" href="#ref6">6</a>
    should be just
    Example 6</p>
    
    </body>
    </html>
    Here is a macro which I have so far. It works only on the opening tag. If the caret is at opening tag, then the two tags are deleted together.

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    PerlReOn
    Find RegExp Up "(?<=<)\w+"
    Copy
    UltraEditReOn
    Find RegExp "</^c>"
    Key DEL
    UltraEditReOn
    Find RegExp Up "<^c*>"
    Key DEL
    

    6,686585
    Grand MasterGrand Master
    6,686585

      10:26 - May 04#2

      Here is a macro for this task which was quickly developed and poorly tested with UltraEdit for Windows v2024.0.0.28. It can be used for HTML, XHTML and XML files.

      The macro has no HTML / XHTML / XML language knowledge. It does not determine if the text to delete is really an HTML, XHTML or XML tag. It does not analyze the structure to find the matching tag. It just deletes the next closing or opening tag. It does not recognize if there is an opening tag in an HTML file like <li> without the optional end tag </li> for deletion of only the opening tag in this case. It does not determine if the first tag found is an empty tag like <br> or <br />. The user using this macro should know when it is executed and what it really does. There can be of course used the command Undo to undo unwanted deletions.

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      UltraEditReOn
      IfCharIs "<"
      Else
      Find MatchCase RegExp "[<>]"
      IfNotFound
      Find MatchCase RegExp Up "[<>]"
      IfNotFound
      ExitMacro
      EndIf
      EndIf
      Key LEFT ARROW
      IfCharIs ">"
      Find MatchCase Up "<"
      IfNotFound
      ExitMacro
      EndIf
      Key LEFT ARROW
      EndIf
      EndIf
      Clipboard 9
      Key RIGHT ARROW
      IfCharIs "/"
      Find RegExp "[a-z]+"
      IfFound
      Copy
      Else
      ExitMacro
      EndIf
      Find MatchCase Up "<"
      Key LEFT ARROW
      Find MatchCase RegExp "</[~>]+>"
      Replace ""
      Find RegExp Up "<^c[^t^n^r >]"
      IfFound
      Key LEFT ARROW
      Find MatchCase Up "<"
      Key LEFT ARROW
      Find MatchCase RegExp "<[~>]+>"
      Replace ""
      EndIf
      Else
      Find RegExp "[a-z]+"
      IfFound
      Copy
      Else
      ExitMacro
      EndIf
      Find MatchCase Up "<"
      Key LEFT ARROW
      Find MatchCase RegExp "<[~>]+>"
      Replace ""
      Find RegExp "</^c[^t^n^r ]++>"
      Replace ""
      EndIf
      ClearClipboard
      Clipboard 0
      
      The code is better understandable with indentations.

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      UltraEditReOn
      IfCharIs "<"
      Else
          Find MatchCase RegExp "[<>]"
          IfNotFound
              Find MatchCase RegExp Up "[<>]"
              IfNotFound
                  ExitMacro
              EndIf
          EndIf
          Key LEFT ARROW
          IfCharIs ">"
              Find MatchCase Up "<"
              IfNotFound
                  ExitMacro
              EndIf
              Key LEFT ARROW
          EndIf
      EndIf
      Clipboard 9
      Key RIGHT ARROW
      IfCharIs "/"
          Find RegExp "[a-z]+"
          IfFound
              Copy
          Else
              ExitMacro
          EndIf
          Find MatchCase Up "<"
          Key LEFT ARROW
          Find MatchCase RegExp "</[~>]+>"
          Replace ""
          Find RegExp Up "<^c[^t^n^r >]"
          IfFound
              Key LEFT ARROW
              Find MatchCase Up "<"
              Key LEFT ARROW
              Find MatchCase RegExp "<[~>]+>"
              Replace ""
          EndIf
      Else
          Find RegExp "[a-z]+"
          IfFound
              Copy
          Else
              ExitMacro
          EndIf
          Find MatchCase Up "<"
          Key LEFT ARROW
          Find MatchCase RegExp "<[~>]+>"
          Replace ""
          Find RegExp "</^c[^t^n^r ]++>"
          Replace ""
      EndIf
      ClearClipboard
      Clipboard 0
      
      Best regards from an UC/UE/UES for Windows user from Austria

      6
      NewbieNewbie
      6

        14:07 - May 04#3

        Thank you
        It works properly