Mercurial > kallithea
comparison scripts/docs-headings.py @ 5537:f38b50f8a6a6
scripts: introduce scripts/docs-headings.py for reformatting rst section titles in docs
Changes from this has already been committed in fbbe80e3322b and 8867673c8192.
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Thu, 08 Oct 2015 23:25:21 +0200 |
parents | |
children | ed2fb6e84a02 |
comparison
equal
deleted
inserted
replaced
5536:06d5c043e989 | 5537:f38b50f8a6a6 |
---|---|
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 print | |
64 | |
65 if __name__ == '__main__': | |
66 main() |