From e7e38d409df04df098c6185acdd936363ac8d91b Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 19 Mar 2013 19:43:38 +0100 Subject: [PATCH] Lexer handling. --- dpaste/forms.py | 26 +++++++++--------- dpaste/highlight.py | 32 +++++++++-------------- dpaste/static/dpaste/theme.css | 2 +- dpaste/templates/dpaste/snippet_form.html | 6 ++--- dpaste/views.py | 4 +-- 5 files changed, 31 insertions(+), 39 deletions(-) diff --git a/dpaste/forms.py b/dpaste/forms.py index c2e345f..8e1e0ab 100644 --- a/dpaste/forms.py +++ b/dpaste/forms.py @@ -2,7 +2,7 @@ from django import forms from django.conf import settings from django.utils.translation import ugettext_lazy as _ from dpaste.models import Snippet -from dpaste.highlight import LEXER_LIST_ALL, LEXER_LIST, LEXER_DEFAULT +from dpaste.highlight import LEXER_LIST, LEXER_DEFAULT import datetime #=============================================================================== @@ -21,7 +21,6 @@ EXPIRE_DEFAULT = 3600 * 24 * 30 class SnippetForm(forms.ModelForm): lexer = forms.ChoiceField( label=_(u'Lexer'), - choices=LEXER_LIST, initial=LEXER_DEFAULT, widget=forms.TextInput, ) @@ -42,23 +41,26 @@ class SnippetForm(forms.ModelForm): def __init__(self, request, *args, **kwargs): super(SnippetForm, self).__init__(*args, **kwargs) self.request = request + self.fields['lexer'].choices = LEXER_LIST + self.fields['lexer'].widget.attrs = { + 'autocomplete': 'off', + 'data-provide': 'typeahead', + 'data-source': '["%s"]' % '","'.join(dict(LEXER_LIST).keys()) + } - try: - if self.request.session['userprefs'].get('display_all_lexer', False): - self.fields['lexer'].choices = LEXER_LIST_ALL - except KeyError: - pass - - try: - self.fields['author'].initial = self.request.session['userprefs'].get('default_name', '') - except KeyError: - pass + def clean_lexer(self): + lexer = self.cleaned_data.get('lexer') + if not lexer: + return LEXER_DEFAULT + lexer = dict(LEXER_LIST).get(lexer, LEXER_DEFAULT) + return lexer def clean(self): if self.cleaned_data.get('title'): raise forms.ValidationError('This snippet was identified as Spam.') return self.cleaned_data + def save(self, parent=None, *args, **kwargs): # Set parent snippet diff --git a/dpaste/highlight.py b/dpaste/highlight.py index b43794a..5e25084 100644 --- a/dpaste/highlight.py +++ b/dpaste/highlight.py @@ -8,26 +8,15 @@ from django.utils.html import escape import logging logger = logging.getLogger(__name__) -LEXER_LIST_ALL = sorted([(i[1][0], i[0]) for i in get_all_lexers()]) -LEXER_LIST = ( - ('bash', 'Bash'), - ('c', 'C'), - ('css', 'CSS'), - ('diff', 'Diff'), - ('django', 'Django/Jinja'), - ('html', 'HTML'), - ('irc', 'IRC logs'), - ('js', 'JavaScript'), - ('php', 'PHP'), - ('pycon', 'Python console session'), - ('pytb', 'Python Traceback'), - ('python', 'Python'), - ('python3', 'Python 3'), - ('rst', 'Restructured Text'), - ('sql', 'SQL'), - ('text', 'Text only'), -) -LEXER_DEFAULT = 'python' +# Python 3: python3 +LEXER_LIST = sorted([(i[0], i[0]) for i in get_all_lexers() if not ( + '+' in i[0] or + 'with' in i[0].lower() or + i[0].islower() +)]) +LEXER_LIST_NAME = dict([(i[0], i[1][0]) for i in get_all_lexers()]) + +LEXER_DEFAULT = 'Python' LEXER_WORDWRAP = ('text', 'rst') class NakedHtmlFormatter(HtmlFormatter): @@ -39,6 +28,9 @@ class NakedHtmlFormatter(HtmlFormatter): yield i, t def pygmentize(code_string, lexer_name=LEXER_DEFAULT): + lexer_name = LEXER_LIST_NAME.get(lexer_name, None) + + print lexer_name try: if lexer_name: lexer = get_lexer_by_name(lexer_name) diff --git a/dpaste/static/dpaste/theme.css b/dpaste/static/dpaste/theme.css index c87eeaf..773b3f2 100644 --- a/dpaste/static/dpaste/theme.css +++ b/dpaste/static/dpaste/theme.css @@ -142,7 +142,7 @@ ol.linenums { } ol.linenums li { - color: #555; + color: #888; font-size: 12px; line-height: 20px; cursor: pointer; diff --git a/dpaste/templates/dpaste/snippet_form.html b/dpaste/templates/dpaste/snippet_form.html index aae8812..4702367 100644 --- a/dpaste/templates/dpaste/snippet_form.html +++ b/dpaste/templates/dpaste/snippet_form.html @@ -11,16 +11,14 @@
- {% if request.session.userprefs.display_all_lexer %} + {{ snippet_form.lexer.errors }}
{{ snippet_form.lexer }}
- {% else %} - {{ snippet_form.lexer }} - {% endif %}
+ {{ snippet_form.expire_options.errors }}
{{ snippet_form.expire_options }} diff --git a/dpaste/views.py b/dpaste/views.py index e5b67d8..971d0b2 100644 --- a/dpaste/views.py +++ b/dpaste/views.py @@ -12,8 +12,8 @@ from django.views.defaults import page_not_found as django_page_not_found, \ from dpaste.forms import SnippetForm from dpaste.models import Snippet -from dpaste.highlight import pygmentize, guess_code_lexer, \ - LEXER_WORDWRAP, LEXER_LIST_ALL, LEXER_LIST +from dpaste.highlight import guess_code_lexer, \ + LEXER_WORDWRAP, LEXER_LIST import difflib