Search code examples
htmlsublimetext3sublime-text-plugin

How to replace numeric suffix of a value 3 times, increment suffix, and repeat with Text Pastry?


I need to create 210 checkboxes each with unique names, id's and label for values.

The checkboxes reside in tables, where there are 6 checkboxes to a table.

I've manually done this for 54 checkboxes (9 tables), and want to automate the creation of the last 156 checkboxes (26 tables).

I therefore want to automate the incrementation of:

  • name
  • id
  • label for value

This is the simplest way I can see to do it:

  • Create the base structure for 26 tables (see below).

  • Use the Text Pastry plugin for Sublime Text.

  • Use a variant of this example:

https://github.com/duydao/Text-Pastry/wiki/Examples#insert-nums-syntax

  • With the instructions to:

    • Find "checkbox00"
    • Replace with "checkbox55"
    • Do this for 3 instances
    • Increment the number used in the replacement text and repeat for the remaining instances.

Is this the sort of thing that Text Pastry was designed to do?

If so, and it's not too large a task to explain, specific directions would be appreciated.

<!-- BEGIN table 10 -->
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
        <label for="checkbox00" class="css-label">custom_text_A</label>
    </td>
</tr>
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox"/>
        <label for="checkbox00" class="css-label">custom_text_B</label>
    </td>
</tr>
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
        <label for="checkbox00" class="css-label">custom_text_C</label>
    </td>
</tr>
    <tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
        <label for="checkbox00" class="css-label">custom_text_D</label>
    </td>
</tr>
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox"/>
        <label for="checkbox00" class="css-label">custom_text_E</label>
    </td>
</tr>
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
        <label for="checkbox00" class="css-label">custom_text_F</label>
    </td>
</tr>
<!-- END table 10 -->
<!-- BEGIN table 11 -->
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
        <label for="checkbox00" class="css-label">custom_text_A</label>
    </td>
</tr>
<tr>
    <td class="area_checkbox">
        <input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox"/>
        <label for="checkbox00" class="css-label">custom_text_B</label>
    </td>
</tr>
<!-- ... etc, continue for 26 tables -->

Solution

  • Update/Solution

    Here's a Python script I put together quickly that seems to do what I want it to do:

    my_string = "<tr><td class=\"area_checkbox\"> \
    <input type=\"checkbox\" name=\"checkbox00\" id=\"checkbox00\" class=\"css-checkbox\" /> \
    <label for=\"checkbox00\" class=\"css-label\">custom_text_00</label> \
    </td></tr>"
    growing_string = ""
    cb_suffix = 55
    text_suffix = 1
    for i in range(156):
        if text_suffix <= 6:
            temp_string = my_string.replace("checkbox00", "checkbox" + str(cb_suffix),3)
            temp_string_2 = temp_string.replace("custom_text_00", "custom_text_0" + str(text_suffix))
            growing_string += temp_string_2
            cb_suffix += 1
            text_suffix += 1
        else:
            text_suffix = 1
            temp_string = my_string.replace("checkbox00", "checkbox" + str(cb_suffix),3)
            temp_string_2 = temp_string.replace("custom_text_00", "custom_text_0" + str(text_suffix))
            growing_string += temp_string_2
            cb_suffix += 1
            text_suffix += 1
    
    print growing_string
    

    Then I copied the output to JSFiddle and clicked TidyUp to format it :).

    Sample Output:

    <tr>
        <td class="area_checkbox">
            <input type="checkbox" name="checkbox55" id="checkbox55" class="css-checkbox" />
            <label for="checkbox55" class="css-label">custom_text_01</label>
        </td>
    </tr>
    <tr>
        <td class="area_checkbox">
            <input type="checkbox" name="checkbox56" id="checkbox56" class="css-checkbox" />
            <label for="checkbox56" class="css-label">custom_text_02</label>
        </td>
    </tr>