45c5304d648a4e4f0ef753bbf23c76b6cffc2bc5
Christian Fraß moved to flat files

Christian Fraß authored 7 years ago

1) import json as _json
2) import os as _os
3) import re as _re
4) 
5) 
6) def dokuwiki_to(data):
7) 	content = ""
8) 	content += ("  * type: %s\n" % (data["type"]))
9) 	content += ("  * description: %s\n" % ("?" if (data["description"] is None) else data["description"]))
10) 	content += ("  * tags\n")
11) 	for tag in data["tags"]:
12) 		content += ("    * %s\n" % (tag))
13) 	content += ("  * translations\n")
14) 	for (language , lemmata) in data["translations"].items():
15) 		content += ("    * %s\n" % (language))
16) 		for lemma in lemmata:
17) 			content += ("      * %s\n" % (lemma))
18) 	return content
19) 	
20) 
21) def dokuwiki_from(content):
22) 	data = {}
23) 	state = "type"
24) 	lines = content.split("\n")
25) 	for line in lines:
26) 		if (line != ""):
27) 			if (state == "type"):
28) 				result = _re.match("^  \\* type: (.*)$", line)
29) 				if (result is not None):
30) 					data["type"] = result.group(1)
31) 					state = "description"
32) 				else:
33) 					raise ValueError("type expected in line '%s' while being in state '%s'" % (line, state))
34) 			elif (state == "description"):
35) 				result = _re.match("^  \\* description: (.*)$", line)
36) 				if (result is not None):
37) 					data["description"] = (None if (result.group(1) == "?") else result.group(1))
38) 					state = "tags"
39) 				else:
40) 					raise ValueError("description expected in line '%s' while being in state '%s'" % (line, state))
41) 			elif (state == "tags"):
42) 				result = _re.match("^  \\* tags$", line)
43) 				if (result is not None):
44) 					data["tags"] = []
45) 					state = "tagentry"
46) 				else:
47) 					raise ValueError("tags expected in line '%s' while being in state '%s'" % (line, state))
48) 			elif (state == "tagentry"):
49) 				result = _re.match("^    \\* (.*)$", line)
50) 				if (result is not None):
51) 					data["tags"].append(result.group(1))
52) 					state = "tagentry"
53) 				else:
54) 					result = _re.match("^  \\* translations$", line)
55) 					if (result is not None):
56) 						data["translations"] = {}
57) 						state = "language"
58) 					else:
59) 						raise ValueError("tag or translations expected in line '%s' while being in state '%s'" % (line, state))
60) 			elif (state == "language"):
61) 				result = _re.match("^    \\* (.*)$", line)
62) 				if (result is not None):
63) 					language = result.group(1)
64) 					data["translations"][language] = []
65) 					state = "lemma"
66) 				else:
67) 					raise ValueError("language expected in line '%s' while being in state '%s'" % (line, state))
68) 			elif (state == "lemma"):
69) 				result = _re.match("^      \\* (.*)$", line)
70) 				if (result is not None):
71) 					lemma = result.group(1)
72) 					data["translations"][language].append(lemma)
73) 					state = "lemma"
74) 				else:
75) 					result = _re.match("^    \\* (.*)$", line)
76) 					if (result is not None):
77) 						language = result.group(1)
78) 						data["translations"][language] = []
79) 						state = "lemma"
80) 					else:
81) 						raise ValueError("language or lemma expected in line '%s' while being in state '%s'" % (line, state))
82) 			else:
83) 				raise ValueError("unhandled state '%s'" % (state))
84) 	return data
85) 	
86) 
87) def json_to(data):
Christian Fraß update_2017-05-02

Christian Fraß authored 7 years ago

88) 	content = _json.dumps(data, indent = "\t", ensure_ascii = False, sort_keys = True)