#!/usr/bin/env python
import re
import os.path
import fnmatch
cvs_header = r'$Header: /home/pknaggs/cvs/hints/hints/tools/python/twiki_to_tiddlywiki.py,v 1.15 2006/08/20 19:42:53 pknaggs Exp $'
#
# Change the setting for 'file_list' so that it finds your twiki syntax
# files that you want to convert to the tiddlywiki syntax.
# Run this script with no parameters, it should create new files
# in the same directory (with '.new' as the suffix), containing the
# the twiki contents converted over to the tiddlywiki syntax.
#
#
# License:
# This script makes use of the example on page 144 of the Python Cookbook, by
# Robin Parmar and Alex Martelli, so it is under the same "modified Berkeley
# license" (see p. xix of that book).
#
#
# listFiles:
# Walk a directory tree, and obtain a list of all the files (and optionally
# all the directories) that match a certain pattern.
#
# The standard directory-tree function os.path.walk is powerful and flexible,
# but it can be confusing to beginners. This recipe dresses it up in a listFiles
# function that lets you choose the root folder, whether to recurse down through
# subfolders, the patterns to match, and whether to include folder names in the
# result list.
#
# The file patterns are case-insensitive but otherwise Linux-style, as supplied
# by the standard fnmatch module, which this recipe uses. To specify multiple
# patterns, join them with a semicolon. Note that this means that semicolons
# themselves can't be part of a pattern.
# For example, you can easilu get a list of all Python and HTML files in
# directory /tmp or any subdirectory thereof, using:
# thefiles = listFiles('/tmp', '*.py;*.htm;*.html')
#
def listFiles(root, patterns='*', recurse=True, return_folders=False):
# Expand patterns from semicolon-separated string to list
pattern_list = patterns.split(';')
# Collect input and output arguments into one bunch
class Bunch:
def __init__(self, **kwds): self.__dict__.update(kwds)
arg = Bunch(recurse=recurse,
pattern_list=pattern_list,
return_folders=return_folders,
results=[])
def visit(arg, dirname, files):
# Append to arg.results all relevant files (and perhaps folders).
for name in files:
fullname = os.path.normpath(os.path.join(dirname, name))
if arg.return_folders or os.path.isfile(fullname):
for pattern in arg.pattern_list:
if fnmatch.fnmatch(name, pattern):
arg.results.append(fullname)
break
# Block recursion if recursion was disallowed
if not arg.recurse:
files[:] = []
os.path.walk(root, visit, arg)
return arg.results
# Change this setting so that it finds the files (in twiki syntax) that
# you want to convert to tiddlywiki syntax:
file_list = listFiles('/home/pknaggs/hints/web/penlug/wiki', '*;')
# Search for links like [[http://www.url.org/][description]]
# and switch them to look like this [[description|http://www.url.org/]]
twiki_link_finder = re.compile(r'''
\[\[ # search for '[['
(?P # search for ' '
''', re.VERBOSE)
paragraph_end = re.compile(r'''