Skip to content

Instantly share code, notes, and snippets.

@ktf
Created July 21, 2010 11:58
Show Gist options
  • Save ktf/484386 to your computer and use it in GitHub Desktop.
Save ktf/484386 to your computer and use it in GitHub Desktop.
Convert a folder of markdown text to an ikiwiki hierarchy
#!/usr/bin/python
from glob import glob
from optparse import OptionParser
from os.path import join, basename
from email import message_from_file
from os import getenv
from os.path import splitext
import re
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-o", "--output", dest="output", default=join(getenv("HOME"), "Dropbox/homepage"),
help="output directory")
opts, args = parser.parse_args()
if not args:
args = [join(getenv("HOME"), "Dropbox/notes")]
files = glob(join(args[0], "*.mdwn")) + glob(join(args[0], "*.txt"))
for f in files:
m = message_from_file(open(f))
f = splitext(basename(f))[0]
filename = basename(re.sub("[\t ]", "-", f))
filename = re.sub("[().]", "", filename) + ".mdwn"
filename = filename.lower()
title = "[[!meta title=\"%s\"]]" % f
tags = []
project = None
permalink = None
if "Tags" in m:
tags = ["%s" % x.strip() for x in m['Tags'].split(",")]
if not "public" in tags:
continue
else:
tags.remove("public")
if tags:
tagsString = "[[!tag %s]]" % " ".join(tags)
# Backward compatibility perma-link
if "Permalink" in m:
permalink = m['Permalink'].strip()
if "Project" in m:
project = m['Project'].strip()
if project:
filename = join(opts.output, project, filename)
elif permalink:
filename = join(opts.output, permalink + ".mdwn")
else:
filename = join(opts.output, filename)
print filename
fp = file(filename, "w")
fp.write(title + "\n")
if tags:
fp.write(tagsString + "\n")
fp.write(m.get_payload())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment