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

7
NewbieNewbie
7

    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,684587
    Grand MasterGrand Master
    6,684587

      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

      7
      NewbieNewbie
      7

        14:07 - May 04#3

        Thank you
        It works properly

          7:12 - Nov 28#4

          I have been using this macro for a long time and it is working perfectly.
          Thanks for that
          But now I have a problem. It would be great if you could make this macro into a script so I don't have to load it again and again.

          6,684587
          Grand MasterGrand Master
          6,684587

            18:05 - Nov 28#5

            One or more macros can be stored together in a single macro file. The macro file with all the often needed macros can be configured to be loaded automatically on startup of UltraEdit / UEStudio for being available from the beginning without the need to first load manually the macro file. See this forum topic for details. The execution of macros from an automatically on startup loaded macro file is faster than the execution of a script from the script list. The macro solution is better than a script solution for this type of task.

            However, there is installed with UltraEdit / UEStudio a tag list file with the tag groups UE/UES Macro Commands and UE/UES Script Commands. I use both on writing macros from scratch or writing scripts. Open the Tag List view, select the tag group for the script commands and replace one macro command after the other by the corresponding script command. The name of the tags is identical in both tag groups if a macro command is available also as a script command. It should be therefore no problem for any user to rewrite a macro as script with little knowledge on JavaScript syntax.
            Best regards from an UC/UE/UES for Windows user from Austria