Permalink
Browse files

api: Pass lexer options

  • Loading branch information...
1 parent 39787e4 commit 32006e0bd055f751f5eea939ea92306d9cb2b698 @alexkay committed Sep 30, 2012
Showing with 17 additions and 4 deletions.
  1. +14 −2 main.py
  2. +1 −0 templates/api.txt
  3. +2 −2 tools.py
View
@@ -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
View
@@ -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 <div> element, can be empty
View
@@ -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 generated using hilite.me -->" + html

0 comments on commit 32006e0

Please sign in to comment.