Skip to content

Instantly share code, notes, and snippets.

@darkfeline
Created May 12, 2014 05:30
Show Gist options
  • Save darkfeline/0884a2f5a179e2719252 to your computer and use it in GitHub Desktop.
Save darkfeline/0884a2f5a179e2719252 to your computer and use it in GitHub Desktop.
Half-hearted dokuwiki to kramdown converter
import os
import re
p_heading = re.compile(r'(=+) (.*?) =+')
p_code = re.compile(r'</?code>')
p_link = re.compile(r'\[\[([^|\]]+)\|([^\]]+)\]\]')
p_inlink = re.compile(r'\[\[([^|\]]+)\]\]')
files = [x for x in os.listdir() if x.endswith('.txt')]
for x in files:
print("Doing {}".format(x))
text = []
with open(x) as f:
for line in f: # includes newline
# headings
match = p_heading.match(line)
if match:
level = -(len(match.group(1)) - 7)
line = '{} {}\n'.format('#' * level, match.group(2))
# code
match = p_code.match(line)
if match:
line = '~~~\n'
text.append(line)
text = ''.join(text)
text = p_link.sub(r'[\2](\1)', text)
text = p_inlink.sub(r'[\1](\1)', text)
with open(x[:-4] + '.md', 'w') as g:
g.write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment