mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-15 08:02:54 +11:00
Moved code highlighting to view.
This commit is contained in:
parent
6090c37f17
commit
3eba19db02
4 changed files with 20 additions and 17 deletions
|
@ -52,9 +52,6 @@ class Snippet(models.Model):
|
||||||
ordering = ('-published',)
|
ordering = ('-published',)
|
||||||
db_table = 'dpaste_snippet'
|
db_table = 'dpaste_snippet'
|
||||||
|
|
||||||
def get_linecount(self):
|
|
||||||
return len(self.content.splitlines())
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def remaining_views(self):
|
def remaining_views(self):
|
||||||
if self.expire_type == self.EXPIRE_ONETIME:
|
if self.expire_type == self.EXPIRE_ONETIME:
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{% load dpaste_tags %}<div class="code {{ snippet.lexer }}"><ol>{% for line in snippet|highlight %}<li id="{{ forloop.counter }}">{{ line|safe|default:" " }}</li>{% endfor %}</ol></div>
|
{% load dpaste_tags %}<div class="code {{ snippet.lexer }}"><ol>{% for line in highlighted %}<li id="{{ forloop.counter }}">{{ line|safe|default:" " }}</li>{% endfor %}</ol></div>
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
from django.template import Library
|
from django.template import Library
|
||||||
|
|
||||||
from ..highlight import pygmentize
|
|
||||||
|
|
||||||
register = Library()
|
register = Library()
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def in_list(value, arg):
|
def in_list(value, arg):
|
||||||
return value in arg
|
return value in arg
|
||||||
|
|
||||||
@register.filter
|
|
||||||
def highlight(snippet):
|
|
||||||
h = pygmentize(snippet.content, snippet.lexer)
|
|
||||||
h = h.replace(u' ', u' ')
|
|
||||||
h = h.replace(u'\t', ' ')
|
|
||||||
return h.splitlines()
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ import datetime
|
||||||
import difflib
|
import difflib
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import requests
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
@ -22,7 +21,7 @@ from pygments.util import ClassNotFound
|
||||||
|
|
||||||
from .forms import EXPIRE_CHOICES, get_expire_values, SnippetForm
|
from .forms import EXPIRE_CHOICES, get_expire_values, SnippetForm
|
||||||
from .highlight import (LEXER_DEFAULT, LEXER_KEYS, LEXER_LIST,
|
from .highlight import (LEXER_DEFAULT, LEXER_KEYS, LEXER_LIST,
|
||||||
LEXER_WORDWRAP, PLAIN_CODE)
|
LEXER_WORDWRAP, PLAIN_CODE, pygmentize)
|
||||||
from .models import ONETIME_LIMIT, Snippet
|
from .models import ONETIME_LIMIT, Snippet
|
||||||
|
|
||||||
template_globals = {
|
template_globals = {
|
||||||
|
@ -107,13 +106,18 @@ class SnippetDetailView(SnippetView, DetailView):
|
||||||
ctx = super(SnippetDetailView, self).get_context_data(**kwargs)
|
ctx = super(SnippetDetailView, self).get_context_data(**kwargs)
|
||||||
ctx.update(template_globals)
|
ctx.update(template_globals)
|
||||||
ctx.update({
|
ctx.update({
|
||||||
'lines': range(snippet.get_linecount()),
|
'highlighted': self.highlight_snippet().splitlines(),
|
||||||
'tree': tree,
|
'tree': tree,
|
||||||
'wordwrap': snippet.lexer in LEXER_WORDWRAP and 'True' or 'False',
|
'wordwrap': snippet.lexer in LEXER_WORDWRAP and 'True' or 'False',
|
||||||
'gist': getattr(settings, 'DPASTE_ENABLE_GIST', True),
|
|
||||||
})
|
})
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
def highlight_snippet(self):
|
||||||
|
snippet = self.get_object()
|
||||||
|
h = pygmentize(snippet.content, snippet.lexer)
|
||||||
|
h = h.replace(u' ', u' ')
|
||||||
|
h = h.replace(u'\t', ' ')
|
||||||
|
return h
|
||||||
|
|
||||||
class SnippetRawView(SnippetDetailView):
|
class SnippetRawView(SnippetDetailView):
|
||||||
"""
|
"""
|
||||||
|
@ -216,11 +220,20 @@ class SnippetDiffView(TemplateView):
|
||||||
|
|
||||||
return diff
|
return diff
|
||||||
|
|
||||||
|
def highlight_snippet(self, content):
|
||||||
|
h = pygmentize(content, 'diff')
|
||||||
|
h = h.replace(u' ', u' ')
|
||||||
|
h = h.replace(u'\t', ' ')
|
||||||
|
return h
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
|
diff = self.get_diff()
|
||||||
|
highlighted = self.highlight_snippet(diff.content)
|
||||||
ctx = super(SnippetDiffView, self).get_context_data(**kwargs)
|
ctx = super(SnippetDiffView, self).get_context_data(**kwargs)
|
||||||
ctx.update(template_globals)
|
ctx.update(template_globals)
|
||||||
ctx.update({
|
ctx.update({
|
||||||
'snippet': self.get_diff(),
|
'snippet': diff,
|
||||||
|
'highlighted': highlighted.splitlines(),
|
||||||
'fileA': self.fileA,
|
'fileA': self.fileA,
|
||||||
'fileB': self.fileB,
|
'fileB': self.fileB,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue