Producing HTML from Trac from python

113 views
Skip to first unread message

Jani Tiainen

unread,
Jun 19, 2008, 5:02:15 PM6/19/08
to trac...@googlegroups.com
How I can make HTML from Trac wiki markup?

Preferably without opening any environments, databases and such.

--

Jani Tiainen

Noah Kantrowitz

unread,
Jun 19, 2008, 5:07:13 PM6/19/08
to trac...@googlegroups.com
> -----Original Message-----
> From: trac...@googlegroups.com [mailto:trac...@googlegroups.com] On
> Behalf Of Jani Tiainen
> Sent: Thursday, June 19, 2008 2:02 PM
> To: trac...@googlegroups.com
> Subject: [Trac-dev] Producing HTML from Trac from python
>
>
> How I can make HTML from Trac wiki markup?
>
> Preferably without opening any environments, databases and such.

Not easily. At the very least you need to make a fake Request object that
the href setup correctly so links can be rendered. Macros may require the
env object, so if you set it to None, some things may break.

--Noah

Erik Bray

unread,
Jun 19, 2008, 5:14:50 PM6/19/08
to trac...@googlegroups.com

It would be nice if this were somehow easier. Not sure how though.
Here's a simple little script I was using for this purpose:

#!/usr/bin/env python
import sys
from trac.test import EnvironmentStub, Mock, MockPerm
from trac.mimeview import Context
from trac.wiki.formatter import HtmlFormatter
from trac.web.href import Href

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


Of course, if you want actual web pages you'll have to add the
appropriate HTML headers and footers. And as Noah said, some wiki
macros may not work with this.

Erik

Jani Tiainen

unread,
Jun 20, 2008, 12:57:04 AM6/20/08
to trac...@googlegroups.com
Erik Bray kirjoitti:

Well I was trying to make wiki preview for offline editor. You could
download wiki page(s)) to your computer and edit them as you wish, take
a preview before pushing changes back to Trac.

It would nice to have simpler way to render at least core markup, and
leave macros unrendered...

--

Jani Tiainen

Noah Kantrowitz

unread,
Jun 20, 2008, 1:18:21 AM6/20/08
to trac...@googlegroups.com

It would be pretty easy to subclass Formatter, and just replace the
method that handles macro rendering with a noop. In that case the code
Erik gave should work fine for most cases. There are also custom syntax
providers to worry about, which you can't as easily disable last I
looked. I would have to stare at the code again to see where those hook in.

--Noah


signature.asc
Reply all
Reply to author
Forward
0 new messages