Skip to content

Commit

Permalink
api: Pass lexer options
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkay committed Sep 30, 2012
1 parent 39787e4 commit 32006e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
16 changes: 14 additions & 2 deletions main.py
Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions templates/api.txt
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tools.py
Expand Up @@ -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;'
Expand All @@ -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
Expand Down

0 comments on commit 32006e0

Please sign in to comment.