From 32006e0bd055f751f5eea939ea92306d9cb2b698 Mon Sep 17 00:00:00 2001 From: Alexander Kojevnikov Date: Sat, 29 Sep 2012 19:41:55 -0700 Subject: [PATCH] api: Pass lexer options --- main.py | 16 ++++++++++++++-- templates/api.txt | 1 + tools.py | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 5faa2b5..40a6e94 100755 --- a/main.py +++ b/main.py @@ -49,7 +49,7 @@ def index(): 'divstyles', unquote(request.cookies.get('divstyles', ''))) divstyles = update_styles(style, divstyles) - html = hilite_me(code, lexer, style, linenos, divstyles) + html = hilite_me(code, lexer, {}, style, linenos, divstyles) response = make_response(render_template('index.html', **locals())) next_year = datetime.datetime.now() + datetime.timedelta(days=365) @@ -69,11 +69,23 @@ def api(): return response lexer = request.values.get('lexer', '') + options = request.values.get('options', '') + + def convert(item): + key, value = item + if value == 'False': + return key, False + elif value == 'True': + return key, True + else: + return key, value + options = dict(convert(option.split('=')) for option in options.split(',') if option) + style = request.values.get('style', '') linenos = request.values.get('linenos', '') divstyles = update_styles(style, request.form.get('divstyles', '')) - html = hilite_me(code, lexer, style, linenos, divstyles) + html = hilite_me(code, lexer, options, style, linenos, divstyles) response = make_response(html) response.headers["Content-Type"] = "text/plain" return response diff --git a/templates/api.txt b/templates/api.txt index e8d7814..1f964c4 100644 --- a/templates/api.txt +++ b/templates/api.txt @@ -4,6 +4,7 @@ GET or POST to http://hilite.me/api with these parameters: * code: source code to format * lexer: [lexer](http://pygments.org/docs/lexers/) to use, default it 'python' +* options: optional comma-separated list of lexer options * style: [style](http://pygments.org/docs/styles/) to use, default is 'colorful' * linenos: if not empty, the HTML will include line numbers * divstyles: CSS style to use in the wrapping
element, can be empty diff --git a/tools.py b/tools.py index b192d13..13dd3c9 100644 --- a/tools.py +++ b/tools.py @@ -21,7 +21,7 @@ from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter -def hilite_me(code, lexer, style, linenos, divstyles): +def hilite_me(code, lexer, options, style, linenos, divstyles): lexer = lexer or 'python' style = style or 'colorful' defstyles = 'overflow:auto;width:auto;' @@ -32,7 +32,7 @@ def hilite_me(code, lexer, style, linenos, divstyles): cssclass='', cssstyles=defstyles + divstyles, prestyles='margin: 0') - html = highlight(code, get_lexer_by_name(lexer), formatter) + html = highlight(code, get_lexer_by_name(lexer, **options), formatter) if linenos: html = insert_line_numbers(html) html = "" + html