Jani Tiainen wrote:
Erik Bray kirjoitti:
On Thu, Jun 19, 2008 at 5:07 PM, Noah Kantrowitz <[EMAIL PROTECTED]> wrote:
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Jani Tiainen
Sent: Thursday, June 19, 2008 2:02 PM
To: [email protected]
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.

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.

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

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


Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to