-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip-2-paste.py
57 lines (47 loc) · 1.35 KB
/
clip-2-paste.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import sys
import xml.etree.ElementTree as ET
if len(sys.argv) != 3:
print('usage: python clip-2-paste.py <input.xml> <output.json>')
exit(1)
tree = ET.parse(sys.argv[1])
root = tree.getroot()
folder_map = {}
folders = {}
for child in root:
node_type = child.attrib.get('type')
if node_type == 'FOLDER':
for folder_child in child:
if folder_child.attrib['name'] == "title":
folder_map[child.attrib['id']] = folder_child.text
for title in folder_map.values():
folders[title] = []
title = ''
content = ''
folder = ''
for child in root:
node_type = child.attrib.get('type')
if node_type == 'SNIPPET':
for folder_child in child:
name = folder_child.attrib['name']
if name == "title":
title = folder_child.text
elif name == "content":
content = folder_child.text
elif name == "folder":
folder = folder_child.attrib['idrefs']
folders[folder_map[folder]].append({
"name" : title,
"content" : content
})
json.dump(
{
"snippets":
[
{
"name": key,
"snippets": value
} for key, value in folders.items()
],
"version" : 1
}, open(sys.argv[2], 'w'), indent=2)