Using M4, how to extract code from a string and increase indentation?

Viewed 48

I need to write a M4 macro that extracts + transforms code between curly braces.

I want to transform

         {
        import math
        a_list = [1, 4, 9, 16]
        if val:
            print([math.sqrt(i) for i in a_list])
        else:
            print("val is False")
        print("bye bye")
     }

to

            import math
            a_list = [1, 4, 9, 16]
            if val:
                print([math.sqrt(i) for i in a_list])
            else:
                print("val is False")
            print("bye bye")

The macro has to trim the whitespace before the first { and after the last }. Because this is python code, nside the curly braces, the relative indentation must be preserved. Because the output of the macro will be outputted somewhere, needed a certain level of indentation. The macro should also be able to add extra indentation (=some number of spaces), e.g. given as an argument. The project is already using m4sugar, so the quotes are [ and ].

Thanks.

0 Answers
Related