File: tango_console_highlighting.py

package info (click to toggle)
itango 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,984 kB
  • sloc: python: 1,476; makefile: 14
file content (119 lines) | stat: -rw-r--r-- 3,974 bytes parent folder | download
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
"""reST directive for syntax-highlighting itango interactive sessions."""

# -----------------------------------------------------------------------------
# Needed modules

# Standard library
import copy
import re

import pygments.styles

# Third party
from pygments.lexer import Lexer, do_insertions
from pygments.lexers.agile import PythonLexer
from pygments.token import Comment, Generic
from sphinx import highlighting

# -----------------------------------------------------------------------------
# Global constants
line_re = re.compile(".*?\n")

DftStyle = pygments.styles.get_style_by_name("default")


class TangoStyle(DftStyle):
    styles = copy.copy(DftStyle.styles)
    styles[Generic.Prompt] = "bold #00AA00"


class TangoConsoleLexer(Lexer):
    """
    For itango console output or doctests, such as:

    .. sourcecode:: itango

      ITango [1]: a = 'foo'

      ITango [2]: a
                   Result [2]: 'foo'

      ITango [3]: print a
      foo

      ITango [4]: 1 / 0

    Notes:

      - Tracebacks are not currently supported.

      - It assumes the default itango prompts, not customized ones.
    """

    name = "ITango console session"
    aliases = ["itango"]
    mimetypes = ["text/x-itango-console"]
    input_prompt = re.compile("(ITango \[(?P<N>[0-9]+)\]: )|(   \.\.\.+:)")
    output_prompt = re.compile("(\s*Result \[(?P<N>[0-9]+)\]: )|(   \.\.\.+:)")
    continue_prompt = re.compile("   \.\.\.+:")
    tb_start = re.compile("\-+")

    def get_tokens_unprocessed(self, text):
        pylexer = PythonLexer(**self.options)

        curcode = ""
        insertions = []
        for match in line_re.finditer(text):
            line = match.group()
            input_prompt = self.input_prompt.match(line)
            continue_prompt = self.continue_prompt.match(line.rstrip())
            output_prompt = self.output_prompt.match(line)
            if line.startswith("#"):
                insertions.append((len(curcode), [(0, Comment, line)]))
            elif input_prompt is not None:
                insertions.append(
                    (len(curcode), [(0, Generic.Prompt, input_prompt.group())])
                )
                curcode += line[input_prompt.end() :]
            elif continue_prompt is not None:
                insertions.append(
                    (len(curcode), [(0, Generic.Prompt, continue_prompt.group())])
                )
                curcode += line[continue_prompt.end() :]
            elif output_prompt is not None:
                # Use the 'error' token for output.  We should probably make
                # our own token, but error is typicaly in a bright color like
                # red, so it works fine for our output prompts.
                insertions.append(
                    (len(curcode), [(0, Generic.Error, output_prompt.group())])
                )
                curcode += line[output_prompt.end() :]
            else:
                if curcode:
                    for item in do_insertions(
                        insertions, pylexer.get_tokens_unprocessed(curcode)
                    ):
                        yield item
                        curcode = ""
                        insertions = []
                yield match.start(), Generic.Output, line
        if curcode:
            for item in do_insertions(
                insertions, pylexer.get_tokens_unprocessed(curcode)
            ):
                yield item


def setup(app):
    """Setup as a sphinx extension."""

    # This is only a lexer, so adding it below to pygments appears sufficient.
    # But if somebody knows that the right API usage should be to do that via
    # sphinx, by all means fix it here.  At least having this setup.py
    # suppresses the sphinx warning we'd get without it.
    pass


# -----------------------------------------------------------------------------
# Register the extension as a valid pygments lexer
highlighting.lexers["itango"] = TangoConsoleLexer()