forked from jagt/python-single-line-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
228 lines (224 loc) · 6.73 KB
/
index.html
File metadata and controls
228 lines (224 loc) · 6.73 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
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>Python Single Line Converter</title>
<style type="text/css">
body{
font-family: Tahoma, Verdana, Segoe, sans-serif;
color: #444444;
line-height: 1;
width: 95%;
}
h1, h2, h3, h4 {
font-family: Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif;
color: #111111;
font-weight: 400;
}
h1, h2, h3, h4, h5 {
margin-bottom: 24px;
font-weight: bold;
padding: 0;
}
h1 {
font-size: 48px;
}
h2 {
font-size: 36px;
margin: 24px 0 6px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 21px;
}
h5 {
font-size: 18px;
}
a {
color: #0099ff;
margin: 0;
padding: 0;
vertical-align: baseline;
}
a h2 {
color: #0099ff;
}
ul, ol {
padding: 0;
margin: 0;
}
li {
line-height: 24px;
}
li ul, li ul {
margin-left: 24px;
}
p, ul, ol {
font-size: 16px;
line-height: 24px;
max-width: 720px;
}
pre {
padding: 0px 24px;
max-width: 800px;
white-space: pre-wrap;
}
code {
font-family: Consolas, Monaco, Andale Mono, monospace;
line-height: 1.5;
font-size: 13px;
}
aside {
display: block;
float: right;
width: 390px;
}
blockquote {
margin: 1em 2em;
max-width: 476px;
}
blockquote p {
color: #666;
max-width: 460px;
}
hr {
width: 540px;
text-align: left;
margin: 0 auto 0 0;
color: #999;
}
/* layouts */
#wrapper {
margin: 4em auto 4em;
width: 70%;
}
</style>
<!--[if lt IE 9 ]>
<script>
var is_ie_lt9 = true;
</script>
<![endif]-->
<script type="text/javascript">
// trim polyfill from somewhere on the internet
if(!String.prototype.trimLeft){
String.prototype.trimLeft = function() {
return this.replace(/^[\s| ]+/g, "");
};
String.prototype.trimRight = function() {
return this.replace(/[\s| ]+$/g, "");
};
String.prototype.trim = function() {
return this.trimLeft().trimRight();
};
}
window.onload = function(){
function convert_to_single_line(code, option) {
// the lesson here is to NEVER TARGET IE WITHOUT JQUERY
if (window.is_ie_lt9) {
code = code.replace(/(\r\n|\r|\n)/g, '\n');
}
var ix;
option = option || {};
var lines = code.trimRight() // keep left indent
.replace(/\\/g, '\\\\') // escape all \
.replace(/"""/g, '\\"\\"\\"') // escape """
.split('\n');
var first_indent;
for (ix = 0; ix < lines.length; ++ix) {
if (first_indent === undefined) {
first_indent = lines[ix].match(/^\s*/);
}
if (option.reindent) {
lines[ix] = lines[ix].replace(new RegExp('^'+first_indent), '');
}
}
lines.unshift('"""');
lines.push('"""');
return 'exec'+(option.is_python3 ? '(' : ' ')+lines.join('\\n')+(option.is_python3 ? ')' : '');
}
var do_convert = document.getElementById('convert').onclick = function(e) {
var option = {};
option.is_python3 = document.getElementById('pyver3').checked;
option.reindent = document.getElementById('pyreindent').checked;
var codearea = document.getElementById('codearea');
codearea.value = convert_to_single_line(codearea.value, option);
};
var delayed_convert = function() {
// to wait paste completion
window.setTimeout(do_convert, 5);
};
document.getElementById('pyauto').onclick = function(e) {
var checked = document.getElementById('pyauto').checked;
var codearea = document.getElementById('codearea');
if (checked) {
if (window.is_ie_lt9) {
codearea.attachEvent("onpaste", delayed_convert);
} else {
codearea.addEventListener('paste', delayed_convert, false);
}
} else {
if (window.is_ie_lt9) {
codearea.detachEvent("onpaste", delayed_convert);
} else {
codearea.removeEventListener('paste', delayed_convert);
}
}
}
};
</script>
</head>
<body>
<div id="wrapper">
<h1>Python Single Line Converter</h1>
<p>
Convert multiple line Python code to a single line in a dead simple way.<br/>
Paste your code below and hit Convert
</p>
<div>
<input type="radio" name="pyver" id="pyver2" value="Python 2" checked="checked" /> Python 2
<input type="radio" name="pyver" id="pyver3" value="Python 3" /> Python 3
<input type="checkbox" name="pyreindent" id="pyreindent" checked="checked"> Reindent based on first line's indent
<input type="checkbox" name="pyauto" id="pyauto"> Auto convert on paste
<br/><br/>
<textarea id="codearea" cols="120" rows="30">
# 'Reindent based on first line's indent' will reindent -8 spaces in this case
# comments are still kept at this moment
from datetime import datetime
def foo():
"""triple double quotes are escaped"""
return '"Hey kid what time is it?"'
print foo() + '''\n\t''' \
+ str(datetime.now())
try:
raise RuntimeError()
except:
pass
# ending \ will be removed, but this happens very rare
\</textarea>
<br/><br/>
<button id="convert">Convert</button>
</div>
<p>
<h2>Rationale</h2>
<p>Ease copy pasting Python code in some cases (e.g paste into Python shell or through Putty). If you find yourself need this a lot you may consider using <a href="http://ipython.org/">ipython</a> with the <code>%paste</code> command, or a graphic based one like <a href="http://www.dreampie.org/">dreampie</a></p>
<h2>How it works</h2>
<p>Basically it paste your multiline code together into a triple quoted string and wraps it with <a href="http://docs.python.org/2/reference/simple_stmts.html#the-exec-statement">exec</a>. To keep the code legal the string is processed as follows:</p>
<ol>
<li>Escape all <code>\</code>, then escape <code>"""</code>.</li>
<li>Reindent to 0 indent based on first line if option is selected. So you can paste indented code directly.</li>
<li>Wraps <code>exec</code> statement or function based on version option.</li>
</ol>
<h2>Limitations</h2>
<ol>
<li>Won't remove comments and docstring. Properly handling these needs more work.</li>
<li>Of course can't check the original code is syntatically legal or properly indented.</li>
<li>Only tested by randomly copying code from Python Recipes... So please report missing cases.</li>
</ol>
<a href="https://github.com/jagt/python-single-line-convert"><h2>Repo</h2></a>
<p><a href="mailto:jagttt@gmail.com">jagttt@gmail.com</a></p>
</p>
</div>
</body>
</html>