-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythoncopy.html
More file actions
241 lines (220 loc) · 8.25 KB
/
pythoncopy.html
File metadata and controls
241 lines (220 loc) · 8.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Code Display Tool</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css" id="highlight-theme">
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
padding: 20px;
}
.input-container {
margin-bottom: 20px;
}
.input-container input[type="text"] {
width: 80%;
padding: 10px;
font-size: 16px;
}
.input-container button {
padding: 10px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}
.code-container {
position: relative;
width: 100%;
padding: 1em;
background: #f5f5f5;
border-radius: 5px;
overflow: auto;
}
.button-container {
position: absolute;
top: 10px;
left: 10px;
}
.button-container button {
background: #007bff;
color: white;
border: none;
padding: 5px 10px;
margin-right: 5px;
cursor: pointer;
border-radius: 5px;
}
.controls-container {
text-align: center;
margin: 20px 0;
}
.controls-container input[type="range"] {
width: 200px;
}
.hljs-ln-numbers {
text-align: right;
border-right: 1px solid #ccc;
padding-right: 10px;
margin-right: 10px;
min-width: 40px;
}
.hljs-ln-code {
padding-left: 10px;
}
@media print {
.input-container,
.controls-container,
.button-container {
display: none;
}
</style>
</head>
<body>
<div class="input-container" id="inputContainer">
<input type="text" id="urlInput" placeholder="Enter the raw gist URL here...">
<button onclick="updateURL()">Load Code</button>
</div>
<div class="controls-container">
<label for="mode">Mode:</label>
<input type="radio" id="light-mode" name="mode" value="light" checked> Light
<input type="radio" id="dark-mode" name="mode" value="dark"> Dark
<br>
<label for="font-size">Font Size:</label>
<input type="range" id="font-size" min="10" max="30" value="16">
</div>
<div class="code-container" id="codeContainer" style="display: none;">
<div class="button-container">
<button class="copy-button">Copy</button>
<button class="download-py-button">Download .py</button>
<button class="download-txt-button">Download .txt</button>
<button class="fullscreen-button">Full Screen</button>
<button class="visualise-button">Visualise</button>
<button class="make-puzzle-button">Make Puzzle</button> <!-- Make Puzzle button -->
</div>
<pre><code id="code" class="python hljs"></code></pre>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script>
<script>
let codeText = '';
let currentURL = '';
document.addEventListener('DOMContentLoaded', () => {
const urlParams = new URLSearchParams(window.location.search);
const url = urlParams.get('url');
if (url) {
fetchCode(url);
} else {
document.getElementById('inputContainer').style.display = 'block';
}
document.getElementById('light-mode').addEventListener('change', function() {
document.getElementById('highlight-theme').href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css';
document.querySelector('.code-container').style.backgroundColor = '#f5f5f5';
});
document.getElementById('dark-mode').addEventListener('change', function() {
document.getElementById('highlight-theme').href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/atom-one-dark.min.css';
document.querySelector('.code-container').style.backgroundColor = '#2d2d2d';
});
document.getElementById('font-size').addEventListener('input', function() {
document.getElementById('code').style.fontSize = this.value + 'px';
});
document.querySelector('.copy-button').addEventListener('click', function() {
navigator.clipboard.writeText(codeText).then(() => {
alert('Code copied to clipboard!');
}, (err) => {
console.error('Could not copy text: ', err);
});
});
document.querySelector('.download-py-button').addEventListener('click', function() {
const blob = new Blob([codeText], { type: 'text/x-python' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'code.py';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
document.querySelector('.download-txt-button').addEventListener('click', function() {
const blob = new Blob([codeText], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'code.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
document.querySelector('.fullscreen-button').addEventListener('click', function() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen().catch(err => {
alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
});
} else {
document.exitFullscreen();
}
});
document.querySelector('.visualise-button').addEventListener('click', function() {
const baseURL = "https://pythontutor.com/iframe-embed.html";
const encodedCode = encodeURIComponent(codeText);
const params = `#code=${encodedCode}&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false`;
const fullURL = `${baseURL}${params}`;
window.open(fullURL, '_blank');
});
document.querySelector('.make-puzzle-button').addEventListener('click', function() {
if (currentURL) {
const puzzleURL = `https://jamesabela.github.io/jsfun/reorder.html?url=${encodeURIComponent(currentURL)}`;
window.open(puzzleURL, '_blank');
} else {
alert('No valid URL available to create a puzzle.');
}
});
document.addEventListener('fullscreenchange', () => {
const button = document.querySelector('.fullscreen-button');
if (document.fullscreenElement) {
button.textContent = 'Exit Full Screen';
} else {
button.textContent = 'Full Screen';
}
});
});
function updateURL() {
const inputURL = document.getElementById('urlInput').value.trim();
if (inputURL === '') {
alert('Please enter a valid URL.');
return;
}
const newURL = `${window.location.pathname}?url=${encodeURIComponent(inputURL)}`;
window.history.pushState({}, '', newURL);
fetchCode(inputURL);
}
function fetchCode(url) {
currentURL = url;
fetch(url)
.then(response => response.text())
.then(data => {
codeText = data;
const codeElement = document.getElementById('code');
codeElement.innerHTML = data.split('\n').map((line, index) => `<span class="hljs-ln-numbers">${index + 1} </span>${line} `).join('\n');
hljs.highlightElement(codeElement);
document.getElementById('codeContainer').style.display = 'block';
document.getElementById('inputContainer').style.display = 'none';
})
.catch(error => {
alert('Failed to load code. Please ensure the URL is correct and points to a raw Python file.');
console.error('Error fetching code:', error);
});
}
</script>
<small> <a href="https://creativecommons.org/licenses/by-nc/4.0/">CC BY-NC 4.0</a> © James Abela</small>
<script async defer src="https://scripts.simpleanalyticscdn.com/latest.js"></script>
<noscript><img src="https://queue.simpleanalyticscdn.com/noscript.gif" alt="" referrerpolicy="no-referrer-when-downgrade" /></noscript>
</body>
</html>