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