SfePy  X.Y
ipython_console_highlighting.py
Go to the documentation of this file.
00001 from pygments.lexer import Lexer, do_insertions
00002 from pygments.lexers.agile import PythonConsoleLexer, PythonLexer, \
00003     PythonTracebackLexer
00004 from pygments.token import Comment, Generic
00005 from sphinx import highlighting
00006 import re
00007 
00008 line_re = re.compile('.*?\n')
00009 
00010 class IPythonConsoleLexer(Lexer):
00011     """
00012     For IPython console output or doctests, such as:
00013 
00014     Tracebacks are not currently supported.
00015 
00016     .. sourcecode:: ipython
00017 
00018       In [1]: a = 'foo'
00019 
00020       In [2]: a
00021       Out[2]: 'foo'
00022 
00023       In [3]: print a
00024       foo
00025 
00026       In [4]: 1 / 0
00027     """
00028     name = 'IPython console session'
00029     aliases = ['ipython']
00030     mimetypes = ['text/x-ipython-console']
00031     input_prompt = re.compile("(In \[[0-9]+\]: )|(   \.\.\.+:)")
00032     output_prompt = re.compile("(Out\[[0-9]+\]: )|(   \.\.\.+:)")
00033     continue_prompt = re.compile("   \.\.\.+:")
00034     tb_start = re.compile("\-+")
00035 
00036     def get_tokens_unprocessed(self, text):
00037         pylexer = PythonLexer(**self.options)
00038         tblexer = PythonTracebackLexer(**self.options)
00039 
00040         curcode = ''
00041         insertions = []
00042         for match in line_re.finditer(text):
00043             line = match.group()
00044             input_prompt = self.input_prompt.match(line)
00045             continue_prompt = self.continue_prompt.match(line.rstrip())
00046             output_prompt = self.output_prompt.match(line)
00047             if line.startswith("#"):
00048                 insertions.append((len(curcode),
00049                                    [(0, Comment, line)]))
00050             elif input_prompt is not None:
00051                 insertions.append((len(curcode),
00052                                    [(0, Generic.Prompt, input_prompt.group())]))
00053                 curcode += line[input_prompt.end():]
00054             elif continue_prompt is not None:
00055                 insertions.append((len(curcode),
00056                                    [(0, Generic.Prompt, continue_prompt.group())]))
00057                 curcode += line[continue_prompt.end():]
00058             elif output_prompt is not None:
00059                 insertions.append((len(curcode),
00060                                    [(0, Generic.Output, output_prompt.group())]))
00061                 curcode += line[output_prompt.end():]
00062             else:
00063                 if curcode:
00064                     for item in do_insertions(insertions,
00065                                               pylexer.get_tokens_unprocessed(curcode)):
00066                         yield item
00067                         curcode = ''
00068                         insertions = []
00069                 yield match.start(), Generic.Output, line
00070         if curcode:
00071             for item in do_insertions(insertions,
00072                                       pylexer.get_tokens_unprocessed(curcode)):
00073                 yield item
00074 
00075 highlighting.lexers['ipython'] = IPythonConsoleLexer()