forked from HubSpot/hubspot-cms-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubl_snippets_gen.py
58 lines (52 loc) · 2.18 KB
/
hubl_snippets_gen.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
import json
import requests
hubl = requests.get('https://api.hubspot.com/cos-rendering/v1/hubldoc').json()
skips = {'for', 'if', 'block', 'widget_block'}
nl = "\n"
def writePrettySnippetJson(filePath, snippetJson):
with open(f'./snippets/auto_gen/{filePath}.json', 'w') as outfile:
json.dump(snippetJson, outfile, indent=4, sort_keys=True)
def paramify(paramType, param, hublType):
if paramType == 'String':
builtParam = f'"${{{param}}}"'
else:
builtParam = f'${{{param}}}'
if hublType == 'tags':
builtParam = f'\n\t{param}={builtParam}'
return builtParam
def buildBody(bodyParams, hublType, name):
if hublType == 'functions':
body = f'{name}(' + bodyParams + ')'
elif hublType == 'filters':
body = f'|{name}{f"({bodyParams})" if bodyParams else ""}'
elif hublType == 'tags':
body = f'{{% {name} "my_{name}"\n\t' + bodyParams + '\n%}'
elif hublType == 'expTests':
body = name
return body
def buildHubLSnippets(hublType, prefixChar):
snippets = {}
hublObjects = hubl[hublType]
for hublObject in hublObjects:
name = hublObjects[hublObject]['name']
if name not in skips:
descParams = ''
bodyParams = ''
snippet = {}
snippet['prefix'] = f'{prefixChar}{name}'
desc = hublObjects[hublObject]['desc']
params = hublObjects[hublObject]['params']
for i, param in enumerate(params):
descParams += f'- {param["name"].replace(" ","_")}({param["type"]}) {param["desc"]}\n'
if i == 0 and hublType == 'filters':
continue
bodyParams += paramify(param["type"], param["name"].replace(" ","_"), hublType) + ', '
bodyParams = bodyParams.strip()[:-1]
snippet['body'] = [buildBody(bodyParams, hublType, name)]
snippet['description'] = f'{desc}{f"{nl}Parameters:{nl}{descParams}" if descParams else ""}'
snippets[name] = snippet
writePrettySnippetJson(f'hubl_{hublType}', snippets)
buildHubLSnippets('filters', '|')
buildHubLSnippets('functions', '~')
buildHubLSnippets('tags', '~')
buildHubLSnippets('expTests', '')