1import string
2def main(text_in):
3	text_size = len(text_in)
4	pos = 0 #start at the beginning of the text
5	text_in += '\n    ' #bit of padding for the end.
6	mode = 'normal'
7
8	print (' == START OF TRANSLATION == \n\n')
9
10	if text_in[pos:pos+5] == '#summ':
11		while text_in[pos] != '\n':
12			pos +=1
13
14	while pos < text_size:
15
16		if text_in[pos:pos+2] == '[[': #links
17			pos += 2
18			start = pos
19			while text_in[pos:pos+2] != ']]':
20				pos +=1
21			parts = (text_in[start:pos].split('|'))
22			if len(parts) == 1:
23				print('[[' + parts[0] + ']]', end='')
24			else:
25				print('[' + parts[0] + '](' + parts[1] + ')', end='')
26			pos += 2
27
28		elif text_in[pos:pos+5] == '\n{{{\n': #code blocks
29			pos += 4
30			while text_in[pos:pos+4] != '\n}}}':
31				if text_in[pos] == '\n':
32					pos +=1
33					print('\n    ', end='')
34				print(text_in[pos], end='')
35				pos += 1
36			pos += 4
37
38		elif text_in[pos:pos+3] == '{{{': #code blocks
39			pos += 3
40			print ('`', end='')
41			while text_in[pos:pos+3] != '}}}':
42				print(text_in[pos], end='')
43				pos += 1
44			pos += 3
45			print ('`', end='')
46
47		elif text_in[pos:pos+2] == '{{': #images
48			pos += 2
49			start = pos
50			while text_in[pos:pos+2] != '}}':
51				pos +=1
52			parts = (text_in[start:pos].split('|'))
53			if len(parts) == 1:
54				print('![](' + parts[0] + ')', end='')
55			else:
56				print('![' + parts[1] + '](' + parts[0] + ')', end='')
57			pos += 2
58
59		elif text_in[pos:pos+2] == '//': #italics
60			pos += 2
61			print ('_', end='')
62
63		elif mode == 'normal' and text_in[pos:pos+2] == '\n|': #start table
64			pos += 2
65			mode = 'table header'
66			row = 0
67			print ('\n<table>\n  <tr>\n    <th>', end='')
68
69		elif mode == 'table header' and (text_in[pos:pos+2] == '|\n' or text_in[pos] == '\n'): #newline from table header row
70			pos += 3
71			mode = 'table'
72			print('</th>\n  </tr>\n  <tr>\n    <td>', end='')
73
74		elif mode == 'table' and (text_in[pos:pos+2] == '|\n' or text_in[pos] == '\n'): #newline from table row
75			pos += 2
76			if text_in[pos] != '|':
77				print('</td>\n  </tr>\n</table>\n', end='')
78			else:
79				pos += 1
80				print('</td>\n  </tr>\n  <tr>\n    <td>', end='')
81			mode = 'normal'
82
83		elif mode == 'table header' and text_in[pos] == '|': #next table header column
84			pos += 1
85			print('</th><th>', end='')
86
87		elif mode == 'table' and text_in[pos] == '|': #next table column
88			pos += 1
89			print('</td><td>', end='')
90
91		elif text_in[pos:pos+2] == '\n=' or text_in[pos:pos+3] == '\n =': #header detection -- a little shoddy, only checks leading =.
92			pos += 1
93			headcount = 0
94			while text_in[pos] == '=' or text_in[pos] == ' ':
95				if text_in[pos] == '=':
96					headcount += 1
97				pos +=1
98			headcounter = headcount
99			while headcounter > 0:
100				print('#', end='')
101				headcounter -=1
102			print(end=' ')
103			while text_in[pos] != '=' and text_in[pos] != '\n':
104				print(text_in[pos], end='')
105				pos += 1
106			pos += headcount
107
108		else: #must be just text, print it normally
109			print(text_in[pos], end='')
110			pos += 1
111
112	print('\n')
113
114if __name__ == "__main__":
115	main("""
116<< PASTE WIKI PAGE HERE >>
117""")