Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/rst-clean.py
blob: 829e4f7b34eebac48229099744fdf73e1fa1689f (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
import codecs, optparse, os

parser = optparse.OptionParser()
parser.add_option("-f", "--file", dest="src_path",
                  help="Path to the .rst file to clean")
parser.add_option("-r", "--replace", dest="replace", default=False,
                  help="Replace previous file with clean one?")
                  
(options, args) = parser.parse_args()

if not options.src_path:
    print '-f option is required'
    exit(0)
elif not os.path.exists(options.src_path):
    print 'source path not found'
    exit(0)

if options.replace:
    dest_path = options.src_path
else:
    dest_path = '__clean.rst'

content = None
    
with codecs.open(options.src_path, 'rb', 'utf-8') as f:
    content = f.read().split('\n\n')


def _join(lines):
    row = ''
    for l in lines.split('\n'):
        l = l.strip()
        if l.startswith('==')\
        or l.startswith('--')\
        or l.startswith('^^')\
        or l.startswith('~~'):
            row += '\n%s' % l
        elif l.endswith('-'):
            row += l.replace('-', '')
        else:
            row += '%s ' % l
    return row


with codecs.open(dest_path, 'wb', 'utf-8') as f:
    f.write('\n\n'.join([_join(lines.strip()) for lines in content]))