-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathlispy.py
251 lines (199 loc) · 8.82 KB
/
lispy.py
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
import os
import logging
import random
import datetime
from cachelib import MemcachedCache
from werkzeug.datastructures import ImmutableOrderedMultiDict
from flask import Flask, jsonify, request, send_file, redirect, url_for
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
pages = [
'splash.html', 'contents.html', 'credits.html', 'faq.html', '404.html', 'purchase.html', 'test.html', 'invalid.html',
'download.html',
'chapter1_introduction.html', 'chapter2_installation.html', 'chapter3_basics.html',
'chapter4_interactive_prompt.html', 'chapter5_languages.html', 'chapter6_parsing.html',
'chapter7_evaluation.html', 'chapter8_error_handling.html', 'chapter9_s_expressions.html',
'chapter10_q_expressions.html', 'chapter11_variables.html', 'chapter12_functions.html',
'chapter13_conditionals.html', 'chapter14_strings.html', 'chapter15_standard_library.html',
'chapter16_bonus_projects.html',
'appendix_a_hand_rolled_parser.html',
]
titles = [
'Learn C', 'Contents', 'Credits', 'Frequently Asked Questions', 'Page Missing', 'Buy Now!', 'Test', 'Invalid Download',
'Download',
'Introduction • Chapter 1', 'Installation • Chapter 2', 'Basics • Chapter 3',
'Interactive Prompt • Chapter 4', 'languages • Chapter 5', 'Parsing • Chapter 6',
'Evaluation • Chapter 7', 'Error Handling • Chapter 8', 'S-Expressions • Chapter 9',
'Q-Expressions • Chapter 10', 'Variables • Chapter 11', 'Functions • Chapter 12',
'Conditionals • Chapter 13', 'Strings • Chapter 14', 'Standard Library • Chapter 15',
'Bonus Projects • Chapter 16',
'Hand Rolled Parser • Appendix A'
]
sources = [
(), (), (), (), (), (), (), (),
(),
(), ('hello_world.c', ), (),
('prompt_unix.c', 'prompt_windows.c', 'prompt.c'), ('doge_code.c', 'doge_grammar.c'),
('parsing.c', ), ('evaluation.c', ), ('error_handling.c',), ('s_expressions.c',), ('q_expressions.c',),
('variables.c',), ('functions.c',), ('conditionals.c',), ('strings.c',), ('prelude.lspy',), (),
('hand_rolled_parser.c',)
]
header = """
<!DOCTYPE html>
<html>
<head>
<title>%s • Build Your Own Lisp</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="static/css/bootstrap.css" rel="stylesheet">
<link href="static/css/code.css" rel="stylesheet">
<link rel="icon" type="image/png" href="/static/img/favicon.png" />
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46885107-1', 'buildyourownlisp.com');
ga('send', 'pageview');
</script>
<style>
@media (max-width: 640px) {
h4 { font-size: 90%%; }
}
</style>
</head>
<body style="background: url(static/img/halftone.png); margin:0px; padding:0px;">
<div style="background: url(static/img/tiletop.png) repeat-x; height:25px;">
<div class='container' style='max-width:750px; padding-top:10px;'>
<div class='row'>
<div class='col-xs-12'>
"""
footer = """
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="static/js/bootstrap.js"></script>
<!-- Syntax Highlighting -->
<script src="static/js/rainbow.js"></script>
<script src="static/js/language/generic.js"></script>
<script src="static/js/language/c.js"></script>
<script src="static/js/language/lispy.js"></script>
</div>
</body>
</html>
"""
try:
cache = MemcachedCache(['127.0.0.1:11211'])
except RuntimeError:
class FakeCache:
def get(self, k): return None
def set(self, k, v, **kwargs): return None
cache = FakeCache()
app = Flask(__name__)
handler = logging.FileHandler(os.path.join(os.path.split(__file__)[0], 'error.log'))
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
#from flask.ext.basicauth import BasicAuth
#app.config['BASIC_AUTH_USERNAME'] = 'byol'
#app.config['BASIC_AUTH_PASSWORD'] = 'lovelace'
#app.config['BASIC_AUTH_FORCE'] = True
#app_auth = BasicAuth(app)
""" Page """
code_header = """
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse%s">
%s
</a>
</h4>
</div>
<div id="collapse%s" class="panel-collapse collapse">
<div class="panel-body">
"""
code_footer = """
</div>
</div>
</div>
"""
def code_html(codes):
string = ('<div class="panel-group alert alert-warning" id="accordion">\n'
' <div class="panel panel-default">')
for num, code in zip(('One', 'Two', 'Three', 'Four', 'Five'), codes):
string += code_header % (num, code, num)
if code.endswith('.lspy'):
string += '<pre><code data-language=\'lispy\'>'
else:
string += '<pre><code data-language=\'c\'>'
path = os.path.join(os.path.split(__file__)[0], 'src', code)
with open(path, 'r') as f:
contents = f.read()
contents = contents.replace('&', '&')
contents = contents.replace('<', '<')
contents = contents.replace('>', '>')
string += contents
string = string.rstrip() + '</code></pre>'
string += code_footer + '\n\n'
string += '</div>\n </div>'
return string
@app.route('/<page>')
def route_page(page):
page = page + '.html'
if not page in pages: page = '404.html'
path = os.path.join(os.path.split(__file__)[0], page)
index = pages.index(page)
title = titles[index]
codes = sources[index]
contents = cache.get("lispy-" + path)
if contents is None:
contents = open(path, 'r').read()
contents = (header % title) + contents.replace('<references />', code_html(codes)) + footer
cache.set("lispy-" + path, contents, timeout=5*60)
return contents
@app.route('/')
def route_index():
return route_page('splash')
@app.errorhandler(404)
def route_404(e):
return redirect(url_for('route_page', page='404'))
@app.route('/download/<id>/<type>')
@app.route('/download/<id>/BuildYourOwnLisp.<type>')
def route_download(id, type):
keys = os.path.join(os.path.split(__file__)[0], 'purchases')
with open(keys, 'r') as keyfile:
keys = map(lambda x: x.strip(), keyfile.readlines())
keys = map(lambda x: x.split(' '), keys)
keys = set([key[1] for key in keys if
(datetime.datetime.now() -
datetime.datetime.strptime(key[0], '%Y-%m-%d-%H:%M:%S'))
< datetime.timedelta(days=60)])
if id in keys:
if type == 'epub': return send_file('BuildYourOwnLisp.epub', mimetype='application/epub+zip')
elif type == 'mobi': return send_file('BuildYourOwnLisp.mobi', mimetype='application/x-mobipocket-ebook')
elif type == 'pdf': return send_file('BuildYourOwnLisp.pdf', mimetype='application/pdf')
elif type == 'tar': return send_file('BuildYourOwnLisp.tar.gz', mimetype='application/x-gtar')
else: return redirect(url_for('route_page', page='invalid'))
else: return redirect(url_for('route_page', page='invalid'))
""" Paypal Stuff """
def ordered_storage(f):
def decorator(*args, **kwargs):
request.parameter_storage_class = ImmutableOrderedMultiDict
return f(*args, **kwargs)
return decorator
""" Main """
if __name__ == '__main__':
port = int(os.getenv('PORT') or 5000)
app.run(port=port, debug=True)