Bring back diff view.

This commit is contained in:
Martin Mahner 2018-03-27 20:54:13 +02:00
parent dd0797dd98
commit c65f225612
5 changed files with 65 additions and 86 deletions

View file

@ -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; }

View file

@ -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 */

View file

@ -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 %}
<a href="{{ request.build_absolute_uri }}">{{ request.build_absolute_uri }}</a>
@ -71,6 +67,13 @@
</p>
{% endif %}
{% if diff %}
<div class="snippet-diff">
<h2>{% trans "Comparision with previous snippet " %}</h2>
<div class="snippet-code">{{ diff|safe }}</div>
</div>
{% endif %}
{% if snippet.lexer == '_text' %}
<div class="snippet-text">{% include "dpaste/highlight/text.html" %}</div>
{% else %}

View file

@ -4,8 +4,6 @@
{% block title %}{% trans "Snippet History" %}{% endblock %}
{% block body_type %}data-code-snippet{% endblock %}
{% block options %}
<ul id="snippetOptions">
<li>{% trans "Snippet History" %}</li>
@ -41,7 +39,6 @@
<a href="{{ snippet.get_absolute_url }}">{% blocktrans with snippet.lexer_name as type %}{{ type }} Snippet{% endblocktrans %}</a>
<span class="sep"></span>
{% blocktrans with snippet.published|timesince as since %}Created {{ since }} ago{% endblocktrans %}
</h3>
{% if snippet.lexer == '_text' %}
@ -54,7 +51,4 @@
<p class="history-empty">
{% trans "No snippets saved. Either all your snippets are expired or your cookie has changed." %}</p>
{% endfor %}
{% endblock %}

View file

@ -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', '&nbsp;&nbsp;&nbsp;&nbsp;')
# 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
# -----------------------------------------------------------------------------