1. cherrypy
  2. CherryPy


CherryPy / sphinx / util / convert-trac.py

The default branch has multiple heads


%prog <filename>

A utility script for performing some commonly-encountered patterns in
Trac Wiki format into reStructuredText (rst).

filename is the name of the text file to be saved. If -U is not used,
the file is converted in-place and filename is also the name of the

from __future__ import print_function
import sys
import re
import inspect
import optparse
import shutil
import urllib2
from StringIO import StringIO

def get_options():
	global options
	parser = optparse.OptionParser(usage=inspect.cleandoc(__doc__))
	parser.add_option('-U', '--url', help="Trac URL from which to retrieve source")
	options, args = parser.parse_args()
		options.filename = args.pop()
	except IndexError:
		parser.error("Filename required")

# each of the replacement functions should have a docstring
#  which is a regular expression to be matched.

def replace_external_link(matcher):
	r"\[(?P<href>(?P<scheme>\w+)\://.+?) (?P<name>.+?)\]"
	return '`{name} <{href}>`_'.format(**matcher.groupdict())

def replace_wiki_link(matcher):
	r"\[wiki\:(?P<ref>.+?) (?P<name>.+?)\]"
	return '`{name} <TODO-fix wiki target {ref}>`_'.format(**matcher.groupdict())

# character array indexed by level for characters
heading_characters = [None, '*', '=', '-', '^']

def replace_headings(matcher):
	r"^(?P<level>=+) (?P<name>.*) (?P=level)$"
	level = len(matcher.groupdict()['level'])
	char = heading_characters[level]
	name = matcher.groupdict()['name']
	lines = [name, char*len(name)]
	if level == 1:
		lines.insert(0, char*len(name))
	return '\n'.join(lines)

def indent(block):
	add_indent = lambda s: '    ' + s
	lines = StringIO(block)
	i_lines = map(add_indent, lines)
	return ''.join(i_lines)

def replace_inline_code(matcher):
	return '``{code}``'.format(**matcher.groupdict())

def replace_code_block(matcher):
	return '::\n\n' + indent(matcher.groupdict()['code'])

def replace_page_outline(matcher):
	return ''

def replace_bang_symbols(matcher):
	return matcher.groupdict()['symbol']

# a number of the files end in
<h2 class='compatibility'>Older versions</h2>
}}}""" # and everything after is garbage, so just remove it.
def remove_2x_compat_notes(matcher):
	return ''

replacements = [remove_2x_compat_notes] + \
	[func for name, func in globals().items() if name.startswith('replace_')]

def normalize_linebreaks(text):
	return text.replace('\r\n', '\n')

def convert_file():
	filename = options.filename
	if options.url:
		text = urllib2.urlopen(options.url).read()
		text = normalize_linebreaks(text)
		shutil.copy(filename, filename+'.bak')
		text = open(filename).read()
	# iterate over each of the replacements and execute it
	new_text = text
	for repl in replacements:
		pattern = re.compile(inspect.getdoc(repl), re.MULTILINE)
		new_text = pattern.sub(repl, new_text)

	open(filename, 'w').write(new_text)

def handle_command_line():

if __name__ == '__main__':