def snippet_diff(request, template_name='dpaste/snippet_diff.html'): try: fileA = Snippet.objects.get(pk=int(request.GET.get('a'))) fileB = Snippet.objects.get(pk=int(request.GET.get('b'))) except ObjectDoesNotExist: return HttpResponseBadRequest(u'Selected file(s) does not exist.') if fileA.content != fileB.content: d = difflib.unified_diff(fileA.content.splitlines(), fileB.content.splitlines(), 'Original', 'Current', lineterm='') difftext = '\n'.join(d) difftext = pygmentize(difftext, 'diff') else: difftext = _(u'No changes were made between this two files.') template_context = { 'difftext': difftext, 'fileA': fileA, 'fileB': fileB, } return render_to_response(template_name, template_context, RequestContext(request))
def snippet_diff(request, template_name='dpaste/snippet_diff.html'): if request.GET.get('a').isdigit() and request.GET.get('b').isdigit(): try: fileA = Snippet.objects.get(pk=int(request.GET.get('a'))) 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.') if fileA.content != fileB.content: d = difflib.unified_diff( fileA.content.splitlines(), fileB.content.splitlines(), 'Original', 'Current', lineterm='' ) difftext = '\n'.join(d) difftext = pygmentize(difftext, 'diff') else: difftext = _(u'No changes were made between this two files.') template_context = { 'difftext': difftext, 'fileA': fileA, 'fileB': fileB, } return render_to_response( template_name, template_context, RequestContext(request) )
def highlight(snippet, maxlines=None): h = pygmentize(snippet.content, snippet.lexer) if not h: s = snippet.content.splitlines() s = h.splitlines() if maxlines: return s[:maxlines] return s
def snippet_diff(request, template_name='dpaste/snippet_diff.html'): a, b = request.GET.get('a'), request.GET.get('b') if (a and b) and (a.isdigit() and b.isdigit()): group, bridge = group_and_bridge(request) # Make queryset suitable for getting the right snippet if group: snippets = group.content_objects(Snippet) else: snippets = Snippet.objects.filter(group_object_id=None) try: fileA = snippets.get(pk=int(a)) fileB = snippets.get(pk=int(b)) except ObjectDoesNotExist: return HttpResponseBadRequest(u'Selected file(s) does not exist.') else: return HttpResponseBadRequest(u'You must select two snippets.') if fileA.content != fileB.content: d = difflib.unified_diff( fileA.content.splitlines(), fileB.content.splitlines(), 'Original', 'Current', lineterm='' ) difftext = '\n'.join(d) difftext = pygmentize(difftext, 'diff') else: difftext = _(u'No changes were made between this two files.') template_context = group_context(group, bridge) template_context.update({ 'difftext': difftext, 'fileA': fileA, 'fileB': fileB, }) return render_to_response( template_name, template_context, RequestContext(request) )
def save(self, *args, **kwargs): if not self.pk: self.published = datetime.datetime.now() self.secret_id = generate_secret_id() self.content_highlighted = pygmentize(self.content, self.lexer) super(Snippet, self).save(*args, **kwargs)
def test_highlighting(self): # You can pass any lexer to the pygmentize function and it will # never fail loudly. from dpaste.highlight import pygmentize pygmentize('code', lexer_name='python') pygmentize('code', lexer_name='doesnotexist')
def highlight(snippet): h = pygmentize(snippet.content, snippet.lexer) h = h.replace(u' ', u' ') h = h.replace(u'\t', ' ') return h.splitlines()
def highlight(snippet): h = pygmentize(snippet.content, snippet.lexer) h = h.replace(u' ', u' ') h = h.replace(u'\t', ' ') return h.splitlines()
def highlight(snippet): h = pygmentize(snippet.content, snippet.lexer) return h.splitlines()