-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow-source.js
More file actions
83 lines (58 loc) · 2.03 KB
/
show-source.js
File metadata and controls
83 lines (58 loc) · 2.03 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
(function () {
var script = document.currentScript
var url = script.dataset.href || ""
var html = /*html*/`
<div>
<b><a href="${url}">Text: ${url}</a></b>
<pre class="text_viewer" contenteditable="true"
style=" overflow-y: scroll; border:1px solid black; padding: 1em; "
spellcheck="false" translate="no">
</pre>
<style>
/* Syntax highlighting colors */
.tag,
.tag-inside {
color: blue;
}
.tag-name,
.tag {
font-weight: bold;
}
.tag-name,
.tag {
color: darkblue;
}
</style>
</div>`
script.insertAdjacentHTML("afterend", html)
var text_viewer = script.nextElementSibling.querySelector(".text_viewer")
fetch(url).then(response => response.text()).
then(response_text => {
text_viewer.textContent = response_text
text_coloring(text_viewer)
})
function text_parsing(html) {
// begin with escapings
html = html.replaceAll('&', "&") // HTML entities escaping
html = html.replaceAll('-tag' + '}', '-tag' + '}') // {open-tag} {close-tag} escaping
html = html.replaceAll("?>", "?>") // for PHP tags escaping
html = html.replaceAll("<", "{open-tag}")
html = html.replaceAll(">", "{close-tag}")
// stricter : only <az> or </az> tags are matched ( also see PHP tags escaping above )
html = html.replaceAll(/{open-tag}(\/?[a-zA-Z][\s\S]*?){close-tag}/g,
"<span class='tag'><</span>"
+ "<span class='tag-inside'>$1</span>"
+ "<span class='tag'>></span>")
html = html.replaceAll("{open-tag}", "<") // <
html = html.replaceAll("{close-tag}", ">") // >
return html
}
function text_coloring(text_viewer) {
var html = text_viewer.textContent
html = text_parsing(html)
text_viewer.innerHTML = html
for (var tagInside of text_viewer.querySelectorAll(".tag-inside")) {
tagInside.innerHTML = tagInside.innerHTML.replace(/^(\/?\w+)(.*)/, "<span class='tag-name'>$1</span>$2")
}
}
})()