-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
183 lines (161 loc) · 5.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Quick Syntax Highlighter</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css">
<link rel="stylesheet" href="css.css">
<link rel="stylesheet" href="styles/all.css">
</head>
<body>
<h1>Quick Syntax Highlighter <small>works with Google Docs — please use Google Chrome</small></h1>
<script src="https://yandex.st/highlightjs/8.0/highlight.min.js"></script>
<script src="styles/index.js"></script>
<p id="first" class="first instructions">
Please paste some code!
<div class="meow" style="display: none">
<div class="text">meow</div>
</div>
</p>
<div id="second" style="display: none">
<p id="text" class="second instructions">Customize and copy!</p>
<div class="box" id="langs">
<strong>Language: </strong>
</div>
<div class="box" id="styles">
<strong>Style: </strong>
</div>
<div class="pre-container hl-s-default" id="pre-container">
<pre class="hljs"></pre>
</div>
</div>
<script>
var get = function(sel) {
return document.querySelector(sel)
}
var pre = document.querySelector('pre.hljs')
var controller = function(sel) {
var container = get(sel)
var active = null
var selected = null
var hovered = null
var actions = { }
var elements = { }
function update() {
var current = hovered || selected
if (current != active) {
if (elements[active]) elements[active].className = ''
actions[current]()
active = current
if (elements[active]) elements[active].className = 'active'
}
}
var out = {
add: function(text, id, callback) {
var a = document.createElement('a')
a.href = 'javascript://'
a.innerHTML = text
a.onclick = function() {
out.select(id)
}
a.onmouseover = function() {
hovered = id
update()
}
a.onmouseout = function() {
hovered = null
update()
}
container.appendChild(a)
actions[id] = callback
elements[id] = a
},
select: function(c) {
selected = c
update()
}
}
return out
}
var ctx = {
code: null,
lang: 'auto'
}
var go = function() {
get('.meow').style.display = 'block'
setTimeout(function() {
get('.meow').style.display = 'none'
get('#first').style.display = 'none'
get('#second').style.display = 'block'
get('#pre-container').appendChild(get('.meow'))
}, 250)
var langs = ["auto", "bash", "cs", "ruby", "diff", "javascript", "xml", "markdown", "css", "http", "java", "php", "python", "sql", "ini", "perl", "objectivec", "coffeescript", "nginx", "json", "apache", "cpp", "makefile"]
var ctrl = controller('#langs')
langs.forEach(function(lang) {
ctrl.add(lang, lang, function() {
ctx.lang = lang
rehighlight()
})
})
ctrl.select('auto')
ctrl = controller('#styles')
HL_S.sort(function(a, b) {
return a.id < b.id ? -1 : 1
})
HL_S.forEach(function(style) {
ctrl.add(style.id, style.id, function() {
get('#pre-container').className = 'pre-container hl-s-' + style.id
})
})
ctrl.select('default')
document.oncopy = function(e) {
var spans = pre.querySelectorAll('span')
var old = pre.innerHTML
;[].forEach.call(spans, function(span) {
var style = window.getComputedStyle(span, null)
var fw = style.fontWeight
var fs = style.fontStyle
var c = style.color
span.className = ''
span.removeAttribute('class')
span.style.fontWeight = fw
span.style.fontStyle = fs
span.style.color = c
})
var html = pre.innerHTML
var cl = window.getComputedStyle(pre, null).color
html = '<pre style="color:' + cl + '">' + html + '</pre>'
console.log(html)
e.clipboardData.setData('text/html', html)
e.clipboardData.setData('text/plain', ctx.code)
pre.innerHTML = old
e.preventDefault()
get('.meow').style.display = 'block'
setTimeout(function() {
get('.meow').style.display = 'none'
}, 250)
}
go = function() {
}
}
function rehighlight() {
var highlighted = ctx.lang == 'auto' ? hljs.highlightAuto(ctx.code) : hljs.highlight(ctx.lang, ctx.code)
pre.innerHTML = highlighted.value
}
function receiveCode(code) {
ctx.code = code
go()
rehighlight()
}
document.onpaste = function(e) {
var code = e.clipboardData.getData('text/plain')
e.preventDefault()
receiveCode(code)
}
</script>
<p class="footer">
Product of <a href="http://spacet.me">spacet<span class="the-dot">.</span>me Labs</a>.
Powered by <a href="http://highlightjs.org/">highlight.js</a>
</p>
</body>
</html>