Skip to content

Instantly share code, notes, and snippets.

@heyimalex
Created January 28, 2014 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyimalex/8671357 to your computer and use it in GitHub Desktop.
Save heyimalex/8671357 to your computer and use it in GitHub Desktop.
hacky pygments filter to change "def" keyword token type
from markdown2 import Markdown
patched = Markdown._get_pygments_lexer
def patch_get_pygments_lexer(self, lexer_name):
if lexer_name != "python":
return patched(self, lexer_name)
from pygments.lexers import PythonLexer
from pygments.token import Name, Keyword
from pygments.filter import Filter
lexer = PythonLexer()
class DefColorFilter(Filter):
def __init__(self, **options):
Filter.__init__(self, **options)
def filter(self, lexer, stream):
for ttype, value in stream:
if ttype is Keyword and value == 'def':
ttype = Name.Other # Whatever token type you want
yield ttype, value
lexer.add_filter(DefColorFilter())
return lexer
Markdown._get_pygments_lexer = patch_get_pygments_lexer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment