#!/usr/bin/env python

# REMEMBER TO BACK UP YOUR FILES FIRST.
# REMEMBER TO BACK UP YOUR FILES FIRST.
#
# Recursively walks the directory and converts UTF-8 or ASCII .txt
# files to Markdown. If you write in other character sets, modify the
# CHARSET line. If you want other file extensions, modify the
# EXTENSION line.
#
# You'll need markdown2.py to be in the same directory.
# [http://python-markdown2.googlecode.com/svn/trunk/lib/markdown2.py]
#
# Usage, from Terminal.app:
#
# > cd /my/directory/of/text/files
# > ./recursive-markdown.py
#
# Output: Something like
#
# ./a.txt: done
# ./a/a.txt: done
# ./a/b.txt: done
# ./a/a/a.txt: done
#
# REMEMBER TO BACK UP YOUR FILES FIRST.

CHARSET = 'utf8'
EXTENSION = '.txt'

import markdown2 as m # You'll need markdown2.py
import os

def markdown(input, output):
markdowned = m.markdown(input.read().decode(CHARSET))
output.write(markdowned.encode(CHARSET))
input.close()
output.close()
return markdowned

for root, dirs, files in os.walk('.'):
for f in files:
if f.endswith(EXTENSION):
path = os.path.join(root, f)
input = open(path, "rb")
output = open(path, "wb")
markdown(input, output)
print("%s: done" % path)

__author__ = "shadytrees"