[Trac] Wiki to HTML

Gerald Kaszuba gak at gakman.com
Sat Apr 16 08:21:43 EDT 2005


Hi

I couldn't find a good Wiki to HTML converter so I made my own in 
Python. You will have to change the script to suit your needs...

Example Usage:
trac-admin /trac/gravit/ wiki export WikiStart | ./wiki2html.py > 
WikiStart.php

Gerald
-------------- next part --------------
#!/usr/bin/python

# wiki2html.py
# Gerald Kaszuba
# Converts Trac Wiki pages to HTML code

#
# Example Usage:
# trac-admin /trac/gravit/ wiki export WikiStart | ./wiki2html.py > WikiStart.php
#

from trac.WikiFormatter import wiki_to_html
from trac import Environment
from trac.Wiki import populate_page_dict
import sys

# Below is a pretend version of the Trac Href class to return a useful href link:
#
# Normally it would do this:
#   <a href="WikiStart">
#
# With a bit of python it can do:
#   <a href="/WikiStart.php">
#
# and for wiki anchor links (needed for AnchorPatch)
#   <a href="/WikiStart.php#moo">
#

class MyHref:
	def __init__(self, base):
		self.base = base

	def wiki(self, page=None, version=None, diff=0, history=0):
		bits = page.split("#", 1)
		if len(bits) == 2:
			return "/" + bits[0] + ".php#" + bits[1]
		else:
			return "/" + bits[0] + ".php"

env = Environment.Environment("/trac/gravit")
env.href = MyHref("")

database = env.get_db_cnx()
populate_page_dict(database, env)

out = wiki_to_html(sys.stdin.read(), "", env, "")

print out


More information about the Trac mailing list