forked from alexanderjulo/wiki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
490 lines (395 loc) · 13.9 KB
/
app.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
from urlparse import urlparse
import json
import string
from functools import wraps
import os
import re
import markdown
import bcrypt # sudo apt-get install libffi-dev
from flask import (Flask, render_template, flash, redirect, url_for, request, Response,
abort, session)
from flask.ext.wtf import Form
from wtforms import (BooleanField, TextField, TextAreaField)
from wtforms.validators import (InputRequired, ValidationError)
CONF_FILE = 'config.json' # 'content/config.json'
app = Flask(__name__)
def load_config():
config = {}
try:
save_flag = False
with open(CONF_FILE, 'r') as f:
config = json.loads(f.read())
# Verify passwords are hashed, re-save config w/ hashed PWs if not
for user, password in config.get('USERS', {}).iteritems():
user = str(user)
password = str(password)
if password[:7] != "$2a$12$":
save_flag = True
config['USERS'][user] = bcrypt.hashpw(str(password), bcrypt.gensalt())
if user != user.lower(): # Make username lowercase if not already
save_flag = True
config['USERS'][user.lower()] = config['USERS'].pop(user)
secret_key_hex = config.get("SECRET_KEY", "")
secret_key = ""
try:
secret_key = secret_key_hex.decode('hex')
except Exception as e:
print(e)
if len(secret_key) != 24:
save_flag = True
config["SECRET_KEY"] = os.urandom(24).encode('hex')
if save_flag:
print('Updating config file...')
with open(CONF_FILE, 'w') as f:
f.write(json.dumps(config, indent=True, sort_keys=True))
content_dir = config.get('CONTENT_DIR', 'content')
if not os.path.isdir(content_dir):
print("Creating content directory: " + content_dir)
os.mkdir(content_dir)
except Exception as e:
print(str(e) + '\n\n Error loading ' + CONF_FILE + '! \n\n')
return config
app.config.update(load_config())
app.debug = app.config.get('DEBUG', False)
def check_auth(username, password):
try:
user = str(username).lower()
pwd = str(password)
hashed = str(app.config['USERS'].get(user, ''))
if bcrypt.hashpw(pwd, hashed) == hashed:
session['username'] = user
return True
except Exception as e:
print(e)
return False
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if app.config.get('AUTH', True):
username = session.get('username', '')
if not username:
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return Response('Please log in.', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
return f(*args, **kwargs)
return decorated
class Processors(object):
def __init__(self, content=""):
self.content = content
def wikilink(self, html):
"""Processes Wikilink syntax "[[Link]]" within content body. This is
intended to be run after the content has been processed by Markdown.
Args:
html (str): Post-processed HTML output from Markdown
Kwargs:
None
Syntax: This accepts Wikilink syntax in the form of [[WikiLink]] or
[[url/location|LinkName]]. Everything is referenced from the base
location "/", therefore sub-pages need to use the
[[page/subpage|Subpage]].
"""
link = r"((?<!\<code\>)\[\[([^<].+?) \s*([|] \s* (.+?) \s*)?]])"
compLink = re.compile(link, re.X | re.U)
for i in compLink.findall(html):
title = [i[-1] if i[-1] else i[1]][0]
url = self.clean_url(i[1])
formattedLink = u"<a href='{0}'>{1}</a>".format(url_for('display', url=url), title)
html = re.sub(compLink, formattedLink, html, count=1)
return html
def clean_url(self, url):
pageStub = re.sub('[ ]{2,}', ' ', url).strip()
pageStub = pageStub.lower().replace(' ', '_')
pageStub = pageStub.replace('\\\\', '/').replace('\\', '/')
valid_characters = string.ascii_letters + string.digits + '_' + '-' + '/'
pageStub = ''.join(c for c in pageStub if c in valid_characters)
return pageStub
def out(self):
"""Final content output. Processes the Markdown, post-processes, and
Meta data.
"""
md = markdown.Markdown(['codehilite', 'fenced_code', 'meta', 'tables'])
html = md.convert(self.content)
phtml = self.wikilink(html)
body = self.content.split('\n\n', 1)[1]
meta = md.Meta
return phtml, body, meta
class Page(object):
cache = []
@classmethod
def __getCache(cls, cacheid):
""" Return cached object """
for o in Page.cache:
if o.cacheid == cacheid:
return o
return None
def __new__(cls, path, url, *args, **kwargs):
""" Initialize the class and start processing """
if app.config.get('CACHE_PAGES', True):
ctime = kwargs.get('ctime', None)
if path and ctime:
existing = cls.__getCache(path + ctime)
if existing:
return existing
page = super(Page, cls).__new__(cls)
return page
def __init__(self, path, url, *args, **kwargs):
if self in self.cache:
return
self.path = path
self.url = url
self._meta = {}
self._html = ""
self._body = ""
self.content = ""
if not kwargs.get('new', False):
self.load()
self.render()
ctime = kwargs.get('ctime', None)
if app.config.get('CACHE_PAGES', True) and ctime:
self.cacheid = path + ctime
self.cache.append(self)
def load(self):
with open(self.path, 'rU') as f:
self.content = f.read().decode('utf-8')
def render(self):
processed = Processors(self.content)
self._html, self.body, self._meta = processed.out()
def save(self, update=True):
folder = os.path.dirname(self.path)
if not os.path.exists(folder):
os.makedirs(folder)
with open(self.path, 'w') as f:
for key, value in self._meta.items():
line = u'%s: %s\n' % (key, value)
f.write(line.encode('utf-8'))
f.write('\n'.encode('utf-8'))
f.write(self.body.replace('\r\n', '\n').encode('utf-8'))
if update:
self.load()
self.render()
@property
def meta(self):
return self._meta
def __getitem__(self, name):
item = self._meta[name]
if len(item) == 1:
return item[0]
print(item)
return item
def __setitem__(self, name, value):
self._meta[name] = value
@property
def html(self):
return self._html
def __html__(self):
return self.html
@property
def title(self):
try:
return self['title']
except KeyError:
return self.url
@title.setter
def title(self, value):
self['title'] = value
@property
def tags(self):
try:
return self['tags']
except KeyError:
return ""
@tags.setter
def tags(self, value):
self['tags'] = value
class Wiki(object):
def __init__(self, root):
self.root = root
def path(self, url):
return os.path.join(self.root, url + '.md')
def exists(self, url):
path = self.path(url)
return os.path.exists(path)
def get(self, url):
path = os.path.join(self.root, url + '.md')
if self.exists(url):
return Page(path, url)
return None
def get_or_404(self, url):
page = self.get(url)
if page:
return page
abort(404)
def get_bare(self, url):
path = self.path(url)
if self.exists(url):
return False
return Page(path, url, new=True)
def move(self, url, newurl):
os.rename(
os.path.join(self.root, url) + '.md',
os.path.join(self.root, newurl) + '.md'
)
def delete(self, url):
path = self.path(url)
if not self.exists(url):
return False
print(path)
os.remove(path)
return True
def index(self):
def _walk(directory, path_prefix=()):
for name in os.listdir(directory):
fullname = os.path.join(directory, name)
if os.path.isdir(fullname):
_walk(fullname, path_prefix + (name,))
elif name.endswith('.md'):
ctime = str(os.path.getctime(fullname) or 0)
if not path_prefix:
url = name[:-3]
else:
url = os.path.join(os.path.join(*path_prefix), name[:-3])
pages.append(Page(fullname, url.replace('\\', '/'), ctime=ctime))
pages = []
_walk(self.root)
return sorted(pages, key=lambda x: x.title.lower())
def get_tags(self):
pages = self.index()
tags = {}
for page in pages:
pagetags = page.tags.split(',')
for tag in pagetags:
tag = tag.strip()
if tag == '':
continue
elif tags.get(tag):
tags[tag].append(page)
else:
tags[tag] = [page]
return tags
def index_by_tag(self, tag):
pages = self.index()
tagged = []
for page in pages:
if tag in page.tags:
tagged.append(page)
return sorted(tagged, key=lambda x: x.title.lower())
def search(self, term, ignore_case=True, attrs=['title', 'tags', 'body']):
pages = self.index()
regex = re.compile(term, re.IGNORECASE if ignore_case else 0)
matched = []
for page in pages:
for attr in attrs:
if regex.search(getattr(page, attr)):
matched.append(page)
break
return matched
class URLForm(Form):
url = TextField('', [InputRequired()])
def validate_url(form, field):
if wiki.exists(field.data):
raise ValidationError('The URL "%s" exists already.' % field.data)
def clean_url(self, url):
return Processors().clean_url(url)
class SearchForm(Form):
term = TextField('', [InputRequired()])
ignore_case = BooleanField(description='Ignore Case', default=app.config.get('DEFAULT_SEARCH_IGNORE_CASE', True))
class EditorForm(Form):
title = TextField('', [InputRequired()])
body = TextAreaField('', [InputRequired()])
tags = TextField('')
wiki = Wiki(app.config.get('CONTENT_DIR'))
@app.route('/')
@requires_auth
def home():
page = wiki.get('home')
if page:
return display('home')
return render_template('home.html')
@app.route('/index/')
@requires_auth
def index():
pages = wiki.index()
return render_template('index.html', pages=pages)
@app.route('/<path:url>/')
@requires_auth
def display(url):
page = wiki.get_or_404(url)
return render_template('page.html', page=page)
@app.route('/create/', methods=['GET', 'POST'])
@requires_auth
def create():
form = URLForm()
if form.validate_on_submit():
return redirect(url_for('edit', url=form.clean_url(form.url.data)))
return render_template('create.html', form=form)
@app.route('/edit/<path:url>/', methods=['GET', 'POST'])
@requires_auth
def edit(url):
page = wiki.get(url)
form = EditorForm(obj=page)
if not form.title.data:
form.title.data = url
if form.validate_on_submit():
if not page:
page = wiki.get_bare(url)
form.populate_obj(page)
page.save()
flash('"%s" was saved.' % page.title, 'success')
return redirect(url_for('display', url=url))
return render_template('editor.html', form=form, page=page)
@app.route('/preview/', methods=['POST'])
@requires_auth
def preview():
a = request.form
data = {}
processed = Processors(a['body'])
data['html'], data['body'], data['meta'] = processed.out()
return data['html']
@app.route('/move/<path:url>/', methods=['GET', 'POST'])
@requires_auth
def move(url):
page = wiki.get_or_404(url)
form = URLForm(obj=page)
if form.validate_on_submit():
newurl = form.url.data
wiki.move(url, newurl)
return redirect(url_for('.display', url=newurl))
return render_template('move.html', form=form, page=page)
@app.route('/delete/<path:url>/', methods=['POST'])
@requires_auth
def delete(url):
page = wiki.get_or_404(url)
wiki.delete(url)
flash('Page "%s" was deleted.' % page.title, 'success')
return redirect(url_for('home'))
@app.route('/tags/')
@requires_auth
def tags():
tags = wiki.get_tags()
return render_template('tags.html', tags=tags)
@app.route('/tag/<string:name>/')
@requires_auth
def tag(name):
tagged = wiki.index_by_tag(name)
return render_template('tag.html', pages=tagged, tag=name)
@app.route('/search/', methods=['GET', 'POST'])
@requires_auth
def search():
form = SearchForm()
if form.validate_on_submit():
results = wiki.search(form.term.data, form.ignore_case.data)
return render_template('search.html', form=form,
results=results, search=form.term.data)
return render_template('search.html', form=form, search=None)
@app.route('/logout/')
def logout():
o = urlparse(request.url)
url = o.scheme + '://foo:bar@' + o.netloc + url_for('home')
session.pop('username', None)
return redirect(url)
@app.errorhandler(404)
def page_not_found(e):
print(e)
return render_template('404.html'), 404
if __name__ == '__main__':
app.run()