diff --git a/client/scss/_globals.scss b/client/scss/_globals.scss index 79e1190..1723291 100644 --- a/client/scss/_globals.scss +++ b/client/scss/_globals.scss @@ -6,8 +6,6 @@ body { font-weight: $baseFontRegular; } -body[data-code-snippet] { background-color: $codeBgColor; } - body[data-platform=win] .platform-mac { display: none; } body[data-platform=mac] .platform-win { display: none; } diff --git a/client/scss/components/_code.scss b/client/scss/components/_code.scss index b5cae5d..ca9f571 100644 --- a/client/scss/components/_code.scss +++ b/client/scss/components/_code.scss @@ -1,15 +1,34 @@ +.snippet-diff { + color: $codeTextColor; + background-color: $codeBgColor; + + h2 { + padding: 20px $boxPadding 10px $boxPadding; + color: $codeTextColor; + font-weight: $baseFontDemiBold; + font-size: 14px; + } + + .snippet-code { + background-color: $codeDiffBgColor; + padding: 20px $boxPadding 20px $boxPadding; + white-space: pre; + font-size: 12px; + } +} .snippet-code { + padding: 20px $boxPadding; + font-family: $codeFont; font-size: 13px; font-weight: 300; line-height: 20px; //overflow: auto; + color: $codeTextColor; background-color: $codeBgColor; - padding: 20px $boxPadding; - &.wordwrap { overflow: auto; li { white-space: pre-wrap !important; } @@ -43,9 +62,16 @@ } } + @mixin diffline { + display: inline-block; + width: 100%; + margin: 0 -10px; + padding: 0 10px; + + } // Pygments - .gd { background-color: rgba(226, 12, 19, .3); color: #fff; display: block; } - .gi { background-color: rgba(23, 189, 10, .2); color: #fff; display: block; } + .gd { background-color: #473335; color: #f8f8f2; @include diffline; } + .gi { background-color: #2d4a39; color: #f8f8f2; @include diffline; } .hll { background-color: #49483e } .c { color: #75715e } /* Comment */ diff --git a/dpaste/templates/dpaste/details.html b/dpaste/templates/dpaste/details.html index a7a8427..37246db 100644 --- a/dpaste/templates/dpaste/details.html +++ b/dpaste/templates/dpaste/details.html @@ -4,10 +4,6 @@ {% block title %}dpaste/{{ snippet.secret_id }} ({{ snippet.lexer }}){% endblock %} -{% block body_type %} - {% if snippet.lexer == 'text' %}data-text-snippet{% else %}data-code-snippet{% endif %} -{% endblock %} - {% block headline %} {{ request.build_absolute_uri }} @@ -71,6 +67,13 @@
{% endif %} + {% if diff %} +{% trans "No snippets saved. Either all your snippets are expired or your cookie has changed." %}
{% endfor %} - - - {% endblock %} diff --git a/dpaste/views.py b/dpaste/views.py index e253647..e524472 100644 --- a/dpaste/views.py +++ b/dpaste/views.py @@ -5,14 +5,12 @@ import difflib import json from django.conf import settings -from django.core.exceptions import ObjectDoesNotExist from django.db.models import Count from django.http import (Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect) from django.shortcuts import get_object_or_404 from django.urls import reverse -from django.utils.encoding import force_text -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext from django.views.defaults import page_not_found as django_page_not_found, \ server_error as django_server_error from django.views.generic import FormView @@ -76,13 +74,12 @@ class SnippetDetailView(SnippetView, DetailView): if 'delete' in self.request.POST: snippet = get_object_or_404(Snippet, secret_id=self.kwargs['snippet_id']) snippet.delete() - + # Append `#` so #delete goes away in Firefox url = '{0}#'.format(reverse('snippet_new')) return HttpResponseRedirect(url) - + return super(SnippetDetailView, self).post(*args, **kwargs) - def get(self, *args, **kwargs): snippet = self.get_object() @@ -110,12 +107,36 @@ class SnippetDetailView(SnippetView, DetailView): snippet = form.save(parent=self.get_object()) return HttpResponseRedirect(snippet.get_absolute_url()) + def get_snippet_diff(self): + snippet = self.get_object() + + if not snippet.parent_id: + return None + + if snippet.content == snippet.parent.content: + return None + + d = difflib.unified_diff( + snippet.parent.content.splitlines(), + snippet.content.splitlines(), + ugettext('Previous Snippet'), + ugettext('Current Snippet'), + n=1 + ) + diff_code = '\n'.join(d).strip() + highlighted = highlight.pygmentize(diff_code, lexer_name='diff') + + # Remove blank lines + return highlighted.replace('\n\n', '\n') + + def get_context_data(self, **kwargs): - self.object = snippet = self.get_object() + snippet = self.get_object() ctx = super(SnippetDetailView, self).get_context_data(**kwargs) ctx.update({ 'wordwrap': snippet.lexer in highlight.LEXER_WORDWRAP, + 'diff': self.get_snippet_diff(), }) return ctx @@ -164,69 +185,6 @@ class SnippetHistory(TemplateView): return ctx -# class SnippetDiffView(TemplateView): -# """ -# Display a diff between two given snippet secret ids. -# """ -# template_name = 'dpaste/includes/diff.html' -# -# def get(self, request, *args, **kwargs): -# """ -# Some validation around input files we will compare later. -# """ -# if request.GET.get('a') and request.GET.get('a').isdigit() \ -# and request.GET.get('b') and request.GET.get('b').isdigit(): -# try: -# self.fileA = Snippet.objects.get(pk=int(request.GET.get('a'))) -# self.fileB = Snippet.objects.get(pk=int(request.GET.get('b'))) -# except ObjectDoesNotExist: -# return HttpResponseBadRequest(u'Selected file(s) does not exist.') -# else: -# return HttpResponseBadRequest(u'You must select two snippets.') -# -# return super(SnippetDiffView, self).get(request, *args, **kwargs) -# -# def get_diff(self): -# class DiffText(object): -# pass -# -# diff = DiffText() -# -# if self.fileA.content != self.fileB.content: -# d = difflib.unified_diff( -# self.fileA.content.splitlines(), -# self.fileB.content.splitlines(), -# 'Original', -# 'Current', -# lineterm='' -# ) -# -# diff.content = '\n'.join(d).strip() -# diff.lexer = 'diff' -# else: -# diff.content = force_text(_(u'No changes were made between this two files.')) -# diff.lexer = 'text' -# -# return diff -# -# def highlight_snippet(self, content): -# h = highlight.pygmentize(content, 'diff') -# h = h.replace(u'\t', ' ') -# return h -# -# 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.update({ -# 'snippet': diff, -# 'highlighted': highlighted.splitlines(), -# 'fileA': self.fileA, -# 'fileB': self.fileB, -# }) -# return ctx - - # ----------------------------------------------------------------------------- # Static pages # -----------------------------------------------------------------------------