forked from orangeduck/BuildYourOwnLisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lispy.py
119 lines (93 loc) · 4.46 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
import os
from werkzeug.contrib.cache import MemcachedCache
from flask import Flask
from flask.ext.basicauth import BasicAuth
pages = [
'splash.html', 'contents.html', 'credits.html', 'faq.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'
]
titles = [
'', 'Contents', 'Credits', 'Frequently Asked Questions',
'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'
]
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>
</head>
<body style="background: url(static/img/tiletop.png) repeat-x;">
<div class='container' style='width:750px; margin-top:50px;'>
"""
footer = """
</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>
</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__)
#app.config['BASIC_AUTH_USERNAME'] = 'byol'
#app.config['BASIC_AUTH_PASSWORD'] = 'lovelace'
#app.config['BASIC_AUTH_FORCE'] = True
#app_auth = BasicAuth(app)
@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)
title = titles[pages.index(page)]
contents = cache.get("lispy-" + path)
if contents is None:
contents = open(path, 'r').read()
contents = (header % title) + contents + footer
cache.set("lispy-" + path, contents, timeout=5*60)
return contents
@app.route('/')
def route_index():
return route_page('splash')
if __name__ == '__main__':
app.run()