mirror of
https://github.com/httpie/cli.git
synced 2024-12-25 15:58:59 +01:00
Use the Pygments HTTP and JSON lexers
This commit is contained in:
parent
3d11042772
commit
2d7f2c65a2
@ -3,12 +3,10 @@ import json
|
||||
import pygments
|
||||
from pygments import token
|
||||
from pygments.util import ClassNotFound
|
||||
from pygments.lexers import get_lexer_for_mimetype
|
||||
from pygments.lexers import get_lexer_for_mimetype, HttpLexer
|
||||
from pygments.formatters.terminal256 import Terminal256Formatter
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
from pygments.lexer import RegexLexer, bygroups
|
||||
from pygments.styles import get_style_by_name, STYLE_MAP
|
||||
from .pygson import JSONLexer
|
||||
from . import solarized
|
||||
|
||||
|
||||
@ -18,25 +16,6 @@ FORMATTER = (Terminal256Formatter
|
||||
if '256color' in os.environ.get('TERM', '')
|
||||
else TerminalFormatter)
|
||||
|
||||
|
||||
class HTTPLexer(RegexLexer):
|
||||
name = 'HTTP'
|
||||
aliases = ['http']
|
||||
filenames = ['*.http']
|
||||
tokens = {
|
||||
'root': [
|
||||
(r'\s+', token.Text),
|
||||
# Request-Line
|
||||
(r'([A-Z]+\s+)(/.*?)(\s+HTTP/[\d.]+)', bygroups(
|
||||
token.Keyword, token.String, token.Keyword)),
|
||||
# Status-Line
|
||||
(r'(HTTP/[\d.]+\s+)(\d+)(\s+.+)', bygroups(
|
||||
token.Keyword, token.Number, token.String)),
|
||||
# Header
|
||||
(r'(.*?:)(.+)', bygroups(token.Name, token.Keyword))
|
||||
]}
|
||||
|
||||
|
||||
class PrettyHttp(object):
|
||||
|
||||
def __init__(self, style_name):
|
||||
@ -47,22 +26,22 @@ class PrettyHttp(object):
|
||||
self.formatter = FORMATTER(style=style)
|
||||
|
||||
def headers(self, content):
|
||||
return pygments.highlight(content, HTTPLexer(), self.formatter)
|
||||
return pygments.highlight(content, HttpLexer(), self.formatter)
|
||||
|
||||
def body(self, content, content_type):
|
||||
lexer = None
|
||||
content_type = content_type.split(';')[0]
|
||||
|
||||
try:
|
||||
lexer = get_lexer_for_mimetype(content_type)
|
||||
except ClassNotFound:
|
||||
return content
|
||||
|
||||
if 'json' in content_type:
|
||||
lexer = JSONLexer()
|
||||
try:
|
||||
# Indent the JSON data.
|
||||
content = json.dumps(json.loads(content),
|
||||
sort_keys=True, indent=4)
|
||||
except Exception:
|
||||
sort_keys=True, indent=4)
|
||||
except:
|
||||
pass
|
||||
if not lexer:
|
||||
try:
|
||||
lexer = get_lexer_for_mimetype(content_type)
|
||||
except ClassNotFound:
|
||||
return content
|
||||
|
||||
return pygments.highlight(content, lexer, self.formatter)
|
||||
|
@ -1,77 +0,0 @@
|
||||
"""
|
||||
JSON lexer by Norman Richards
|
||||
|
||||
It's already been merged into Pygments but not released yet,
|
||||
so we are temporarily bundling it with HTTPie.
|
||||
|
||||
It can be removed once Pygments > 1.4 has been released.
|
||||
|
||||
See <https://github.com/jkbr/httpie/pull/25> for more details.
|
||||
|
||||
"""
|
||||
import re
|
||||
from pygments import token
|
||||
from pygments.lexer import RegexLexer, include
|
||||
|
||||
|
||||
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'),
|
||||
],
|
||||
|
||||
}
|
2
setup.py
2
setup.py
@ -9,7 +9,7 @@ if sys.argv[-1] == 'test':
|
||||
sys.exit()
|
||||
|
||||
|
||||
requirements = ['requests>=0.10.1', 'Pygments>=1.4']
|
||||
requirements = ['requests>=0.10.1', 'Pygments>=1.5']
|
||||
if sys.version_info[:2] in ((2, 6), (3, 1)):
|
||||
# argparse has been added in Python 3.2 / 2.7
|
||||
requirements.append('argparse>=1.2.1')
|
||||
|
Loading…
Reference in New Issue
Block a user