Search code examples
htmlxmlnotepad++

Linearize specific node of XML/HTML document for vertical editing on Notepad++


In order to easily modify in vertical selection a huge number of cells in the table in one Selection with a vertical paste. (the paste on each line is from a list which has different value on each line).

I would like to do it inside Notepad++ only without the need to program anything.

What I have from the start is normal <Tbody> content of a table like this :

    <tbody>
    <tr>
        <td>
            MachineType 
        </td>
        <td>
            Yt_GP_MachineType
        </td>
        <td/>
        <td>
            MyMachine.MachineType
        </td>
    </tr>
    <tr>
        <td>
            Variant 
        </td>
        <td>
            Yt_GP_Variant
        </td>
        <td>
        </td>
        <td>
            MyMachine.Variant
        </td>
    </tr>
    <tr>
        <td>
            Emulation
        </td>
        <td>
            Yt_GP_Emulation
        </td>
        <td>
        </td>
        <td>
            MyMachine.Emulation
        </td>
    </tr>

And I would like to have a macro that linearize and align all <tr> nodes and below in a single line like this :

    <tbody>
    <tr><td>    MachineType </td><td>   Yt_GP_MachineType   </td><td></td><td>  MyMachine.MachineType   </td></tr>
    <tr><td>    Variant     </td><td>   Yt_GP_Variant       </td><td></td><td>  MyMachine.Variant       </td></tr>
    <tr><td>    Emulation   </td><td>   Yt_GP_Emulation     </td><td></td><td>  MyMachine.Emulation     </td></tr>

Note: the auto alignment of each <td> & </td> nodes is important and the "Code alignment" plugin of Notepad++ doesn't work if I mentioned align by... (CTRL+SHIFT+=) "<" for my part. Currently I'm doing this manually...

Note 2 : Linearize or Pretty print from XML Tool plug in doesn't solve my issue.


Solution

  • The short story - IMHO you should write a script in your favorite scripting language.

    BTW - spaces within <tr>...</tr> only makes manual editing easier and should be replaced by the use of CSS e.g. <tbody style="vertical-align:center;text-align:center"><tr style="height:100px">

    But if you really want to do a special linearize with Notepad++, here is the man's way on Windows. For other operating systems you have to adapt this.

    1. Tidy up different (!) tags like <td/>. Find what: <td/>, Replace with: <td></td>
    2. Tidy trailing space (see your HTML snippet posted): From main menu Edit > Blank Operations > Trim Trailing Space
    3. Tidy tabs vs. space (see your HTML snippet posted): From main menu Edit > Blank Operations > TAB to Space
    4. From main menu Plugins > XML Tools > Linearize
    5. RegEx Make single lines: Find what: <tr>, Replace with: \r\n<tr>
    6. RegEx Expand lines (only change trto td): Find what: <td>, Replace with: \r\n<td>
    7. Go to begin of first line e.g Strg+Pos1
    8. Align: Plugins > Code alignment > Align by...</td> (It may be necessary for you to install the plugin.)
    9. If really necessary (for your needs only) insert two spaces. Uncheck Wrap around and try single replace first! Find what: <td>, Replace with: <td> . Please note the two space here!
    10. Repeat (4) - From main menu Plugins > XML Tools > Linearize
    11. Repeat (5) - RegEx Make single lines: Find what: <tr>, Replace with: \r\n<tr>

    All this is resulting in:

    enter image description here