Show More
@@ -0,0 +1,66 b'' | |||
|
1 | #!/usr/bin/env python2 | |
|
2 | ||
|
3 | """ | |
|
4 | Consistent formatting of rst section titles | |
|
5 | """ | |
|
6 | ||
|
7 | import re | |
|
8 | import subprocess | |
|
9 | ||
|
10 | spaces = [ | |
|
11 | (0, 1), # we assume this is a over-and-underlined header | |
|
12 | (2, 1), | |
|
13 | (1, 1), | |
|
14 | (1, 0), | |
|
15 | (1, 0), | |
|
16 | ] | |
|
17 | ||
|
18 | # match on a header line underlined with one of the valid characters | |
|
19 | headermatch = re.compile(r'''\n*(.+)\n([][!"#$%&'()*+,./:;<=>?@\\^_`{|}~-])\2{2,}\n+''', flags=re.MULTILINE) | |
|
20 | ||
|
21 | ||
|
22 | def main(): | |
|
23 | for fn in subprocess.check_output(['hg', 'loc', 'set:**.rst+kallithea/i18n/how_to']).splitlines(): | |
|
24 | print 'processing %s:' % fn | |
|
25 | s = file(fn).read() | |
|
26 | ||
|
27 | # find levels and their styles | |
|
28 | lastpos = 0 | |
|
29 | styles = [] | |
|
30 | for markup in headermatch.findall(s): | |
|
31 | style = markup[1] | |
|
32 | if style in styles: | |
|
33 | stylepos = styles.index(style) | |
|
34 | if stylepos > lastpos + 1: | |
|
35 | print 'bad style %r with level %s - was at %s' % (style, stylepos, lastpos) | |
|
36 | else: | |
|
37 | stylepos = len(styles) | |
|
38 | if stylepos > lastpos + 1: | |
|
39 | print 'bad new style %r - expected %r' % (style, styles[lastpos + 1]) | |
|
40 | else: | |
|
41 | styles.append(style) | |
|
42 | lastpos = stylepos | |
|
43 | ||
|
44 | # remove superfluous spacing (may however be restored by header spacing) | |
|
45 | s = re.sub(r'''(\n\n)\n*''', r'\1', s, flags=re.MULTILINE) | |
|
46 | ||
|
47 | # rewrite header markup with correct style, length and spacing | |
|
48 | def subf(m): | |
|
49 | title, style = m.groups() | |
|
50 | level = styles.index(style) | |
|
51 | before, after = spaces[level] | |
|
52 | return '\n' * (before + 1) + title + '\n' + style * len(title) + '\n' * (after + 1) | |
|
53 | s = headermatch.sub(subf, s) | |
|
54 | ||
|
55 | # remove superfluous spacing when headers are adjacent | |
|
56 | s = re.sub(r'''(\n.+\n([][!"#$%&'()*+,./:;<=>?@\\^_`{|}~-])\2{2,}\n\n\n)\n*''', r'\1', s, flags=re.MULTILINE) | |
|
57 | # fix trailing space and spacing before link sections | |
|
58 | s = s.strip() + '\n' | |
|
59 | s = re.sub(r'''\n+((?:\.\. _[^\n]*\n)+)$''', r'\n\n\n\1', s) | |
|
60 | ||
|
61 | file(fn, 'w').write(s) | |
|
62 | print subprocess.check_output(['hg', 'diff', fn]) | |
|
63 | ||
|
64 | ||
|
65 | if __name__ == '__main__': | |
|
66 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now