MCPcopy Index your code
hub / github.com/ipython/ipython / highlight

Method highlight

IPython/lib/demo.py:520–540  ·  view source on GitHub ↗

Method called on each block to highlight it content

(self, block)

Source from the content-addressed store, hash-verified

518 pass
519
520 def highlight(self, block):
521 """Method called on each block to highlight it content"""
522 tokens = pygments.lex(block, self.python_lexer)
523 if self.format_rst:
524 from pygments.token import Token
525 toks = []
526 for token in tokens:
527 if token[0] == Token.String.Doc and len(token[1]) > 6:
528 toks += pygments.lex(token[1][:3], self.python_lexer)
529 # parse doc string content by rst lexer
530 toks += pygments.lex(token[1][3:-3], self.rst_lexer)
531 toks += pygments.lex(token[1][-3:], self.python_lexer)
532 elif token[0] == Token.Comment.Single:
533 toks.append((Token.Comment.Single, token[1][0]))
534 # parse comment content by rst lexer
535 # remove the extra newline added by rst lexer
536 toks += list(pygments.lex(token[1][1:], self.rst_lexer))[:-1]
537 else:
538 toks.append(token)
539 tokens = toks
540 return pygments.format(tokens, self.formatter)
541
542
543class IPythonDemo(Demo):

Callers 1

editMethod · 0.95

Calls 1

formatMethod · 0.45

Tested by

no test coverage detected