Colorizing Python Source Using the Built-in Tokenizer


/ Published in: Python
Save to your folder(s)

You need to convert Python source code into HTML markup, rendering comments, keywords, operators, and numeric and string literals in different colors.
tokenize.generate_tokens does most of the work. We just need to loop over all tokens it finds, to output them with appropriate colorization:

URL: http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-16-sect-6.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.