forked from SonarSource/rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
188 lines (171 loc) · 6.44 KB
/
index.html
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
178
179
180
181
182
183
184
185
186
187
188
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Http Request From JS</title>
<link href="asciidoctor/asciidoctor.css" rel="stylesheet" />
</head>
<body onload="init()">
<h3><label for="branch-name-selector">Repository branch</label></h3>
<select id="branch-name-selector" onchange="onChangeBranch(this.value)">
<!-- dynamically filled by initBranchNameSelector() -->
</select>
<h3><label for="rule-key-selector">Rule Key</label></h3>
<select id="rule-key-selector" onchange="onChangeRuleKey(this.value)">
<!-- dynamically filled by initRuleKeySelector() -->
</select>
<br />
<div id="div-result"></div>
</body>
<script src="asciidoctor/asciidoctor.js"></script>
<script>
function init() {
initBranchNameSelector();
}
/** @type HTMLSelectElement */
function branchNameSelector() {
return /** @type HTMLSelectElement */ document.getElementById("branch-name-selector");
}
/** @type HTMLSelectElement */
function ruleKeySelector() {
return /** @type HTMLSelectElement */ document.getElementById("rule-key-selector");
}
/** @type HTMLDivElement */
function divResult() {
return /** @type HTMLDivElement */ document.getElementById("div-result");
}
function initBranchNameSelector() {
const selectElement = branchNameSelector();
clearSelect(selectElement);
addSelectOption(selectElement, "", "loading...");
httpGetGithubApi("/repos/SonarSource/rspec/branches", function(body) {
const branchList = JSON.parse(body);
clearSelect(selectElement);
for (let i = 0; i < branchList.length; i++) {
const branchName = branchList[i].name;
addSelectOption(selectElement, branchName, branchName);
if (branchName === "master") {
selectElement.selectedIndex = i;
}
}
initRuleKeySelector();
});
}
function fileList(/*string*/relativePath, /*string*/branchName, /*function*/fileListConsumer) {
httpGetGithubApi("/repos/SonarSource/rspec/contents/" + relativePath + "?ref=" + branchName, function (body) {
let files = /** @type object[] */ JSON.parse(body);
fileListConsumer(files);
});
}
function initRuleKeySelector() {
const selectElement = ruleKeySelector();
const branchName = branchNameSelector().value;
clearSelect(selectElement);
addSelectOption(selectElement, "", "loading...");
fileList("rules", branchName, function(files) {
clearSelect(selectElement);
files.filter(file => file.type === "dir")
.sort((a,b) => (parseInt(a.name.substr(1)) < parseInt(b.name.substr(1))) ? -1 : 1)
.forEach(file => addSelectOption(selectElement, file.name, file.name));
displaySelectedRule();
});
}
function displaySelectedRule() {
const branchName = branchNameSelector().value;
const ruleKey = ruleKeySelector().value;
divResult().innerHTML = "loading...";
if (ruleKey === "") {
return;
}
fileList("rules/" + ruleKey, branchName, function(files) {
const languages = /** @type string[] */ files.filter(file => file.type === "dir")
.map(file => file.name)
.sort();
divResult().innerHTML = "" +
"<p>" +
"Languages: " + languages.map(language => "<a href='#language-" + language + "'>" + language + "</a>").join(", ") +
"</p>";
languages.forEach(language => {
const asciiDoctor = Asciidoctor();
const rulesDir = "https://raw.githubusercontent.com/SonarSource/rspec/" + branchName + "/rules";
const ruleFile = rulesDir + "/" + ruleKey + "/" + language + "/rule.adoc";
httpGet(ruleFile, function(body) {
const baseDir = parentDirectory(ruleFile);
const opts = {safe: 'safe', base_dir: baseDir, attributes: { } };
divResult().innerHTML += "<hr /><h1 id='language-" + language + "'>◖ " + language + " ◗</h1><hr />";
divResult().innerHTML += asciiDoctor.convert(body, opts);
},
"*",
function(errorMessage) {
divResult().innerHTML += "<hr /><h1 id='language-" + language + "'>◖ " + language + " ◗</h1><hr />";
divResult().innerHTML += "<p>" + errorMessage + + "</p>";
});
});
});
}
function clearSelect(/*HTMLSelectElement*/selectElement) {
while (selectElement.options.length > 0) {
selectElement.options.remove(0);
}
}
function addSelectOption(/*HTMLSelectElement*/selectElement, /*string*/value, /*string*/text) {
const opt = document.createElement('option');
opt.appendChild(document.createTextNode(text));
opt.value = value;
selectElement.appendChild(opt);
}
function onChangeBranch(/*string*/branchName) {
initRuleKeySelector();
}
function onChangeRuleKey(/*string*/ruleKey) {
displaySelectedRule();
}
function httpGet(/*string*/url, /*function*/bodyConsumer, /*string?*/accept, /*function*/errorConsumer) /*void*/ {
if (typeof accept === "undefined") {
accept = "*";
}
if (typeof errorConsumer === "undefined") {
errorConsumer = function (errorMessage) {
console.error(errorMessage);
};
}
try {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader("Accept", accept);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 0 /*local files*/ || xhr.status === 200) {
bodyConsumer(xhr.responseText);
} else {
errorConsumer("ERROR " + xhr.status + " while loading " + url);
}
}
}
xhr.send();
} catch (e) {
errorConsumer("ERROR: " +exceptionMessage(e));
}
}
function httpGetGithubApi(/*string*/relativeUrl, /*function*/bodyConsumer) {
const url = "https://api.github.com" + relativeUrl;
httpGet(url, bodyConsumer, "application/vnd.github.v3.html");
}
function exceptionMessage(e) {
let message = "";
if (e.message) {
message += e.message;
}
if (e.stack) {
message += ' | stack: ' + e.stack;
}
if (!e.message && !e.stack) {
message += e;
}
return message;
}
function parentDirectory(/*string*/path) {
return path.replace(/\/[^/]*$/, "/");
}
</script>
</html>