hi all,

using a little bit of code snippets from pygments demo app views and of
course using pygments with django for syntax highlighting. im running
into an issue where no matter what style ive selected i can't get it to
highlight properly.. to get around this ive manually copied a
stylesheet from pygments.pocoo.org and using one style at the moment.

if anyone has any experience with this i'd appreciate the help, here is
my templates/views

{% extends "base.html" %}
{% block title %}
{{ code.id }}
{% endblock %}
{% block content %}
{% load sitevars %}
<h1>Entry {{ code.id }}</h1>
<div class="controls">
<h3><a href="/toolbox/cpaste/">Paste some code!</a></h3>
<form action="{% site_base %}cpaste/detail/{{ code.id }}/" method="get"
name="filter_form" id="filter_form">
Style
   <select id="style" name="style"
onchange="document.forms.filter_form.submit()">
   {% for style in styles %}
       <option value="{{ style }}" {% ifequal style curstyle
%}selected="select"{% endifequal %}>{{ style }}</option>
   {% endfor %}
   </select>
   <input type="submit" value="Go">
</form>
</div>
   <div class="hlcode">
       {{ code.hlcode }}
   </div>

{% endblock %}


and my view
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib import auth
from django.contrib.auth.models import User
from djangoprojects.cpaste.models import CPaste

from pygments import highlight
from pygments.lexers import get_lexer_by_name,LEXERS
from pygments.formatters import HtmlFormatter
from pygments.styles import STYLE_MAP

from datetime import datetime

#--------
# globals
HTML_STYLE='trac'
fmter =
HtmlFormatter(style=HTML_STYLE,cssclass='syntax',encoding='utf-8')

lexers = [(x[1], x[2]) for x in LEXERS.values()]
lexers.sort()

lx_name = dict((lx[1][0],lx[0]) for lx in lexers)
lx_name[''] = ''

def detail(request,code_id):
   if not request.user.is_authenticated():
       user = ''
   else:
       user = request.user
       uid = User.objects.get(username=request.user)
   try:
       code = CPaste.objects.get(pk=code_id)
       _do_hl(code)
       code.save()
   except CPaste.DoesNotExist:
       return HttpResponseRedirect("/toolbox/cpaste/")
   style = request.GET.get('style')
   if style and style in STYLE_MAP:
       request.session['style'] = style
   else:
       style = request.session.get('style', 'friendly')

   return render_to_response('cpaste/detail.html',
dict(code=code,styles=STYLE_MAP.keys(),curstyle=style,user=user))

def _do_hl(code):
   lexer=get_lexer_by_name(code.sourcelang)
   code.hlcode =
highlight(code.sourcecode,lexer,fmter).decode('utf-8')

so as far as saving the content based on style that seems to work, but
i can't figure out how to push the selected style back into the
template? from what i noticed on pygments.pocoo.org is that it writes
to a /media/pygments_style.css each time a different style is
requested.

thx
adam


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com
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