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