-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path.generateDescription.py
76 lines (64 loc) · 2.68 KB
/
.generateDescription.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from xml.dom import minidom
import os
import glob
print (">> Updating README.md based on .codesnippet files")
file = open('README.md', 'w')
file.write('# CodeSnippets\n\n')
file.write('These are my Xcode CodeSnippets. \n')
file.write('To use them, clone this repository into the following path: \n')
file.write('(The folder must be empty, to clone the repository directly in it.) \n\n')
file.write('```sh\n')
file.write('cd ~/Library/Developer/Xcode/UserData/CodeSnippets\n')
file.write('git clone [email protected]:jaydee3/CodeSnippets.git .\n\n')
file.write('```\n')
file.write('And you\'re ready to go.\n\n')
file.write('#### Installing the pre-commit hook \n\n')
file.write('This README is generated automatically using `.generateDescription.py`. \n')
file.write('To run this script automatically before each commit, install the pre-commit hook like this:\n\n')
file.write('```sh\n')
file.write('sh .install-precommit-hook.sh\n\n')
file.write('```\n')
file.write('## Snippet Descriptions\n\n')
# get all filenames and sort by name
listing = os.listdir(".")
listing.sort()
for fileName in listing:
# ignore other files than snippets & ignore focus ones
if fileName.startswith("focus_") or not fileName.endswith(".codesnippet"):
continue
# parse snippet file
xmldoc = minidom.parse(fileName)
keyslist = xmldoc.getElementsByTagName('key')
allChilds = xmldoc.getElementsByTagName('dict')[0].childNodes
for x in allChilds:
if not x.firstChild:
allChilds.remove(x)
title=""
summary=""
shortcut=""
contents=""
lang=""
# parse relevant fields
for key in keyslist:
value = key.firstChild.nodeValue
if (value == "IDECodeSnippetCompletionPrefix"):
shortcut = allChilds[allChilds.index(key)+1].firstChild.nodeValue
elif value == "IDECodeSnippetContents":
contents = allChilds[allChilds.index(key)+1].firstChild.nodeValue
elif value == "IDECodeSnippetSummary":
summary = allChilds[allChilds.index(key)+1].firstChild.nodeValue
elif value == "IDECodeSnippetTitle":
title = allChilds[allChilds.index(key)+1].firstChild.nodeValue
elif value == "IDECodeSnippetLanguage":
langstr = allChilds[allChilds.index(key)+1].firstChild.nodeValue
if langstr.endswith("Swift"):
lang = "Swift"
else:
lang = "Obj-C"
# write snippet description to README.md
file.write('**' + title + '** \n')
file.write(summary + '\n\n')
file.write('Shortcut: `' + shortcut + '` \n')
file.write('File: `' + fileName + '` \n\n')
file.write('```' + lang + '\n' + contents + '\n```\n\n')
file.close()