]> de.git.xonotic.org Git - xonotic/xonotic.wiki.git/blob - assets/convert-wiki-links.py
added blaster jumping images
[xonotic/xonotic.wiki.git] / assets / convert-wiki-links.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # not a simple sed script because I can do non-greedy easier here :x
5
6 # changing gollum/wiki links to markdown/markup syntax (no redlinks support... fuuu gitlab)
7 # but not the other way around...
8 # since external links and images are better left using portable markdown syntax
9
10 import os
11 import re
12
13 FILES = ('.md',)
14 RX = [
15 #       (re.compile(r''), ''),
16         # I'm sure this could be cleaner... but it works (order is important (with \W for french chars), or [[a|b]] is matched to [a|b](a|b) !)
17         (re.compile(r'(?u)\[\[([\w\W \\/\.#\(\)_-]+?)\|([\w\W \\/\.#\(\)_-]+?)\]\]'), r'[\1](\2)'), # [[This|that#top]] -> [This](that#top)
18         (re.compile(r'(?u)\[\[([\w\W \\/\.#\(\)_-]+?)\]\]'), r'[\1](\1)'), # [[This]] -> [This](This)
19 ]
20
21 path = '.'
22 lsdir = os.listdir(path)
23 for f in lsdir:
24         file_name, file_extension = os.path.splitext(f)
25         new_f = file_name + file_extension + '.rx'
26
27         if file_extension in FILES:
28                 i = os.path.join(path, f)
29                 o = os.path.join(path, new_f)
30                 with open(i, "r") as inf, open(o, "w") as outf:
31                         for line in inf:
32                                 for search, replace in RX:
33                                         #line = search.sub(replace, line)
34                                         line = re.sub(search, replace, line)
35                                 outf.write(line)
36                 os.rename(o, i)