Пример #1
0
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))
Пример #2
0
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)
    )
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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)
    )
Пример #6
0
 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)
Пример #7
0
 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')
Пример #8
0
def highlight(snippet):
    h = pygmentize(snippet.content, snippet.lexer)
    h = h.replace(u'  ', u'  ')
    h = h.replace(u'\t', '    ')
    return h.splitlines()
Пример #9
0
 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)
Пример #10
0
 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')
Пример #11
0
def highlight(snippet):
    h = pygmentize(snippet.content, snippet.lexer)
    h = h.replace(u'  ', u'  ')
    h = h.replace(u'\t', '    ')
    return h.splitlines()
Пример #12
0
def highlight(snippet):
    h = pygmentize(snippet.content, snippet.lexer)
    return h.splitlines()