This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (102 loc) · 3.24 KB
/
index.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// TODO: refactor these.
/**
* Get argument names from a string.
*
* @param {string} str - The string containing placeholders.
*
* @return {object} placeholders - Array keyed with the arguments names and with an array of corresponding placeholders.
*/
function getArgumentsFromString(str) {
return getPlaceholdersFromString(str, '%')
}
/**
* Get placeholder names from a string.
*
* @param {string} str - The string containing placeholders.
* @param {string} prefix - The prefix of the placeholders. Must be Regex-escaped.
*
* @return {object} placeholders - Array keyed with the arguments names and with an array of corresponding placeholders.
*
* If the placeholder with the same name occurs multiple times, there are also
* multiple arrays in the nested array.
*
* Example:
* http://{%first|type=foo}{%first|type=bar}
* becomes:
* Array
* (
* [first] => Array
* (
* [{%first|type=foo}] => Array
* (
* [type] => foo
* )
* [{%first|type=bar}] => Array
* (
* [type] => bar
* )
* )
* )
*/
function getPlaceholdersFromString(str, prefix) {
var pattern = '{' + prefix + '(.+?)}';
var re = RegExp(pattern, 'g');
var match;
var placeholders = {};
do {
match = re.exec(str);
if (!match) {
break;
}
// Example value:
// match[1] = 'query|encoding=utf-8|another=attribute'
var nameAndAttributes = match[1].split('|');
// Example value:
// name = 'query'
var name = nameAndAttributes.shift();
var placeholder = {};
// Example value:
// name_and_attributes = ['encoding=utf-8', 'another=attribute']
for (attrStr of nameAndAttributes) {
[attrName, attrValue] = attrStr.split('=', 2);
placeholder[attrName] = attrValue;
}
placeholders[name] = placeholders[name] || {};
placeholders[name][match[0]] = placeholder;
} while (match);
return placeholders;
}
var fs = require('fs');
jsyaml = require('js-yaml');
let shortcutsPath = '../trovu-data/shortcuts';
namespaces = fs.readdirSync(shortcutsPath);
// Interate over all namespaces.
for (namespace of namespaces) {
let shortcuts = [];
let namespacePath = shortcutsPath + '/' + namespace;
keywords = fs.readdirSync(namespacePath);
// Interate over all keywords.
for (keyword of keywords) {
//shortcuts[keyword] = {};
let keywordPath = namespacePath + '/' + keyword;
filenames = fs.readdirSync(keywordPath) ;
// Interate over all argumentCounts.
for (filename of filenames) {
[argumentCount, extension] = filename.split('.');
let filenamePath = keywordPath + '/' + filename
var yml = fs.readFileSync(filenamePath, 'utf8');
shortcut = jsyaml.safeLoad(yml);
arguments = getArgumentsFromString(shortcut.url);
let obj = {
keyword: keyword,
arguments: Object.keys(arguments),
namespace: namespace,
title: shortcut.title,
};
shortcuts.push(obj);
}
}
let json = JSON.stringify(shortcuts);
let jsonFileName = 'public/suggestions/' + namespace + '.json';
fs.writeFileSync(jsonFileName, json, 'utf8');
}