On 11/29/07, Tane Piper <[EMAIL PROTECTED]> wrote:
>
>
> Hi Folks,
>
> I have only been using Python since the weekend, and Django since
> Tuesday so I'm quite new to the API, and I've hit a snag with what I'm
> trying to do.  I'm currently working on porting over my CakePHP
> application which used the GeSHi library for syntax highlighting and I
> came across Pygments as the Python alternative.  What I am trying to
> do is take a paste from the database which is stored as plain text and
> display it highlighted.
>
> So far, here is my view:
>
> from django.shortcuts import render_to_response, get_object_or_404
> from django.http import HttpResponseRedirect
> from django.core.urlresolvers import reverse
> from pastemonkey2.pastes.models import Paste, Language
>
> from pygments import highlight
> from pygments.lexers import get_lexer_by_name
> from pygments.formatters import HtmlFormatter
>
> def highlight(source, language):
>     lexer = get_lexer_by_name(language, stripall=True)
>     formatter = HtmlFormatter(linenos=True, cssclass="source")
>     result = (source, lexer, formatter)
>     return result
>
> def show_paste(request, object_id):
>     p = get_object_or_404(Paste, pk=object_id)
>     p.formatted = highlight(str(p.paste), str(p.language))
>     return render_to_response('pastes/paste_detail.html', {'object': p})


You are first importing the pygments highlight function, then defining your
own, then you never actually call the pygments highlight.  I believe what
you want is something more like:

def format(source, language):
    lexer = get_lexer_by_name(language, stripall=True)
    formatter = HtmlFormatter(linenos=True, cssclass="source")
    result = highlight(source, lexer, formatter)
    return result

def show_paste(request, object_id):
    p = get_object_or_404(Paste, pk=object_id)
    p.formatted = format(str(p.paste), str(p.language))
    return render_to_response('pastes/paste_detail.html', {'object': p})

That is, call your function something other than highlight, and within it
return the result of calling the pygments highlight function.

Karen

What I am trying to do in show_paste is when the paste is received
> from the database, I pass the paste and language from the model into
> the highlight function, which should generate the marked up code and
> pass back, creating a new p.formatted object, which I display in my
> template.  However, what I get back is this:
>
> ("from django.conf.urls.defaults import *\r\nfrom
> pastemonkey2.pastes.models import Paste, Language\r\nfrom
> pastemonkey2.pastes.views import *\r\n\r\ninfo_dict = {\r\n
> 'queryset': Paste.objects.all(),\r\n}\r\n\r\nurlpatterns =
> patterns('',\r\n (r'^$',
> 'django.views.generic.list_detail.object_list', info_dict),\r\n
> (r'^(?P<object_id>\\d+)/$',
> 'pastemonkey2.pastes.views.show_paste'),\r\n)",
> <pygments.lexers.PythonLexer with {'stripall': True}>,
> <pygments.formatters.html.HtmlFormatter object at 0x88ca1ec>)
>
> Instead of nice marked up code.  I'm wondering if anyone can suggest
> where I am going wrong?
>
> Thanks
>
>
> Tane Piper
> Blog - http://digitalspaghetti.me.uk
> AJAX Pastebin - http://pastemonkey.org
>
> This email is: [ ] blogable [ x ] ask first [ ] private
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to