2012-03-02 01:39:22 +01:00
|
|
|
import os
|
2012-02-25 13:39:38 +01:00
|
|
|
import json
|
|
|
|
import pygments
|
2012-03-05 18:58:21 +01:00
|
|
|
import re
|
2012-03-02 01:39:22 +01:00
|
|
|
from pygments import token
|
|
|
|
from pygments.util import ClassNotFound
|
2012-02-25 13:39:38 +01:00
|
|
|
from pygments.lexers import get_lexer_for_mimetype
|
|
|
|
from pygments.formatters.terminal256 import Terminal256Formatter
|
2012-02-29 21:21:43 +01:00
|
|
|
from pygments.formatters.terminal import TerminalFormatter
|
2012-03-05 18:58:21 +01:00
|
|
|
from pygments.lexer import include, RegexLexer, bygroups
|
2012-03-02 01:39:22 +01:00
|
|
|
from pygments.styles import get_style_by_name, STYLE_MAP
|
2012-02-26 16:13:12 +01:00
|
|
|
from . import solarized
|
2012-02-25 13:39:38 +01:00
|
|
|
|
|
|
|
|
2012-03-02 01:39:22 +01:00
|
|
|
DEFAULT_STYLE = 'solarized'
|
|
|
|
AVAILABLE_STYLES = [DEFAULT_STYLE] + STYLE_MAP.keys()
|
2012-02-25 13:39:38 +01:00
|
|
|
TYPE_JS = 'application/javascript'
|
2012-03-02 01:39:22 +01:00
|
|
|
FORMATTER = (Terminal256Formatter
|
2012-03-04 16:40:02 +01:00
|
|
|
if '256color' in os.environ.get('TERM', '')
|
2012-03-02 01:39:22 +01:00
|
|
|
else TerminalFormatter)
|
2012-02-25 13:39:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPLexer(RegexLexer):
|
|
|
|
name = 'HTTP'
|
|
|
|
aliases = ['http']
|
|
|
|
filenames = ['*.http']
|
|
|
|
tokens = {
|
|
|
|
'root': [
|
|
|
|
(r'\s+', token.Text),
|
|
|
|
(r'(HTTP/[\d.]+\s+)(\d+)(\s+.+)', bygroups(
|
|
|
|
token.Operator, token.Number, token.String)),
|
|
|
|
(r'(.*?:)(.+)', bygroups(token.Name, token.String))
|
|
|
|
]}
|
|
|
|
|
2012-03-05 18:58:21 +01:00
|
|
|
# Stolen from https://github.com/orb/pygments-json
|
|
|
|
class JSONLexer(RegexLexer):
|
|
|
|
name = 'JSON Lexer'
|
|
|
|
aliases = ['json']
|
|
|
|
filenames = ['*.json']
|
|
|
|
mimetypes = []
|
|
|
|
|
|
|
|
|
|
|
|
flags = re.DOTALL
|
|
|
|
tokens = {
|
|
|
|
'whitespace': [
|
|
|
|
(r'\s+', token.Text),
|
|
|
|
],
|
|
|
|
|
|
|
|
# represents a simple terminal value
|
|
|
|
'simplevalue':[
|
|
|
|
(r'(true|false|null)\b', token.Keyword.Constant),
|
|
|
|
(r'-?[0-9]+', token.Number.Integer),
|
|
|
|
(r'"(\\\\|\\"|[^"])*"', token.String.Double),
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
# the right hand side of an object, after the attribute name
|
|
|
|
'objectattribute': [
|
|
|
|
include('value'),
|
|
|
|
(r':', token.Punctuation),
|
|
|
|
# comma terminates the attribute but expects more
|
|
|
|
(r',', token.Punctuation, '#pop'),
|
|
|
|
# a closing bracket terminates the entire object, so pop twice
|
|
|
|
(r'}', token.Punctuation, ('#pop', '#pop')),
|
|
|
|
],
|
|
|
|
|
|
|
|
# a json object - { attr, attr, ... }
|
|
|
|
'objectvalue': [
|
|
|
|
include('whitespace'),
|
|
|
|
(r'"(\\\\|\\"|[^"])*"', token.Name.Tag, 'objectattribute'),
|
|
|
|
(r'}', token.Punctuation, '#pop'),
|
|
|
|
],
|
|
|
|
|
|
|
|
# json array - [ value, value, ... }
|
|
|
|
'arrayvalue': [
|
|
|
|
include('whitespace'),
|
|
|
|
include('value'),
|
|
|
|
(r',', token.Punctuation),
|
|
|
|
(r']', token.Punctuation, '#pop'),
|
|
|
|
],
|
|
|
|
|
|
|
|
# a json value - either a simple value or a complex value (object or array)
|
|
|
|
'value': [
|
|
|
|
include('whitespace'),
|
|
|
|
include('simplevalue'),
|
|
|
|
(r'{', token.Punctuation, 'objectvalue'),
|
|
|
|
(r'\[', token.Punctuation, 'arrayvalue'),
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
# the root of a json document would be a value
|
|
|
|
'root': [
|
|
|
|
include('value'),
|
|
|
|
],
|
|
|
|
|
|
|
|
}
|
2012-02-29 21:21:43 +01:00
|
|
|
|
2012-03-02 01:39:22 +01:00
|
|
|
class PrettyHttp(object):
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-03-02 01:39:22 +01:00
|
|
|
def __init__(self, style_name):
|
|
|
|
if style_name == 'solarized':
|
|
|
|
style = solarized.SolarizedStyle
|
|
|
|
else:
|
|
|
|
style = get_style_by_name(style_name)
|
|
|
|
self.formatter = FORMATTER(style=style)
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-03-02 01:39:22 +01:00
|
|
|
def headers(self, content):
|
2012-03-04 13:03:21 +01:00
|
|
|
return pygments.highlight(content, HTTPLexer(), self.formatter)
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-03-02 01:39:22 +01:00
|
|
|
def body(self, content, content_type):
|
|
|
|
content_type = content_type.split(';')[0]
|
|
|
|
if 'json' in content_type:
|
|
|
|
content_type = TYPE_JS
|
|
|
|
try:
|
|
|
|
# Indent JSON
|
|
|
|
content = json.dumps(json.loads(content),
|
|
|
|
sort_keys=True, indent=4)
|
2012-03-05 18:58:21 +01:00
|
|
|
lexer = JSONLexer()
|
2012-03-02 01:39:22 +01:00
|
|
|
except Exception:
|
|
|
|
pass
|
2012-03-05 18:58:21 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
lexer = get_lexer_for_mimetype(content_type)
|
|
|
|
except ClassNotFound:
|
|
|
|
return content
|
2012-03-02 01:39:22 +01:00
|
|
|
content = pygments.highlight(content, lexer, self.formatter)
|
2012-03-04 13:03:21 +01:00
|
|
|
return content
|