Hi,

I am trying setup pygments and markdown described here:
http://www.unessa.net/en/hoyci/2006/11/highlighting-code-using-pygments-and-beautiful-soup/

My Model.py is:
(Also here http://dpaste.com/hold/16731/)
-----
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)

        def save(self):
                import markdown
                self.body_html = self._highlight_python_code()
                super(Entry,self).save()

-----

If I create an entry with use_markdown True, and body:
this is a regular paragraph
`print "hello world"`

the body_html is:

<p>this is a regular paragraph
   <code>print &quot;hello world&quot;</code>
</p>


If I create an entry with use_markdown False, and body:
this is a regular paragraph
<code>print "hello world"</code>

the body_html is:

this is a regular paragraph
<div class="highlight"><pre><span class="k">print</span> <span
class="s">&quot;hello world&quot;</span>
</pre></div>

I want markdown to find my code blocks or code spans and then let them
be colored by pygments. If I turn use_markdown to False, pygments works.
If it is True, pygments doesn't work.

Maybe there is a problem with the replaceWith() markdowned_code and it
isn't getting changed in soup, which is then returned.



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