-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler-explorer.js
More file actions
95 lines (88 loc) · 3.25 KB
/
Copy pathcompiler-explorer.js
File metadata and controls
95 lines (88 loc) · 3.25 KB
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
(function () {
const ce_nodes = document.querySelectorAll('.ce');
for (let i = 0, len = ce_nodes.length; i < len; i++) {
let element = ce_nodes[i];
let compiler = "g71";
let options = "-O1"
let source = unescape(element.textContent);
let lines = source.split('\n');
source = "";
let displaySource = "";
let matcher = /^\/\/\/\s*([^:]+):(.*)$/;
let skipDisplay = false;
for (let idx = 0; idx < lines.length; ++idx) {
let line = lines[idx];
let match = line.match(matcher);
if (match) {
compiler = match[1];
options = match[2];
} else if (line.trim() !== "") {
if (line === "// setup") {
skipDisplay = true;
} else if (line[0] != ' ') {
skipDisplay = false;
}
source += line + "\n";
if (!skipDisplay)
displaySource += line + "\n"
}
}
let content = [];
content.push({
type: 'component',
componentName: 'codeEditor',
componentState: {
id: 1,
source: source,
options: {compileOnChange: true, colouriseAsm: true},
fontScale: 1.6
}
});
content.push({
type: 'component',
componentName: 'compiler',
componentState: {
source: 1,
filters: {commentOnly: true, directives: true, intel: true, labels: true, trim: true},
options: options,
compiler: compiler,
fontScale: 1.8
}
});
let obj = {
version: 4,
content: [{type: 'row', content: content}]
};
let ceFragment = encodeURIComponent(JSON.stringify(obj));
let parent = element.parentElement;
//const isPdf = !!window.location.search.match(/print-pdf/gi);
//const baseUrl = isPdf ? 'https://gcc.godbolt.org/' : 'http://localhost:10240/';
const baseUrl = 'https://gcc.godbolt.org/';
if (parent.tagName === "PRE") {
let a = document.createElement('a');
a.setAttribute('href', baseUrl + "#" + ceFragment);
a.setAttribute('target', '_blank');
a.setAttribute('class', 'view-button');
a.textContent = 'View';
parent.parentElement.appendChild(a);
element.textContent = displaySource;
} else {
element.remove();
let ceElement = document.createElement('iframe');
ceElement.setAttribute('width', '1200px');
ceElement.setAttribute('height', '300px');
parent.appendChild(ceElement);
let embedUrl = baseUrl + "e#" + ceFragment;
if (isPdf) {
ceElement.setAttribute('src', embedUrl);
} else {
Reveal.addEventListener('slidechanged', (e) => {
if (e.currentSlide.contains(ceElement)) {
ceElement.setAttribute('src', embedUrl);
Reveal.sync();
}
});
}
}
}
})();