summaryrefslogtreecommitdiffstats
path: root/doc/sort-doc.py
blob: 87c35a20a0bb5d6836f13e12583be4b45ff13ca0 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Tool to sort the documentation alphabetically
# Makes a lot of assumptions about the structure of the document it edits
# Looks for special markers that indicate structure

# prints output to stdout

# print lines until in a cmdhelp_<section>
# collect all cmdhelp_<section>_<subsection> subsections
# sort and print

import sys
import re


class Sorter(object):
    def __init__(self):
        self.current_section = None
        self.current_subsection = None
        self.subsections = []
        self.re_section = re.compile(r'^\[\[cmdhelp_([^_,]+),')
        self.re_subsection = re.compile(r'^\[\[cmdhelp_([^_]+)_([^,]+),')

    def beginsection(self, line):
        m = self.re_section.match(line)
        name = m.group(1)
        self.current_section = [name, line]
        self.current_subsection = None
        self.subsections = []
        return self.insection

    def insection(self, line):
        if line.startswith('[[cmdhelp_%s_' % (self.current_section[0])):
            return self.beginsubsection(line)
        elif line.startswith('[['):
            self.finishsection()
            return self.preprint(line)
        else:
            self.current_section[1] += line
        return self.insection

    def beginsubsection(self, line):
        m = self.re_subsection.match(line)
        name = m.group(2)
        self.current_subsection = [name, line]
        return self.insubsection

    def insubsection(self, line):
        if line.startswith('[['):
            self.subsections.append(self.current_subsection)
            self.current_subsection = None
            return self.insection(line)
        self.current_subsection[1] += line
        return self.insubsection

    def finishsection(self):
        if self.current_section:
            print self.current_section[1],
            for name, text in sorted(self.subsections, key=lambda x: x[0]):
                print text,
        self.current_section = None
        self.subsections = []

    def preprint(self, line):
        if self.re_section.match(line):
            return self.beginsection(line)
        print line,
        return self.preprint

    def run(self, lines):
        action = self.preprint
        for line in lines:
            prevaction = action
            action = action(line)
            if action is None:
                print prevaction
                print self.current_section
                print self.current_subsection
                sys.exit(1)
        if self.current_section:
            self.finishsection()

Sorter().run(open(sys.argv[1]).readlines())