If you cannot find a tool that does the job, write a tool that does:
#!/usr/bin/env python3
from sys import stdin
from string import whitespace
def main():
not_directive = whitespace + '#'
indentation = 0
for line in stdin:
stripped = line.lstrip()
if stripped.startswith('#'):
directive = stripped.lstrip(not_directive)
if directive.startswith('endif'):
indentation -= 1
print('#{}{}'.format(' ' * indentation, directive), end='')
if directive.startswith('if'):
indentation += 1
else:
print(line, end='')
if __name__ == '__main__':
main()
See it working online
It reads the source from the stdin, and writes changed source to the stdout.
You can easily redirect input and output with shell.
If you don't know Python3 and wish to understand it:
string
.lstrip(
chars
)
returns string
with chars
removed from it's beggining.
chars
defaults to whitespace
.
'test' * 0
=> ''
, 'test' * 1
=> 'test'
, 'test' * 3
=> 'testtesttest'
'#{}{}'.format('textA', 'textB')
=> '#textAtextB'