-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeview.html
executable file
·669 lines (568 loc) · 28 KB
/
codeview.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<!DOCTYPE html>
<html lang="en">
<!-- visit with anchor: #mycode.12-14 -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon"
href="data:image/svg+xml,%3Csvg xmlns='http:https://www.w3.org/2000/svg' width='16' height='16' fill='currentColor' class='bi bi-code-square' viewBox='0 0 16 16'%3E%3Cpath d='M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2z'/%3E%3Cpath d='M6.854 4.646a.5.5 0 0 1 0 .708L4.207 8l2.647 2.646a.5.5 0 0 1-.708.708l-3-3a.5.5 0 0 1 0-.708l3-3a.5.5 0 0 1 .708 0zm2.292 0a.5.5 0 0 0 0 .708L11.793 8l-2.647 2.646a.5.5 0 0 0 .708.708l3-3a.5.5 0 0 0 0-.708l-3-3a.5.5 0 0 0-.708 0z'/%3E%3C/svg%3E"
type="image/svg+xml">
<title>Code View</title>
<script src="https://cdn.jsdelivr.net/npm/mark.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css">
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script> -->
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.css"
data-noprefix />
<script
src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-highlight/prism-line-highlight.css">
<script>
(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined' || !document.querySelector) {
return;
}
var LINE_NUMBERS_CLASS = 'line-numbers';
var LINKABLE_LINE_NUMBERS_CLASS = 'linkable-line-numbers';
var NEW_LINE_EXP = /\n(?!$)/g;
/**
* @param {string} selector
* @param {ParentNode} [container]
* @returns {HTMLElement[]}
*/
function $$(selector, container) {
return Array.prototype.slice.call((container || document).querySelectorAll(selector));
}
/**
* Returns whether the given element has the given class.
*
* @param {Element} element
* @param {string} className
* @returns {boolean}
*/
function hasClass(element, className) {
return element.classList.contains(className);
}
/**
* Calls the given function.
*
* @param {() => any} func
* @returns {void}
*/
function callFunction(func) {
func();
}
// Some browsers round the line-height, others don't.
// We need to test for it to position the elements properly.
var isLineHeightRounded = (function () {
var res;
return function () {
if (typeof res === 'undefined') {
var d = document.createElement('div');
d.style.fontSize = '13px'; // larger.
d.style.lineHeight = '1.5';
d.style.padding = '0';
d.style.border = '0';
d.innerHTML = ' <br /> ';
document.body.appendChild(d);
// Browsers that round the line-height should have offsetHeight === 38
// The others should have 39.
res = d.offsetHeight === 38;
document.body.removeChild(d);
}
return res;
};
}());
/**
* Returns the top offset of the content box of the given parent and the content box of one of its children.
*
* @param {HTMLElement} parent
* @param {HTMLElement} child
*/
function getContentBoxTopOffset(parent, child) {
var parentStyle = getComputedStyle(parent);
var childStyle = getComputedStyle(child);
/**
* Returns the numeric value of the given pixel value.
*
* @param {string} px
*/
function pxToNumber(px) {
return +px.substr(0, px.length - 2);
}
return child.offsetTop
+ pxToNumber(childStyle.borderTopWidth)
+ pxToNumber(childStyle.paddingTop)
- pxToNumber(parentStyle.paddingTop);
}
/**
* Returns whether the Line Highlight plugin is active for the given element.
*
* If this function returns `false`, do not call `highlightLines` for the given element.
*
* @param {HTMLElement | null | undefined} pre
* @returns {boolean}
*/
function isActiveFor(pre) {
if (!pre || !/pre/i.test(pre.nodeName)) {
return false;
}
if (pre.hasAttribute('data-line')) {
return true;
}
if (pre.id && Prism.util.isActive(pre, LINKABLE_LINE_NUMBERS_CLASS)) {
// Technically, the line numbers plugin is also necessary but this plugin doesn't control the classes of
// the line numbers plugin, so we can't assume that they are present.
return true;
}
return false;
}
var scrollIntoView = true;
Prism.plugins.lineHighlight = {
/**
* Highlights the lines of the given pre.
*
* This function is split into a DOM measuring and mutate phase to improve performance.
* The returned function mutates the DOM when called.
*
* @param {HTMLElement} pre
* @param {string | null} [lines]
* @param {string} [classes='']
* @returns {() => void}
*/
highlightLines: function highlightLines(pre, lines, classes) {
lines = typeof lines === 'string' ? lines : (pre.getAttribute('data-line') || '');
var ranges = lines.replace(/\s+/g, '').split(',').filter(Boolean);
var offset = +pre.getAttribute('data-line-offset') || 0;
var parseMethod = isLineHeightRounded() ? parseInt : parseFloat;
var lineHeight = parseMethod(getComputedStyle(pre).lineHeight);
var hasLineNumbers = Prism.util.isActive(pre, LINE_NUMBERS_CLASS);
var codeElement = pre.querySelector('code');
var parentElement = hasLineNumbers ? pre : codeElement || pre;
var mutateActions = /** @type {(() => void)[]} */ ([]);
var lineBreakMatch = codeElement.textContent.match(NEW_LINE_EXP);
var numberOfLines = lineBreakMatch ? lineBreakMatch.length + 1 : 1;
/**
* The top offset between the content box of the <code> element and the content box of the parent element of
* the line highlight element (either `<pre>` or `<code>`).
*
* This offset might not be zero for some themes where the <code> element has a top margin. Some plugins
* (or users) might also add element above the <code> element. Because the line highlight is aligned relative
* to the <pre> element, we have to take this into account.
*
* This offset will be 0 if the parent element of the line highlight element is the `<code>` element.
*/
var codePreOffset = !codeElement || parentElement == codeElement ? 0 : getContentBoxTopOffset(pre, codeElement);
ranges.forEach(function (currentRange) {
var range = currentRange.split('-');
var start = +range[0];
var end = +range[1] || start;
end = Math.min(numberOfLines + offset, end);
if (end < start) {
return;
}
/** @type {HTMLElement} */
var line = pre.querySelector('.line-highlight[data-range="' + currentRange + '"]') || document.createElement('div');
mutateActions.push(function () {
line.setAttribute('aria-hidden', 'true');
line.setAttribute('data-range', currentRange);
line.className = (classes || '') + ' line-highlight';
});
// if the line-numbers plugin is enabled, then there is no reason for this plugin to display the line numbers
// TODO: fix line wrapping problem.
if (hasLineNumbers && Prism.plugins.lineNumbers) {
var startNode = Prism.plugins.lineNumbers.getLine(pre, start);
var endNode = Prism.plugins.lineNumbers.getLine(pre, end);
if (startNode) {
var top = startNode.offsetTop + codePreOffset + 'px';
mutateActions.push(function () {
line.style.top = top;
});
}
if (endNode) {
var height = (endNode.offsetTop - startNode.offsetTop) + endNode.offsetHeight + 'px';
mutateActions.push(function () {
line.style.height = height;
});
}
} else {
mutateActions.push(function () {
line.setAttribute('data-start', String(start));
if (end > start) {
line.setAttribute('data-end', String(end));
}
line.style.top = (start - offset - 1) * lineHeight + codePreOffset + 'px';
line.textContent = new Array(end - start + 2).join(' \n');
});
}
mutateActions.push(function () {
line.style.width = pre.scrollWidth + 'px';
});
mutateActions.push(function () {
// allow this to play nicely with the line-numbers plugin
// need to attack to pre as when line-numbers is enabled, the code tag is relatively which screws up the positioning
parentElement.appendChild(line);
});
});
var id = pre.id;
if (hasLineNumbers && Prism.util.isActive(pre, LINKABLE_LINE_NUMBERS_CLASS) && id) {
// This implements linkable line numbers. Linkable line numbers use Line Highlight to create a link to a
// specific line. For this to work, the pre element has to:
// 1) have line numbers,
// 2) have the `linkable-line-numbers` class or an ascendant that has that class, and
// 3) have an id.
if (!hasClass(pre, LINKABLE_LINE_NUMBERS_CLASS)) {
// add class to pre
mutateActions.push(function () {
pre.classList.add(LINKABLE_LINE_NUMBERS_CLASS);
});
}
var start = parseInt(pre.getAttribute('data-start') || '1');
// iterate all line number spans
$$('.line-numbers-rows > span', pre).forEach(function (lineSpan, i) {
var lineNumber = i + start;
lineSpan.onclick = function () {
var hash = id + '.' + lineNumber;
// this will prevent scrolling since the span is obviously in view
scrollIntoView = false;
location.hash = hash;
setTimeout(function () {
scrollIntoView = true;
}, 1);
};
});
}
return function () {
mutateActions.forEach(callFunction);
};
}
};
function applyHash() {
var hash = location.hash.slice(1);
// Remove pre-existing temporary lines
$$('.temporary.line-highlight').forEach(function (line) {
line.parentNode.removeChild(line);
});
var range = (hash.match(/\.([\d,-]+)$/) || [, ''])[1];
if (!range || document.getElementById(hash)) {
return;
}
var id = hash.slice(0, hash.lastIndexOf('.'));
var pre = document.getElementById(id);
// if (pre.classList.contains("wrap_pre")){return}
if (!pre) {
return;
}
// now we have hash.
if (!pre.hasAttribute('data-line')) {
pre.setAttribute('data-line', '');
}
var mutateDom = Prism.plugins.lineHighlight.highlightLines(pre, range, 'temporary ');
mutateDom();
if (scrollIntoView) {
document.querySelector('.temporary.line-highlight').scrollIntoView();
}
}
var fakeTimer = 0; // Hack to limit the number of times applyHash() runs
Prism.hooks.add('before-sanity-check', function (env) {
var pre = env.element.parentElement;
if (!isActiveFor(pre)) {
return;
}
/*
* Cleanup for other plugins (e.g. autoloader).
*
* Sometimes <code> blocks are highlighted multiple times. It is necessary
* to cleanup any left-over tags, because the whitespace inside of the <div>
* tags change the content of the <code> tag.
*/
var num = 0;
$$('.line-highlight', pre).forEach(function (line) {
num += line.textContent.length;
line.parentNode.removeChild(line);
});
// Remove extra whitespace
if (num && /^(?: \n)+$/.test(env.code.slice(-num))) {
env.code = env.code.slice(0, -num);
}
});
Prism.hooks.add('complete', function completeHook(env) {
var pre = env.element.parentElement;
if (!isActiveFor(pre)) {
return;
}
clearTimeout(fakeTimer);
var hasLineNumbers = Prism.plugins.lineNumbers;
var isLineNumbersLoaded = env.plugins && env.plugins.lineNumbers;
if (hasClass(pre, LINE_NUMBERS_CLASS) && hasLineNumbers && !isLineNumbersLoaded) {
Prism.hooks.add('line-numbers', completeHook);
} else {
var mutateDom = Prism.plugins.lineHighlight.highlightLines(pre);
mutateDom();
fakeTimer = setTimeout(applyHash, 1);
}
});
window.addEventListener('hashchange', applyHash);
function conditionalApplyHash() {
const pre_elem = document.getElementById('mycode');
const loaded = pre_elem.getAttribute('data-src-status');
if (loaded == "loaded") {
applyHash();
// Prism.highlightElement(pre_elem);
// Prism.highlightAll()
// pre_elem.classList.add("wrap_pre");
// setTimeout(() => {pre_elem.classList.add("wrap_pre");}, 1000);
// pre_elem.style.whiteSpace="pre-wrap !important";
} else {
setTimeout(conditionalApplyHash, 500)
}
}
function getQueryParams() {
var search = window.location.search.substring(1); // Remove leading '?'
var queryParams = {};
search.split('&').forEach(function (pair) {
var parts = pair.split('=');
var key = decodeURIComponent(parts[0]);
var value = decodeURIComponent(parts[1]);
queryParams[key] = value;
});
return queryParams;
}
function loadedAction() {
// console.log('location search:', window.location.search);
const section_elem = document.getElementById('code-section');
const pre_elem = document.createElement('pre');
const queryParams = getQueryParams(window.location.search);
// const queryParams = new URLSearchParams(window.location.search);
const language = queryParams.language;
const code_path = queryParams.file;
const project_name = queryParams.project;
const keywords = queryParams.keywords;
var keywordList
try {
keywordList = JSON.parse(keywords);
} catch (e) { }
document.title = `Code view of: ${code_path} - Project: ${project_name}`;
const h1_element = document.getElementById('code-path');
// h1_element.textContent = code_path.slice('src/'.length);
// debugger;
// let mycodepath = project_name + "/" + code_path.slice('src/'.length);
// `?full=true#${manchor}`
let myhtml = `<a href="tree.html?full=true#/">${project_name}</a>`;
let myslices = code_path.slice('src/'.length).split('/')
// debugger;
let accpath = ""
for (let i in myslices) {
s = myslices[i];
accpath += ("/" + s)
var maccpath = accpath
if (i != (myslices.length - 1)) {
maccpath = accpath + "/"
}
myhtml += `/<a href="tree.html?full=true#${maccpath}">${s}</a>`
}
h1_element.innerHTML = myhtml;
// pre_elem.className = `language-${language}`
pre_elem.id = "mycode";
const code_elem = document.createElement('code');
code_elem.className = `language-${language}`
var xhr = new XMLHttpRequest();
console.log("trying: " + code_path)
xhr.open('GET', code_path, false); // The third parameter is set to false for synchronous request
xhr.send(null);
if (xhr.status == 200) {
code_elem.textContent = xhr.responseText;
} else {
var xhr = new XMLHttpRequest();
// TODO: mitigate this evil hack by passing more info of the original project.
// the reason is that github does not allow accessing file with '_' as prefix.
// is that disabled for security reasons?
var newLink = `https://raw.githubusercontent.com/James4Ever0/${project_name}/main/docs/` + code_path
console.log("trying: " + newLink)
xhr.open('GET', newLink, false); // The third parameter is set to false for synchronous request
xhr.send(null);
if (xhr.status == 200) {
code_elem.textContent = xhr.responseText;
} else {
var xhr = new XMLHttpRequest();
// TODO: mitigate this evil hack by passing more info of the original project.
var newLink = `https://raw.githubusercontent.com/James4Ever0/${project_name}/master/docs/` + code_path
xhr.open('GET', newLink, false); // The third parameter is set to false for synchronous request
xhr.send(null);
if (xhr.status == 200) {
code_elem.textContent = xhr.responseText;
} else {
var xhr = new XMLHttpRequest();
// TODO: mitigate this evil hack by passing more info of the original project.
var newLink = `https://raw.githubusercontent.com/James4Ever0/${project_name}_doc/master/` + code_path
console.log("trying: " + newLink)
xhr.open('GET', newLink, false); // The third parameter is set to false for synchronous request
xhr.send(null);
if (xhr.status == 200) {
code_elem.textContent = xhr.responseText;
} else {
var xhr = new XMLHttpRequest();
// TODO: mitigate this evil hack by passing more info of the original project.
var newLink = `https://raw.githubusercontent.com/James4Ever0/${project_name}_doc/main/` + code_path
console.log("trying: " + newLink)
xhr.open('GET', newLink, false); // The third parameter is set to false for synchronous request
xhr.send(null);
if (xhr.status == 200) {
code_elem.textContent = xhr.responseText;
} else {
code_elem.textContent = "Failed to load code."
}
}
}
}
}
pre_elem.appendChild(code_elem);
// pre_elem.setAttribute("data-src", code_path);
section_elem.appendChild(pre_elem)
// Prism.highlightAllUnder(pre_elem);
Prism.highlightAllUnder(document.getElementById("code-div"));
// Prism.highlightAllUnder(document.getElementById("code-section"));
if (window.location.hash != "") {
// conditionalApplyHash();
applyHash();
} else {
// Prism.highlightAll();
// Prism.highlightElement(pre_elem);
// setTimeout(() => {pre_elem.classList.add("wrap_pre");}, 1000);
// pre_elem.style.whiteSpace="pre-wrap !important";
}
if (keywordList != undefined) {
setInterval(() => {
const markInstance = new Mark(document.getElementById('code-div'));
markInstance.unmark(); // Clear previous marks
markInstance.mark(keywordList);
}, 1000)
}
// applyHash();
// Prism.highlightElement(pre_elem, () => {applyHash()});
// Prism.highlightElement(pre_elem).then(applyHash);
}
// window.addEventListener('load', loadedAction);
document.addEventListener('DOMContentLoaded', loadedAction);
window.addEventListener('resize', function () {
var actions = $$('pre')
.filter(isActiveFor)
.map(function (pre) {
return Prism.plugins.lineHighlight.highlightLines(pre);
});
actions.forEach(callFunction);
});
}());
</script>
<style>
/*
html, body{
background-color: transparent;
} */
#code-path {
overflow-x: scroll;
direction: rtl;
/* opt1 */
/* word-wrap: break-word;
overflow-wrap: break-word */
/* opt2 */
/* white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
direction: rtl; */
}
html,
body {
/* max-width: 900px; */
/* margin: 0 auto; */
/* margin-left: 5%;
margin-right: 5%; */
height: 100%;
display: flex;
flex-direction: column;
font-family: 'Roboto', sans-serif;
/* Adjust based on the height of the header */
}
section {
flex: 1;
/* margin: 0 auto; */
/* Provide spacing to accommodate the fixed header */
/* Ensure the section fills the remaining viewport height */
overflow-y: auto;
/* overflow-x:hidden; */
/* white-space: pre-wrap; */
/* Enable vertical scrolling if content exceeds viewport height */
}
/* .line-highlight {
background-color: rgba(228, 239, 12, 0.07) !important;
} */
/* do this after jump */
/* .line-highlight {
background-color: rgba(0, 0, 0, 0) !important;
background-image: linear-gradient(to right, rgba(121, 96, 72, 0.1) 70%, rgba(121, 96, 72, 0)) !important;
} */
pre {
/* white-space: pre-line !important; */
white-space: pre-wrap !important;
}
.container {
display: flex;
flex-direction: column;
align-items: left;
justify-content: center;
text-align: left;
}
.monospace-text {
font-family: "Courier New", monospace;
color: #333;
}
@media (max-width: 767px) {
.container {
padding-left: 10px;
}
html,
body {
margin-left: 3%;
margin-right: 3%;
}
#code-section {
font-size: 14px;
}
}
#code-section {
border: 1px solid #ccc;
}
/* Styles for desktop devices */
@media (min-width: 768px) {
.container {
padding-left: 1.6%;
}
html,
body {
margin-left: 5%;
margin-right: 5%;
}
#code-section {
font-size: 17px;
}
}
</style>
</head>
<!-- <body> -->
<body class="line-numbers">
<header class="container" data-plugin-header="line-numbers">
<p id="code-path" class="monospace-text">Code Preview</p>
</header>
<div id="code-div">
<section id="code-section">
<!-- <pre id="mycode" class="language-python" data-src="code_view_demo.py">
</pre> -->
</section>
</div>
</body>
</html>