mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-12-19 00:09:21 +11:00
Lexer handling.
This commit is contained in:
parent
f7916a6960
commit
e7e38d409d
5 changed files with 31 additions and 39 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -142,7 +142,7 @@ ol.linenums {
|
|||
}
|
||||
|
||||
ol.linenums li {
|
||||
color: #555;
|
||||
color: #888;
|
||||
font-size: 12px;
|
||||
line-height: 20px;
|
||||
cursor: pointer;
|
||||
|
|
|
@ -11,16 +11,14 @@
|
|||
|
||||
<div class="control-group form-options">
|
||||
<div class="form-options-lexer">
|
||||
{% if request.session.userprefs.display_all_lexer %}
|
||||
{{ snippet_form.lexer.errors }}
|
||||
<div class="input-append">
|
||||
{{ snippet_form.lexer }}
|
||||
<button class="btn" id="guess_lexer_btn" type="button">{% trans "Guess lexer" %}</button>
|
||||
</div>
|
||||
{% else %}
|
||||
{{ snippet_form.lexer }}
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="form-options-expire">
|
||||
{{ snippet_form.expire_options.errors }}
|
||||
<div class="input-prepend">
|
||||
<span class="add-on"><i class="icon-trash" title="{% trans "Expire in" %}"></i></span>
|
||||
{{ snippet_form.expire_options }}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue