removed obsolete issues (many of them fixed with AE)
[docutils.git] / sandbox / cben / make / make2rst.py
blob1a4498f0c256484118194ee3e0bc057ffde0d507
1 #!/usr/bin/env python
2 """
3 A simple tool for converting a Makefile to reStructuredText.
5 Basically it strips all comments starting at column 0 that don't start with
6 ``##`` and converts all the rest to literal blocks. The first comment line
7 defines the number of spaces after the comment sign to be ignored (this allows
8 ``# text...`` which is more readable than ``#text...``).
10 """
12 from __future__ import generators
14 import fileinput, sys
16 def convert(lines):
17 """Generate lines of reST from makefile lines."""
18 in_literal_block = False # state
19 comment_spaces = None # stripped from all lines
20 leading_spaces = 0 # this/last line's indentation
22 for line in lines:
23 if line.isspace():
24 # Empty line accepted in both states.
25 if not in_literal_block:
26 line = '#\n'
27 else:
28 line = '\n'
29 if line[0] == '#' and not line[:2] == '##':
30 # Comment line
31 if in_literal_block:
32 yield '\n'
33 in_literal_block = False
35 line = line.expandtabs()[1:]
36 leading_spaces = len(line) - len(line.lstrip(' '))
37 if comment_spaces is None:
38 comment_spaces = leading_spaces
39 else:
40 line = line[min(leading_spaces, comment_spaces):]
42 yield line
43 else:
44 # Literal line
45 if not in_literal_block:
46 yield '\n'
47 yield '::\n'
48 yield '\n'
49 in_literal_block = True
51 yield '\t' + line
53 def main(*args):
54 sys.stdout.writelines(convert(fileinput.input(args)))
56 if __name__ == '__main__':
57 main(*sys.argv[1:])