Hi,

I am trying to get pygments and markdown highlighting working on my blog.

When I try and save a post that has ` in it I get an attribute error.

My models are (more beautifully here http://dpaste.com/17230/) ignore
the _highlight_python_code part. that was from a previous attempt:

from django.db import models
import datetime

# Create your models here.

class Tag(models.Model):
        slug = models.SlugField(
                        prepopulate_from=("name",),
                        help_text='Automatically prepopulated from name',
                        )
        name = models.CharField(maxlength=30)
        description = models.TextField(
                        help_text='Short summary of this tag'
                        )
        def __str__(self):
                return self.name

        def get_absolute_url(self):
                return "/blog/tag/%s/" % self.slug

        class Admin:
                list_display = ('name', 'slug', )
                search_fields = ('name', 'description',)

class Entry(models.Model):
        title = models.CharField(maxlength=255, core=True,
unique_for_date="pub_date")
        pub_date = models.DateTimeField(core=True)
        slug = models.SlugField(maxlength=30, prepopulate_from= ['title'])
        body = models.TextField(core=True, help_text='Use <a
href="http://daringfireball.net/projects/markdown/syntax";>Markdown-syntax</a>')
        body_html = models.TextField(blank=True, null=True)
        use_markdown = models.BooleanField(default=True)
        tags = models.ManyToManyField(Tag,
filter_interface=models.HORIZONTAL)

        class Admin:
                fields = (
                        (None, {'fields': ('slug', 'title', 'tags',
'use_markdown', 'pub_date', 'body', 'body_html',)}),
                )

        def __str__(self):
                return self.title

        def get_absolute_url(self):
                return "/blog/%s/%s/" %
(self.pub_date.strftime("%Y/%m/%d").lower(), self.slug)

        #from
http://www.unessa.net/en/hoyci/2006/11/highlighting-code-using-pygments-and-beautiful-soup/

        def _highlight_python_code(self):
                from pygments import highlight
                from pygments.lexers import PythonLexer
                from pygments.formatters import HtmlFormatter
                from BeautifulSoup import BeautifulSoup

                soup = BeautifulSoup(self.body)
                python_code = soup.findAll("code")

                if self.use_markdown:
                        import markdown

                        index = 0
                        for code in python_code:
                                code.replaceWith('<p
class="python_mark">mark %i</p>' % index)
                                index = index+1

                        markdowned = markdown.markdown(str(soup))
                        soup = BeautifulSoup(markdowned)
                        markdowned_code = soup.findAll("p", "python_mark")

                        index = 0
                        for code in markdowned_code:

code.repalceWith(highlight(python_code[index].renderContents(),
PythonLexer(), HtmlFormatter()))
                                index = index+1
                else:
                        for code in python_code:
                                code.replaceWith(highlight(code.string,
PythonLexer(), HtmlFormatter()))

                return str(soup)


        #from dpaste.com/16735
        def markup(self, value):
                import re
                from BeautifulSoup import BeautifulSoup
                from pygments import highlight
                from pygments.formatters import HtmlFormatter
                from pygments.lexers import guess_lexer, get_lexer_by_name
                from markdown import markdown

                value = markdown(value)
                tree = BeautifulSoup(value)
                try:
                        for code in tree.findAll("code"):
                                print code.contents[0].__dict__
                                lexer = get_lexer_by_name('python',
stripall=True)
                                formatter = HtmlFormatter
                                new_content =
highlight(code.contents[0], lexer, formatter)
                                code.replaceWith(new_content)
                        value = str(tree)
                except IndexError:
                        pass
                return value



        def save(self):
                self.body_html = self.markup(self.body)
                super(Entry,self).save()


I was trying to save a post with the following content:

`print "hello world"`

I am using a PostgreSQL database.

Thanks,
Evan

--~--~---------~--~----~------------~-------~--~----~
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