Lexer handling.

This commit is contained in:
Martin Mahner 2013-03-19 19:43:38 +01:00
parent f7916a6960
commit e7e38d409d
5 changed files with 31 additions and 39 deletions

View file

@ -2,7 +2,7 @@ from django import forms
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from dpaste.models import Snippet 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 import datetime
#=============================================================================== #===============================================================================
@ -21,7 +21,6 @@ EXPIRE_DEFAULT = 3600 * 24 * 30
class SnippetForm(forms.ModelForm): class SnippetForm(forms.ModelForm):
lexer = forms.ChoiceField( lexer = forms.ChoiceField(
label=_(u'Lexer'), label=_(u'Lexer'),
choices=LEXER_LIST,
initial=LEXER_DEFAULT, initial=LEXER_DEFAULT,
widget=forms.TextInput, widget=forms.TextInput,
) )
@ -42,23 +41,26 @@ class SnippetForm(forms.ModelForm):
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
super(SnippetForm, self).__init__(*args, **kwargs) super(SnippetForm, self).__init__(*args, **kwargs)
self.request = request 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: def clean_lexer(self):
if self.request.session['userprefs'].get('display_all_lexer', False): lexer = self.cleaned_data.get('lexer')
self.fields['lexer'].choices = LEXER_LIST_ALL if not lexer:
except KeyError: return LEXER_DEFAULT
pass lexer = dict(LEXER_LIST).get(lexer, LEXER_DEFAULT)
return lexer
try:
self.fields['author'].initial = self.request.session['userprefs'].get('default_name', '')
except KeyError:
pass
def clean(self): def clean(self):
if self.cleaned_data.get('title'): if self.cleaned_data.get('title'):
raise forms.ValidationError('This snippet was identified as Spam.') raise forms.ValidationError('This snippet was identified as Spam.')
return self.cleaned_data return self.cleaned_data
def save(self, parent=None, *args, **kwargs): def save(self, parent=None, *args, **kwargs):
# Set parent snippet # Set parent snippet

View file

@ -8,26 +8,15 @@ from django.utils.html import escape
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
LEXER_LIST_ALL = sorted([(i[1][0], i[0]) for i in get_all_lexers()]) # Python 3: python3
LEXER_LIST = ( LEXER_LIST = sorted([(i[0], i[0]) for i in get_all_lexers() if not (
('bash', 'Bash'), '+' in i[0] or
('c', 'C'), 'with' in i[0].lower() or
('css', 'CSS'), i[0].islower()
('diff', 'Diff'), )])
('django', 'Django/Jinja'), LEXER_LIST_NAME = dict([(i[0], i[1][0]) for i in get_all_lexers()])
('html', 'HTML'),
('irc', 'IRC logs'), LEXER_DEFAULT = 'Python'
('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'
LEXER_WORDWRAP = ('text', 'rst') LEXER_WORDWRAP = ('text', 'rst')
class NakedHtmlFormatter(HtmlFormatter): class NakedHtmlFormatter(HtmlFormatter):
@ -39,6 +28,9 @@ class NakedHtmlFormatter(HtmlFormatter):
yield i, t yield i, t
def pygmentize(code_string, lexer_name=LEXER_DEFAULT): def pygmentize(code_string, lexer_name=LEXER_DEFAULT):
lexer_name = LEXER_LIST_NAME.get(lexer_name, None)
print lexer_name
try: try:
if lexer_name: if lexer_name:
lexer = get_lexer_by_name(lexer_name) lexer = get_lexer_by_name(lexer_name)

View file

@ -142,7 +142,7 @@ ol.linenums {
} }
ol.linenums li { ol.linenums li {
color: #555; color: #888;
font-size: 12px; font-size: 12px;
line-height: 20px; line-height: 20px;
cursor: pointer; cursor: pointer;

View file

@ -11,16 +11,14 @@
<div class="control-group form-options"> <div class="control-group form-options">
<div class="form-options-lexer"> <div class="form-options-lexer">
{% if request.session.userprefs.display_all_lexer %} {{ snippet_form.lexer.errors }}
<div class="input-append"> <div class="input-append">
{{ snippet_form.lexer }} {{ snippet_form.lexer }}
<button class="btn" id="guess_lexer_btn" type="button">{% trans "Guess lexer" %}</button> <button class="btn" id="guess_lexer_btn" type="button">{% trans "Guess lexer" %}</button>
</div> </div>
{% else %}
{{ snippet_form.lexer }}
{% endif %}
</div> </div>
<div class="form-options-expire"> <div class="form-options-expire">
{{ snippet_form.expire_options.errors }}
<div class="input-prepend"> <div class="input-prepend">
<span class="add-on"><i class="icon-trash" title="{% trans "Expire in" %}"></i></span> <span class="add-on"><i class="icon-trash" title="{% trans "Expire in" %}"></i></span>
{{ snippet_form.expire_options }} {{ snippet_form.expire_options }}

View file

@ -12,8 +12,8 @@ from django.views.defaults import page_not_found as django_page_not_found, \
from dpaste.forms import SnippetForm from dpaste.forms import SnippetForm
from dpaste.models import Snippet from dpaste.models import Snippet
from dpaste.highlight import pygmentize, guess_code_lexer, \ from dpaste.highlight import guess_code_lexer, \
LEXER_WORDWRAP, LEXER_LIST_ALL, LEXER_LIST LEXER_WORDWRAP, LEXER_LIST
import difflib import difflib