1 """reST directive for syntax-highlighting ipython interactive sessions.
4 #-----------------------------------------------------------------------------
11 from pygments
.lexer
import Lexer
, do_insertions
12 from pygments
.lexers
.agile
import (PythonConsoleLexer
, PythonLexer
,
14 from pygments
.token
import Comment
, Generic
16 from sphinx
import highlighting
19 #-----------------------------------------------------------------------------
21 line_re
= re
.compile('.*?\n')
23 #-----------------------------------------------------------------------------
24 # Code begins - classes and functions
26 class IPythonConsoleLexer(Lexer
):
28 For IPython console output or doctests, such as:
30 .. sourcecode:: ipython
44 - Tracebacks are not currently supported.
46 - It assumes the default IPython prompts, not customized ones.
49 name
= 'IPython console session'
51 mimetypes
= ['text/x-ipython-console']
52 input_prompt
= re
.compile("(In \[[0-9]+\]: )|( \.\.\.+:)")
53 output_prompt
= re
.compile("(Out\[[0-9]+\]: )|( \.\.\.+:)")
54 continue_prompt
= re
.compile(" \.\.\.+:")
55 tb_start
= re
.compile("\-+")
57 def get_tokens_unprocessed(self
, text
):
58 pylexer
= PythonLexer(**self
.options
)
59 tblexer
= PythonTracebackLexer(**self
.options
)
63 for match
in line_re
.finditer(text
):
65 input_prompt
= self
.input_prompt
.match(line
)
66 continue_prompt
= self
.continue_prompt
.match(line
.rstrip())
67 output_prompt
= self
.output_prompt
.match(line
)
68 if line
.startswith("#"):
69 insertions
.append((len(curcode
),
70 [(0, Comment
, line
)]))
71 elif input_prompt
is not None:
72 insertions
.append((len(curcode
),
73 [(0, Generic
.Prompt
, input_prompt
.group())]))
74 curcode
+= line
[input_prompt
.end():]
75 elif continue_prompt
is not None:
76 insertions
.append((len(curcode
),
77 [(0, Generic
.Prompt
, continue_prompt
.group())]))
78 curcode
+= line
[continue_prompt
.end():]
79 elif output_prompt
is not None:
80 insertions
.append((len(curcode
),
81 [(0, Generic
.Output
, output_prompt
.group())]))
82 curcode
+= line
[output_prompt
.end():]
85 for item
in do_insertions(insertions
,
86 pylexer
.get_tokens_unprocessed(curcode
)):
90 yield match
.start(), Generic
.Output
, line
92 for item
in do_insertions(insertions
,
93 pylexer
.get_tokens_unprocessed(curcode
)):
96 #-----------------------------------------------------------------------------
97 # Register the extension as a valid pygments lexer
98 highlighting
.lexers
['ipython'] = IPythonConsoleLexer()