Ok, the problem was using <code> tags, CKEditor attempts to format them.

Switching to <pre> tags, CKEditor will not format them.... So

def __highlight__(content, dom_element='pre'):
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import HtmlFormatter
    from BeautifulSoup import BeautifulSoup

    soup = BeautifulSoup(content)

    formatter = HtmlFormatter(linenos=True, noclasses=True)

    for tag in soup.findAll(dom_element):
        language = tag.get('lang')
        lexer = get_lexer_by_name(language, encoding='UTF-8')
        tag.replaceWith(highlight(tag.renderContents(), lexer, formatter))
        pass
    return unicode(soup)

-Thadeus




On Sun, Nov 15, 2009 at 2:55 PM, Thadeus Burgess <thade...@thadeusb.com>wrote:

> Thanks that got me on the right track!
>
> This is what I ended up with, a view function that gets called on the blog
> posts content. The only problem is CKEditor tries to format the code during
> editing, so you end up with alot of <br/> leaked in, working on solving
> that.
>
> {{
> def __highlight__(content):
>     from pygments import highlight
>     from pygments.lexers import get_lexer_by_name
>     from pygments.formatters import HtmlFormatter
>     from BeautifulSoup import BeautifulSoup
>
>     soup = BeautifulSoup(content)
>
>     formatter = HtmlFormatter(linenos=True, noclasses=True)
>
>     for tag in soup.findAll('code'):
>         language = tag.get('lang')
>         lexer = get_lexer_by_name(language, encoding='UTF-8')
>         tag.replaceWith(highlight(tag.renderContents(), lexer, formatter))
>         pass
>     return unicode(soup)
> }}
>
> -Thadeus
>
>
>
>
>
> On Sun, Nov 15, 2009 at 1:35 PM, Mengu <whalb...@gmail.com> wrote:
>
>>
>> hi Thadeus,
>>
>> hope it helps:
>> http://github.com/mengu/blog/blob/master/models/post.py#L39
>>
>> On Nov 15, 8:48 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
>> > I have a html content (for a blog) and would like to parse the content
>> for
>> > <code> tags, and of course replace the content with web2py syntax
>> > highlighting using CODE helper.
>> >
>> > Has anybody done this yet?
>> >
>> > -Thadeus
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to