forked from ronreiter/interactive-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
435 lines (331 loc) · 14.1 KB
/
main.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# -*- coding: utf-8 -*-
import json
import re
import markdown
import os
import cgi
import urllib.request, urllib.parse, urllib.error
import time
import functools
import logging
import binascii
import datetime
from flask import Flask, render_template, request, make_response, session, Response
from ideone import Ideone
import constants
courses = json.load(open("courses.json"))
# Flask app
app = Flask(__name__)
app.secret_key = constants.SECRET_KEY
sections = re.compile(r"Tutorial\n[=\-]+\n+(.*)\n*Tutorial Code\n[=\-]+\n+(.*)\n*Expected Output\n[=\-]+\n+(.*)\n*Solution\n[=\-]+\n*(.*)\n*", re.MULTILINE | re.DOTALL)
WIKI_WORD_PATTERN = re.compile('\[\[([^]|]+\|)?([^]]+)\]\]')
current_domain = constants.LEARNPYTHON_DOMAIN # python
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--domain",
help="Default domain when running in development mode",
default=current_domain,
required=True,
choices=list(constants.DOMAIN_DATA.keys())
)
parser.add_argument("-p", "--port", help="port to listen to", default=5000, type=int)
parser.add_argument("-H", "--host", help="host", default='127.0.0.1')
args = parser.parse_args()
current_domain = args.domain
LANGUAGES = {
"en": "English",
"pl": "Polski",
"fa": "فارسی",
"es": "Español",
"it": "Italiano",
"de": "Deutsch",
"cn": "普通话",
"fr": "Français",
"pt": "Português",
"tr": "Türkçe",
}
tutorial_data = {}
def run_code(code, language_id):
ideone_api = Ideone(
constants.IDEONE_USERNAME,
constants.IDEONE_PASSWORD,
api_url='http://ronreiter.compilers.sphere-engine.com/api/1/service.wsdl')
code = ideone_api.create_submission(code, language_id=language_id, std_input="")["link"]
result = None
while True:
time.sleep(1)
result = ideone_api.submission_details(code)
if result["status"] in [1,3]:
continue
break
data = { "code" : code }
if result["stderr"] or result["cmpinfo"]:
data["output"] = "exception"
if result["cmpinfo"]:
data["text"] = result["cmpinfo"]
elif result["stderr"]:
data["text"] = result["stderr"]
else:
data["output"] = "text"
data["text"] = result["output"]
return data
def pageurl(value, language):
if value.startswith("http"):
return value
else:
return urllib.parse.quote("/%s/%s" % (language, value.replace(' ', '_')))
def _wikify_one(language, pat):
"""
Wikifies one link.
"""
page_title = pat.group(2)
if pat.group(1):
page_name = pat.group(1).rstrip('|')
else:
page_name = page_title
# interwiki
if ':' in page_name and not page_name.startswith("http"):
parts = page_name.split(':', 2)
if page_name == page_title:
page_title = parts[1]
link = "<a href='%s'>%s</a>" % (pageurl(page_name, language), page_title)
return link
def wikify(text, language):
text, count = WIKI_WORD_PATTERN.subn(functools.partial(_wikify_one, language), text)
return markdown.markdown(text).strip()
def untab(text):
lines = text.strip("\n").split("\n")
if not all([x.startswith(" ") or x == "" for x in lines]):
return "\n".join(lines)
return "\n".join([x[4:] if len(x) >= 4 else "" for x in lines])
def init_tutorials():
contributing_tutorials = wikify(open(os.path.join(os.path.dirname(__file__), "tutorials", "Contributing Tutorials.md")).read(), "en")
for domain in os.listdir(os.path.join(os.path.dirname(__file__), "tutorials")):
if domain.endswith(".md"):
continue
logging.info("loading data for domain: %s", domain)
tutorial_data[domain] = {}
if not os.path.isdir(os.path.join(os.path.dirname(__file__), "tutorials", domain)):
continue
if domain not in constants.DOMAIN_DATA:
logging.warning("skipping domain %s beacause no domain data exists" % domain)
continue
for language in os.listdir(os.path.join(os.path.dirname(__file__), "tutorials", domain)):
tutorial_data[domain][language] = {}
tutorials_path = os.path.join(os.path.dirname(__file__), "tutorials", domain, language)
if not os.path.isdir(tutorials_path):
continue
tutorials = os.listdir(tutorials_path)
# place the index file first
tutorials.remove("Welcome.md")
tutorials = ["Welcome.md"] + tutorials
for tutorial_file in tutorials:
if not tutorial_file.endswith(".md"):
continue
tutorial = tutorial_file[:-3]
logging.debug("loading tutorial %s" % tutorial)
if not tutorial in tutorial_data[domain][language]:
tutorial_data[domain][language][tutorial] = {}
tutorial_dict = tutorial_data[domain][language][tutorial]
tutorial_path = os.path.join(os.path.dirname(__file__), "tutorials", domain, language, tutorial_file)
tutorial_dict["text"] = open(tutorial_path).read().replace("\r\n", "\n")
# create links by looking at all lines that are not code lines
stripped_text = "\n".join([x for x in tutorial_dict["text"].split("\n") if not x.startswith(" ")])
links = [x[0].strip("|") if x[0] else x[1] for x in WIKI_WORD_PATTERN.findall(stripped_text)]
tutorial_dict["links"] = [(x, pageurl(x, language)) for x in links]
tutorial_sections = sections.findall(tutorial_dict["text"])
if tutorial_sections:
text, code, output, solution = tutorial_sections[0]
tutorial_dict["page_title"] = tutorial
tutorial_dict["text"] = wikify(text, language)
tutorial_dict["code"] = untab(code)
tutorial_dict["output"] = untab(output)
tutorial_dict["solution"] = untab(solution)
tutorial_dict["is_tutorial"] = True
else:
if tutorial_file != "Welcome.md":
logging.warning("File %s/%s/%s is not a tutorial", domain, language, tutorial_file)
tutorial_dict["page_title"] = ""
tutorial_dict["text"] = wikify(tutorial_dict["text"], language)
tutorial_dict["code"] = constants.DOMAIN_DATA[domain]["default_code"]
tutorial_dict["is_tutorial"] = False
for link in links:
if not link in tutorial_data[domain][language]:
tutorial_data[domain][language][link] = {
"page_title" : link,
"text": contributing_tutorials,
"code": ""
}
if not "back_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["back_chapter"] = tutorial.replace(" ", "_")
elif not link.startswith("http"):
logging.info("Warning! duplicate links to tutorial %s from tutorial %s/%s", link, language, tutorial)
num_links = len(links)
page_index = links.index(link)
if page_index > 0:
if not "previous_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["previous_chapter"] = links[page_index - 1].replace(" ", "_")
if page_index < (num_links - 1):
if not "next_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["next_chapter"] = links[page_index + 1].replace(" ", "_")
init_tutorials()
def get_languages():
return sorted(tutorial_data[get_host()].keys())
def get_language_names():
arr = []
langs = get_languages()
for lang in langs:
arr.append(LANGUAGES.get(lang))
return arr
def get_host():
if is_development_mode():
return current_domain
return request.host[4:] if request.host.startswith("www.") else request.host
def is_development_mode():
return "localhost" in request.host or "127.0.0.1" in request.host
def get_domain_data():
data = constants.DOMAIN_DATA[get_host()]
data["courses"] = courses.get(get_host())
return data
def get_tutorial_data(tutorial_id, language="en"):
return tutorial_data[get_host()][language][tutorial_id]
def get_tutorial(tutorial_id, language="en"):
td = get_tutorial_data(tutorial_id, language)
if not td:
return {
"page_title": cgi.escape(tutorial_id),
"text": "Page not found."
}
else:
return td
def error404():
domain_data = get_domain_data()
return make_response(render_template(
"error404.html",
domain_data=domain_data,
all_data=constants.DOMAIN_DATA,
language_code="en",
languages=get_languages(),
), 404)
@app.route("/favicon.ico")
def favicon():
return open(os.path.join(os.path.dirname(__file__), get_domain_data()["favicon"][1:]), "rb").read()
@app.route("/ads.txt")
def ads():
return Response(render_template("ads.txt"), mimetype='text/plain')
@app.route("/", methods=["GET", "POST"])
@app.route("/<language>/", methods=["GET", "POST"])
def default_index(language="en"):
return index("Welcome", language)
@app.route("/about")
@app.route("/privacy")
@app.route("/tos")
def static_file():
return make_response(render_template(
request.path.strip("/") + ".html",
domain_data=get_domain_data(),
all_data=constants.DOMAIN_DATA,
domain_data_json=json.dumps(get_domain_data()),
language_code="en",
))
@app.route("/signin")
def signin():
email = request.args.get("email", None)
password = request.args.get("password", None)
user = users.findOne({"email": email})
if user:
session["user_id"] = str(user._id)
return make_response(json.dumps({"status": "error", "error": "no_user"}))
@app.route("/signup")
def signup():
email = request.args.get("email", None)
password = request.args.get("password", None)
confirm = request.args.get("confirm", None)
if not email or not password or not confirm:
return make_response(json.dumps({"status": "error", "error": "missing_field"}))
if not re.findall("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", email):
return make_response(json.dumps({"status": "error", "error": "invalid_email"}))
if password != confirm:
return make_response(json.dumps({"status": "error", "error": "passwords_dont_match"}))
id = users.insert({
"email": email,
"password": password,
})
session["user_id"] = str(id)
@app.route("/<language>/progress")
def progress(language):
return make_response(render_template(
"progress.html",
domain_data=get_domain_data(),
all_data=constants.DOMAIN_DATA,
))
@app.route("/<title>", methods=["GET", "POST"])
@app.route("/<language>/<title>", methods=["GET", "POST"])
def index(title, language="en"):
tutorial = title.replace("_", " ")
try:
current_tutorial_data = get_tutorial(tutorial, language)
except KeyError:
return error404()
domain_data = get_domain_data()
domain_data["language_code"] = language
if request.method == "GET":
title_suffix = "Learn %s - Free Interactive %s Tutorial" % (domain_data["language_uppercase"], domain_data["language_uppercase"])
html_title = "%s - %s" % (title.replace("_", " "), title_suffix) if title != "Welcome" else title_suffix
if not "uid" in session:
session["uid"] = binascii.b2a_hex(os.urandom(16))
uid = session["uid"]
try:
site_links = tutorial_data[get_host()][language]["Welcome"]["links"]
except Exception as e:
site_links = []
logging.error("cant get site links for %s %s" % (get_host(), language))
return make_response(render_template(
"index-python.html" if (language == "en" and domain_data["language"] == "python") else "index.html",
tutorial_page=tutorial != "Welcome",
domain_data=domain_data,
all_data=constants.DOMAIN_DATA,
site_tutorial_links=site_links,
tutorial_data=current_tutorial_data,
tutorial_data_json=json.dumps(current_tutorial_data),
domain_data_json=json.dumps(domain_data),
html_title=html_title,
language_code=language,
languages=get_languages(),
language_names=get_language_names(),
uid=uid,
**current_tutorial_data
))
# POST method handling
data = run_code(request.json["code"], domain_data["language_id"])
if "output" in current_tutorial_data and current_tutorial_data["output"] == data["output"]:
data["solved"] = True
else:
data["solved"] = False
return make_response(json.dumps(data))
@app.route("/sitemap.xml")
def sitemap_index():
languages = tutorial_data[get_host()].keys()
response = make_response(render_template("sitemap_index.xml", languages=languages))
response.headers['Content-Type'] = 'application/xml'
return response
@app.route("/sitemap_<language>.xml")
def sitemap(language):
try:
titles = tutorial_data[get_host()][language].keys()
except KeyError:
return error404()
# use today as lastmod to make sure the most recent version is always indexed
lastmod = datetime.datetime.utcnow().date()
response = make_response(render_template("sitemap.xml", titles=titles, lastmod=lastmod, language=language))
response.headers['Content-Type'] = 'application/xml'
return response
@app.route("/robots.txt")
def robots():
return make_response("User-agent: *\nAllow: /")
if __name__ == "__main__":
logging.info("listening on port %s", args.port)
app.run(debug=True, port=args.port, host=args.host)