forked from tkisason/localwiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
executable file
·79 lines (63 loc) · 1.91 KB
/
wiki.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
#!/usr/bin/python
from bottle import route, run, get, post, request, redirect
import shelve
import string
import re
#spoljos frankenstein modulez
import templates
def wikify(data):
out = ""
keys = dict(zip(map(lambda x: string.lower(x), db.keys()), db.keys()))
for elem in data.split(" "):
le = string.lower(elem)
if le in keys.keys():
out += '<a href="/'+keys[le]+'">'+elem+'</a>'+" "
else:
out += r.sub(r'<a href="\1">\1</a>', elem)+" "
# out += elem + " "
return out
def markup(data):
data = style+wikify(data)
return str(data.replace("\n", "<br>"))
@route("/")
def print_pages():
html = ""
for elem in db:
html += markup(elem) + "<br>"
else:
html += templates.newentry
return html
@get("/:name")
def get_page(name):
if name in db:
return markup(db[name]) + templates.editme + templates.delme
else:
return redirect("/%s/edit"%name)
@get("/:name/:command")
def node(name, command):
if name in db:
cont = db[name]
else:
cont = ""
if command == "edit":
return templates.content_form.format(**{"content":cont})
elif command == "del":
db.pop(name)
return str("Deleted: "+name)+templates.rootredirjs
else:
return "Unknown command: ", command
@post('/:name/:command')
def node_submit(name, command):
cont = request.forms.get('content')
db[name] = cont
return redirect("/"+name)
if __name__ == "__main__":
db = shelve.open("database", writeback=True)
style = "" # this element will be added in every view. Add your custom
# javascript/css here
r = re.compile(r"(http://[^ \n]+)")#dont compile regex on every request
#do it once on startup
run(host='127.0.0.1', port=8000)
print "[+] Flushing data to db..."
db.close()
print "[+] Done, bye"