Automatically indent a whole block of text into str.format I wondered if there was any better way of doing so.

I had a hard time figuring it out (it seemed nobody else already tried to do so).

import re
import string

class BetterFormat(string.Formatter):
    """ Introduce better formatting """

    def parse(self, format_string):
        """ Receive the string to be transformed and split its elements to facilitate the actual replacing """

        # Automatically counts how many tabs are before the {} then automatically adds the new rule (defined in format_field)
        return [(before, identifiant, str(len(re.search('\t*$', before).group(0))) + '\t' + (param if param is not None else ''), modif) for before, identifiant, param, modif in super().parse(format_string)]

    def format_field(self, v, pattern):
        """ Receive the string to be transformed and the pattern according which it is supposed to be modified """

        # Hacky way to remove a dynamic sequence of characters ([0-9]+\t) and returning it

        sharedData = {'numberOfTabs': 0}

        def extractTabs(pattern):
            sharedData['numberOfTabs'] = int(pattern.group(0)[:-1]) # Save the data that will be erased
            return "" # This will erase it

        pattern = re.sub('[0-9]+\t', extractTabs, pattern)

        if sharedData['numberOfTabs']: # If there are tabs to be added
            v = (sharedData['numberOfTabs'] * '\t').join(v.splitlines(True))

        return super().format_field(v, pattern)

css = """
/* Some multi-lines CSS
All automatically indented */
"""

print(BetterFormat.format("""
&lt;html&gt;
    &lt;head&gt;
        &lt;style type="text/css"&gt;
            {css}
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;!-- Whatever --&gt;
    &lt;/body&gt;
&lt;/html&gt;""",
    css=css
 ))

 # Output
 &lt;html&gt;
    &lt;head&gt;
        &lt;style type="text/css"&gt;
            /* Some multi-line CSS 
            All automatically indented */
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;!-- Whatever --&gt;
    &lt;/body&gt;
&lt;/html&gt;


Otherwise, previously I had to do this:

# […]

print(""" […]
            {css}
[…]""".format(css='\t\t\t'.join(css.splitlines(True)))
)


And just so you better understand, I first only implemented BetterFormat.format_field, then I was able to do that, but I was still required to count the number of tabulations myself:

# […]

print(""" […]
            {css:3\t}
[…]""".format(css=css)
)
Could textwrap do some of the job?

Particularly the dedent and indent function?  You will still need to count the number of tabs you want, but the rest will be done.