-
-
Save jbs1/ba01208e254ed35b60b34edd69748a90 to your computer and use it in GitHub Desktop.
Trac Wiki to Markdown converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import os | |
| import sqlite3 | |
| import datetime | |
| import re | |
| import codecs | |
| import sys | |
| if (len(sys.argv)!=3): | |
| print("Usage:\n python3 "+sys.argv[0]+" <path to trac.db> <path to output dir>"); | |
| sys.exit(2); | |
| SQL = ''' | |
| select | |
| name, time, author, text | |
| from | |
| wiki w2 | |
| where | |
| version = (select max(version) from wiki where name = w2.name); | |
| ''' | |
| conn = sqlite3.connect(sys.argv[1]) | |
| result = conn.execute(SQL) | |
| path = None | |
| for row in result: | |
| name = row[0] | |
| time = row[1] | |
| author = row[2] | |
| text = row[3] | |
| if(len(name.split('/'))>1): | |
| path=name.split('/') | |
| name=path.pop() | |
| path='/'.join(path) | |
| text = re.sub('\r\n', '\n', text) | |
| text = re.sub(r'\[\[PageOutline\]\]','',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) | |
| if(path!=None): | |
| os.makedirs(sys.argv[2]+os.sep+path, exist_ok=True) | |
| fp = codecs.open('{}{}{}{}{}.md'.format(sys.argv[2],os.sep,path,os.sep,name), 'w',encoding='utf8') | |
| else: | |
| fp = codecs.open('{}{}{}.md'.format(sys.argv[2],os.sep,name), 'w',encoding='utf8') | |
| print('<!-- Author: %s -->' % author,file=fp) | |
| fp.write(text) | |
| fp.close() | |
| if(path!=None): | |
| path=None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment