Login

Template filter implementing the Trac wiki markup language

Author:
simon
Posted:
September 11, 2008
Language:
Python
Version:
1.0
Tags:
filter templatetag markup trac
Score:
1 (after 1 ratings)

I don't know how robust or secure this is, but it's working for me so far.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
Usage:

{% load tracwiki %}

{{ object.body|tracwiki }}

# Logic from http://groups.google.com/group/trac-dev/msg/479decac43883dc0
"""

from trac.test import EnvironmentStub, Mock, MockPerm 
from trac.mimeview import Context 
from trac.wiki.formatter import HtmlFormatter 
from trac.web.href import Href

from django.utils.safestring import mark_safe
from django import template
register = template.Library()

env = EnvironmentStub() 
req = Mock(href=Href('/'), abs_href=Href('http://www.example.com/'), 
           authname='anonymous', perm=MockPerm(), args={})
context = Context.from_request(req, 'wiki')

@register.filter
def tracwiki(s):
    return mark_safe(HtmlFormatter(env, context, s).generate())

More like this

Comments

tutuca (on April 23, 2009):
<p>It fails to use the [Image()] macro. But for works flawlessy rendering text</p>

#

albertorcf (on October 21, 2010):
<p>failed to render a wiki link like</p> <p>[wiki:DoSomeThing do some thing]</p> <p>change the next line to correct this</p> <p>context = Context.from_request(req, id='wiki')</p>

#

albertorcf (on October 21, 2010):
<p>or simply remove the parameter:</p> <p>context = Context.from_request(req)</p> <p>tested at Trac version 0.12.1</p> <p>I will see more about the resource parameter to find how improve these wiki links</p>

#

Please login first before commenting.