Last active
March 4, 2016 05:55
-
-
Save gazpachoking/36a7fa0f491666713e4d to your computer and use it in GitHub Desktop.
This was used to convent the FlexGet trac wiki to markdown suitable for the realms wiki engine. Git repo was prepared with https://github.com/hinnerk/Trac2Gollum but with the gollum markdown conversion stripped out and pathseps left in. Based on https://gist.github.com/gazpachoking/9540849 which was based on https://gist.github.com/sgk/1286682
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
"""Usage: trac2down <trac wiki dump path>""" | |
from __future__ import unicode_literals | |
import codecs | |
import os | |
import re | |
import sys | |
import path | |
def convert_wiki_link(link): | |
if link.startswith('wiki:'): | |
link = link[5:] | |
return link.strip("'") | |
def sub_full_wiki_link(m): | |
return '[%s](%s)' % (m.group(2), convert_wiki_link(m.group(1))) | |
def sub_simple_wiki_link(m): | |
l = convert_wiki_link(m.group(1)) | |
return '[%s](%s)' % (l, l) | |
def sub_fenced_block(m): | |
if m.group(1) == 'html': | |
return '\n%s\n' % m.group(2) | |
elif m.group(1): | |
return '```%s\n%s\n```' % (m.group(1), m.group(2)) | |
return '```\n%s\n```' % m.group(2) | |
def sub_table(m): | |
lines = [] | |
for group in m.group(0).strip().split('\n'): | |
lines.append(' | '.join(group.strip().split('||')).strip()) | |
width = len(m.group(1).strip().split('||')) - 2 | |
lines.insert(1, '| %s |' % ' | '.join('---' for x in range(width))) | |
return '\n%s\n' % '\n'.join(lines) | |
def trac2down(text): | |
text = re.sub('\r\n', '\n', text) | |
text = re.sub(r'{{{(.*?)}}}', r'`\1`', text) | |
text = re.sub(r'(?sm){{{\n(?:#!([a-z]+)\n)?(.*?)\n}}}', sub_fenced_block, text) | |
text = re.sub(r'(?m)^(\|\|[^\n]+\|\|\n)+$', sub_table, text) | |
text = re.sub(r'(?m)^====\s+(.*?)\s+====\s?$', r'#### \1', text) | |
text = re.sub(r'(?m)^===\s+(.*?)\s+===\s?$', r'### \1', text) | |
text = re.sub(r'(?m)^==\s+(.*?)\s+==\s?$', r'## \1', text) | |
text = re.sub(r'(?m)^=\s+(.*?)\s+=\s?$', r'# \1', text) | |
text = re.sub(r'^ ([ ]*\*)', r'\1', text) | |
text = re.sub(r'^ \d+.', r'1.', text) | |
text = re.sub(r'(?m)\[\[BR\]\]$', ' ', text) | |
a = [] | |
for line in text.split('\n'): | |
if not line.startswith(' '): | |
line = re.sub(r'(?<!\[)\[([^\s\[\]]+?)\]', sub_simple_wiki_link, line) | |
line = re.sub(r'\[([a-z]+?://[^\s\[\]]+)\s([^\[\]]+)\]', r'[\2](\1)', line) | |
line = re.sub(r'\[(wiki:?[^\s\[\]]+)\s([^\[\]]+)\]', sub_full_wiki_link, 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) | |
return '\n'.join(a) | |
if __name__ == '__main__': | |
wiki_dump = sys.argv[1] | |
for oldfile in path.Path(wiki_dump).walkfiles(): | |
text = trac2down(oldfile.text()) | |
with oldfile.open('w') as fp: | |
fp.write(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment