~testdoc-dev/testdoc/trunk.git

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Formatters for creating documents.

A formatter is an object which accepts an output stream (usually a file or
standard output) and then provides a structured way for writing to that stream.
All formatters should provide 'title', 'section', 'subsection' and 'paragraph'
methods which write to the stream.
"""


class WikiFormatter(object):
    """Moin formatter."""

    def __init__(self, stream):
        self.stream = stream

    def writeln(self, line):
        self.stream.write('%s\n' % (line,))

    def title(self, name):
        self.writeln('= %s =\n' % (name,))

    def section(self, name):
        self.writeln('')
        self.writeln('== %s ==\n' % (name,))

    def subsection(self, name):
        self.writeln('=== %s ===\n' % (name,))

    def paragraph(self, text):
        self.writeln('%s\n' % (text.strip(),))


class ReSTFormatter(object):
    """ReST formatter."""

    def __init__(self, stream):
        self.stream = stream

    def writeln(self, line):
        self.stream.write('%s\n' % (line,))

    def title(self, name):
        self.writeln('%s' % ('=' * len(name),))
        self.writeln('%s' % (name,))
        self.writeln('%s' % ('=' * len(name),))
        self.writeln('')
        self.writeln('.. contents::')
        self.writeln('')
        self.writeln('')

    def section(self, name):
        self.writeln('')
        self.writeln('%s' % (name,))
        self.writeln('%s' % ('=' * len(name),))
        self.writeln('')

    def subsection(self, name):
        self.writeln('%s' % (name,))
        self.writeln('%s' % ('-' * len(name),))
        self.writeln('')

    def paragraph(self, text):
        self.writeln('%s\n' % (text.strip(),))


class ShinyFormatter(object):
    """Coloured, indented output.
    
    For use on terminals that support ANSI colour sequences.
    """

    _colours = {
        "black": "30",
        "red": "31",
        "green": "32",
        "bright green": "32;1",
        "yellow": "33",
        "bright yellow": "33;1",
        "blue": "34",
        "magenta": "35",
        "cyan": "36",
        "white": "37",
        "bright white": "37;1",
        }

    def __init__(self, stream):
        self.stream = stream
        self._last_indent = 0

    def writeln(self, line, indent, colour):
        if indent is None:
            indent = self._last_indent + 2
        else:
            self._last_indent = indent
        line = ' ' * indent + line
        if colour is None:
            self.stream.write(line + '\n')
        else:
            colour = self._colours[colour]
            self.stream.write('\x1b[%sm%s\x1b[0m\n' % (colour, line))

    def title(self, name):
        self.writeln(name, 0, 'bright green')

    def section(self, name):
        self.writeln(name, 2, 'bright yellow')

    def subsection(self, name):
        self.writeln(name, 4, 'bright white')

    def paragraph(self, text):
        colour = None
        if self._last_indent == 0:
            # title
            colour = 'green'
        elif self._last_indent == 2:
            # section
            colour = 'yellow'
        for line in text.strip().splitlines(True):
            self.writeln(line.rstrip('\n'), None, colour)