-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathview.html
175 lines (150 loc) · 5.85 KB
/
view.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>View</title>
<style>
a { text-decoration: none; }
</style>
</head>
<body>
<div id="content"></div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
let numExistingElements = 0; // Always growing
let numAddedElements = 0; // Reset to 0 after each reload
let prevStackDescription = null; // Previous stack description, print if changed
function parseUrlParams() {
const urlParams = {};
const data = window.location.hash.slice(1);
if (data) {
const items = data.split("&");
for (let item of items) {
const [key, value] = item.split("=");
urlParams[key] = value;
}
}
return urlParams;
}
function serializeUrlParams(urlParams) {
const items = [];
for (let key in urlParams) {
items.push(key + "=" + encodeURIComponent(urlParams[key]));
}
window.location.hash = "#" + items.join("&");
}
function reload() {
// Remove old script element if it exists
const oldScript = document.getElementById("script");
if (oldScript) {
document.head.removeChild(oldScript);
}
// Figure out what content to load
const urlParams = parseUrlParams();
if (!urlParams.load) {
urlParams.load = prompt("Enter file to load (e.g., lecture_06-content.js)");
console.log(urlParams);
serializeUrlParams(urlParams);
}
// Create a new script element
const script = document.createElement("script");
script.id = "script";
script.type = "text/javascript";
script.src = withTimestamp(urlParams.load);
document.head.appendChild(script);
numAddedElements = 0;
}
function addContent(stack, element) {
const content = document.getElementById("content");
const div = document.createElement("div");
// Show stack only if it has changed
const stackDescription = stack.map((frame) => frame.name).join(" / ");
if (stackDescription !== prevStackDescription) {
const stackDiv = document.createElement("h4");
stackDiv.appendChild(document.createTextNode(stackDescription));
div.appendChild(stackDiv);
prevStackDescription = stackDescription;
}
// Prefix provides link to the GitHub
const prefixSpan = document.createElement("a");
const verticalRectangle = "▮"
const prefix = verticalRectangle + " ";
prefixSpan.href = getStackFrameUrl(stack[stack.length - 1]);
prefixSpan.target = "_blank";
prefixSpan.style.color = "lightgray";
prefixSpan.innerHTML = prefix;
div.appendChild(prefixSpan);
// Add the actual element
div.appendChild(element);
content.appendChild(div);
window.scrollTo(0, document.body.scrollHeight);
}
// Functions that the script can call
function addText(stack, text, style) {
if (!shouldAddElement()) {
return;
}
const textSpan = document.createElement("span");
setStyle(textSpan, style);
if (isUrl(text)) {
// If the text is a URL, make it a link
const link = document.createElement("a");
link.href = text;
link.target = "_blank";
link.appendChild(document.createTextNode(text));
textSpan.appendChild(link);
} else {
if (style["font-family"] === "monospace") {
// Verbatim
textSpan.appendChild(document.createTextNode(text));
} else {
// Interpret as markdown
let html = marked.parse(text)
html = html.replace("<p>", "").replace("</p>", "");
html = html.replace("<ul>", "").replace("</ul>", "");
html = html.replace("<li>", "• ").replace("</li>", "");
textSpan.innerHTML = html;
}
}
addContent(stack, textSpan);
}
function addImage(stack, path, style) {
if (!shouldAddElement()) {
return;
}
// Create the image
const image = document.createElement("img");
image.src = withTimestamp(path);
setStyle(image, style);
addContent(stack, image);
}
function shouldAddElement() {
numAddedElements += 1;
if (numAddedElements <= numExistingElements) {
return false;
}
numExistingElements += 1;
return true;
}
function setStyle(elem, style) {
if (style) {
for (const key in style) {
elem.style[key] = style[key];
}
}
}
function withTimestamp(path) {
// Append a unique query string to the image src URL to bypass the cache
return path + "?" + new Date().getTime();
}
function isUrl(text) {
return text.startsWith("http://") || text.startsWith("https://");
}
function getStackFrameUrl(frame) {
return "https://github.com/stanford-cs336/spring2024-lectures/blob/main/" + frame.filename + "#L" + frame.lineno;
}
reload();
setInterval(reload, 1000);
</script>
</body>
</html>