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