-
Notifications
You must be signed in to change notification settings - Fork 1
/
regulationsAndGuidelines.js
177 lines (151 loc) · 5.57 KB
/
regulationsAndGuidelines.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* takes both regulations and guidelins and combins them into one single json */
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const almphanumbericCompare = require('alphanumeric-sort').compare;
const markdownItAST = require('markdown-it-ast')
const markdownIt = require('markdown-it')({
html: true
});
const regexes = {
article: /article\-(\w+)/,
label: /\[(\w+)\]\s(.+)/,
guideline: /(\w+)(\++)\)\s\[(\w+)\]\s(.+)/,
regulation: /(\w+)\)\s(.+)/,
}
const getArticle = (regulationId) => regulationId.match(/(1[012]|[1-9]|[A-Z])\w+/) ? regulationId.match(/(1[012]|[1-9]|[A-Z])\w+/)[1] : null
const parseDescription = (description, inlineToken) => {
let desc = [{
content: description
}];
for (let i = 1; i < inlineToken.children.length;) {
let child = inlineToken.children[i];
if (child.type === 'text') {
desc.push({
content: child.content
});
i++;
} else if (child.type === 'link_open') {
desc.push({
href: child.attrs[0][1],
content: inlineToken.children[i+1].content
});
// TODO: figure out how we want to link regulations;
i += 3;
}
}
return desc;
}
const findArticle = (articles, article) => articles.find(a => a.id.slice(0, article.length) === article);
const rules = {
heading (state, token, level) {
let inline = token.children[0];
if (inline.content[0] !== '<') // Probably not worth looking at
return;
let trimmed = inline.children.map(i => i.content.replace('<', '').replace('>', ''))
let article = regexes.article.exec(trimmed[0]);
// looking at an article and hasn't been added yet
if (article && !findArticle(state.articles, article[1])) {
state.articles.push({
id: article[1],
name: trimmed[1],
name2: trimmed[2],
description: trimmed[3],
regulations: []
});
}
},
// form of text -> inline -> paragraph -> listitem
html_inline (state, token, level) {
if (token.content === '<version>') {
state.version = token.parent.children[1].content.replace('Version: ', '');
} else if (token.content === '<label>') {
let label = regexes.label.exec(token.parent.content);
state.labels.push({
type: label[1],
description: label[2]
});
}
},
// look for text nodes inside a list item node that isn't a label
list_item (state, token, level) {
let inline = token.children[0].children[0];
if (inline.children[0].content !== '<label>') { // regulation / guideline
let textToken = inline.children[0];
let guideline = regexes.guideline.exec(textToken.content);
if (guideline !== null) {
let article = findArticle(state.articles, getArticle(guideline[1]));
let regulation = article.regulations.find(r => r.id === guideline[1]);
if (regulation) { // Not all guidelines attach to regulations
regulation.guidelines.push({
id: guideline[1],
level: guideline[1].length - 1,
pluses: guideline[2],
label: guideline[3],
description: parseDescription(guideline[4], inline)
});
} else { // create a dummy regulation with no description and just the guideline
article.regulations.push({
id: guideline[1],
level: guideline[1].length - 1,
guidelines: [{
id: guideline[1],
level: guideline[1].length - 1,
pluses: guideline[2],
label: guideline[3],
description: parseDescription(guideline[4], inline)
}]
});
}
} else { // looks like it's a regulation
let regulation = regexes.regulation.exec(textToken.content);
let article = findArticle(state.articles, getArticle(regulation[1]));
if (article) {
article.regulations.push({
id: regulation[1],
level: regulation[1].length - 1,
description: parseDescription(regulation[2], inline),
guidelines: []
});
}
}
}
}
};
const traverse = (state, token, level) => {
if (!token.type) {
token.type = token.nodeType; // freaking inconsistency
}
if (rules[token.type]) {
rules[token.type](state, token, level);
}
if (token.children) {
token.children.forEach(child => {
child.parent = token;
traverse(state, child, level + 1)
});
}
}
const buildRegsAndGuidelines = () => {
const regulations = String(fs.readFileSync(path.resolve(__dirname, './wca-regulations/wca-regulations.md')));
const guidelines = String(fs.readFileSync(path.resolve(__dirname, './wca-regulations/wca-guidelines.md')));
const state = {
version: null,
labels: [],
articles: [],
};
markdownItAST.makeAST(markdownIt.parse(regulations, {}))
.forEach(child => traverse(state, child, 0));
markdownItAST.makeAST(markdownIt.parse(guidelines, {}))
.forEach(child => traverse(state, child, 0));
state.articles.sort((a,b) => almphanumbericCompare(a.id, b.id))
return state;
}
const outputFile = path.resolve(__dirname, './src/assets/regulationsAndGuidelines.json');
const commit = String(fs.readFileSync(path.resolve(__dirname, '.git/modules/wca-regulations/HEAD'))).substring(0, 7);
console.log('Building with wca-regulations commit', chalk.green(commit))
const regsAndGuidlinesJSON = buildRegsAndGuidelines();
console.log(`Regs version: ${chalk.green(regsAndGuidlinesJSON.version)}`);
fs.writeFile(outputFile, JSON.stringify(regsAndGuidlinesJSON), function () {
console.log(`Wrote to ${chalk.green(outputFile)}`);
});