その仕事をするツールが見つからない場合は、次のようなツールを作成してください。
#!/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()
オンラインでの動作を確認する
stdin からソースを読み取り、変更されたソースを stdout に書き込みます。
シェルで入出力を簡単にリダイレクトできます。
Python3 を知らず、理解したい場合:
string
.lstrip(
chars
)
最初から削除されてstring
戻ります。デフォルトはです。chars
chars
whitespace
'test' * 0
=> ''
、'test' * 1
=> 'test'
、'test' * 3
=>'testtesttest'
'#{}{}'.format('textA', 'textB')
=>'#textAtextB'