MCPcopy Create free account
hub / github.com/ipython/ipython / highlight

Method highlight

IPython/lib/demo.py:519–539  ·  view source on GitHub ↗

Method called on each block to highlight it content

(self, block)

Source from the content-addressed store, hash-verified

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

Callers 1

editMethod · 0.95

Calls 1

formatMethod · 0.45

Tested by

no test coverage detected