Simplify vendor extension content-types since they are most likely lexable

This commit is contained in:
Jökull Sólberg Auðunsson 2012-07-14 14:27:11 +00:00
parent 7bc2de2f9d
commit 36de166b28

View File

@ -1,4 +1,5 @@
import os import os
import re
import json import json
import pygments import pygments
@ -18,6 +19,8 @@ FORMATTER = (Terminal256Formatter
if '256color' in os.environ.get('TERM', '') if '256color' in os.environ.get('TERM', '')
else TerminalFormatter) else TerminalFormatter)
application_content_type_re = re.compile(r'application/(.+\+)?(json|xml)$')
class PrettyHttp(object): class PrettyHttp(object):
@ -33,12 +36,18 @@ class PrettyHttp(object):
def body(self, content, content_type): def body(self, content, content_type):
content_type = content_type.split(';')[0] content_type = content_type.split(';')[0]
application_match = re.match(application_content_type_re, content_type)
if application_match:
# Strip vendor and extensions from Content-Type
vendor, extension = application_match.groups()
content_type = content_type.replace(vendor, u"")
try: try:
lexer = get_lexer_for_mimetype(content_type) lexer = get_lexer_for_mimetype(content_type)
except ClassNotFound: except ClassNotFound:
return content return content
if content_type == 'application/json': if content_type == "application/json":
try: try:
# Indent and sort the JSON data. # Indent and sort the JSON data.
content = json.dumps(json.loads(content), content = json.dumps(json.loads(content),