Skip to content

Instantly share code, notes, and snippets.

@infomaniac50
Forked from sgk/trac2down.py
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save infomaniac50/1b02a179ab27af160b0f to your computer and use it in GitHub Desktop.

Select an option

Save infomaniac50/1b02a179ab27af160b0f to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# vim:set fileencoding=utf-8 sw=2 ai:
import datetime
import re
import sys
def trac2md():
text = sys.stdin.read()
text = re.sub('\r\n', '\n', text)
text = re.sub(r'{{{(.*?)}}}', r'`\1`', text)
def indent4(m):
return '\n ' + m.group(1).replace('\n', '\n ')
text = re.sub(r'(?sm){{{\n(.*?)\n}}}', indent4, text)
text = re.sub(r'(?m)^====\s+(.*?)\s+====$', r'#### \1', text)
text = re.sub(r'(?m)^===\s+(.*?)\s+===$', r'### \1', text)
text = re.sub(r'(?m)^==\s+(.*?)\s+==$', r'## \1', text)
text = re.sub(r'(?m)^=\s+(.*?)\s+=$', r'# \1', text)
text = re.sub(r'^ * ', r'****', text)
text = re.sub(r'^ * ', r'***', text)
text = re.sub(r'^ * ', r'**', text)
text = re.sub(r'^ * ', r'*', text)
text = re.sub(r'^ \d+. ', r'1.', text)
a = []
for line in text.split('\n'):
if not line.startswith(' '):
line = re.sub(r'\[(https?://[^\s\[\]]+)\s([^\[\]]+)\]', r'[\2](\1)', line)
line = re.sub(r'\[(wiki:[^\s\[\]]+)\s([^\[\]]+)\]', r'[\2](/\1/)', line)
line = re.sub(r'\!(([A-Z][a-z0-9]+){2,})', r'\1', line)
line = re.sub(r'\'\'\'(.*?)\'\'\'', r'*\1*', line)
line = re.sub(r'\'\'(.*?)\'\'', r'_\1_', line)
a.append(line)
text = '\n'.join(a)
sys.stdout.write(text)
# fp = file('%s.md' % name, 'w')
# print >>fp, '<!-- Name: %s -->' % name
# print >>fp, '<!-- Version: %d -->' % version
# print >>fp, '<!-- Last-Modified: %s -->' % datetime.datetime.fromtimestamp(time).strftime('%Y/%m/%d %H:%M:%S')
# print >>fp, '<!-- Author: %s -->' % author
# fp.write(text.encode('utf-8'))
# fp.close()
trac2md()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment