-
Notifications
You must be signed in to change notification settings - Fork 197
/
main.py
637 lines (523 loc) · 22.2 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# -*- coding: utf-8 -*-
# This Blueprint is about adding a Knowledge Base to the LMFDB website.
# referencing content, dynamically inserting information into the website, …
#
# This is more than just a web of entries in a wiki, because content is "transcluded".
# Transclusion is an actual concept, you can read about it here:
# http://en.wikipedia.org/wiki/Transclusion
#
# a "Knowl" (see knowl.py) is our base class for any bit of "knowledge". we might
# subclass it into "theorem", "proof", "description", and much more if necessary
# (i.e. when it makes sense to add additional fields, e.g. for referencing each other)
#
# author: Harald Schilly <harald.schilly@univie.ac.at>
import string
import re
import pymongo
import flask
from lmfdb.base import app
from datetime import datetime
from flask import render_template, render_template_string, request, url_for, make_response
from flask.ext.login import login_required, current_user
from knowl import Knowl, knowl_title, get_history, knowl_exists
from lmfdb.users import admin_required, housekeeping
import markdown
from lmfdb.knowledge import logger
ASC = pymongo.ASCENDING
DSC = pymongo.DESCENDING
# just for those, who still use an older markdown
try:
markdown.util.etree
except:
logger.fatal("You need to update the markdown python utility: sage -sh -> easy_install -U markdown flask-markdown")
exit()
# know IDs are restricted by this regex
allowed_knowl_id = re.compile("^[a-z0-9._-]+$")
# Tell markdown to not escape or format inside a given block
class IgnorePattern(markdown.inlinepatterns.Pattern):
def handleMatch(self, m):
return m.group(2)
class HashTagPattern(markdown.inlinepatterns.Pattern):
def handleMatch(self, m):
el = markdown.util.etree.Element("a")
el.set('href', url_for('knowledge.index') + '?search=%23' + m.group(2))
el.text = '#' + m.group(2)
return el
class KnowlTagPatternWithTitle(markdown.inlinepatterns.Pattern):
def handleMatch(self, m):
tokens = m.group(2).split("|")
kid = tokens[0].strip()
if len(tokens) > 1:
tit = ''.join(tokens[1:])
return "{{ KNOWL('%s', title='%s') }}" % (kid, tit.strip())
return "{{ KNOWL('%s') }}" % kid
# Initialise the markdown converter, sending a wikilink [[topic]] to the L-functions wiki
md = markdown.Markdown(extensions=['markdown.extensions.wikilinks'],
extension_configs={'wikilinks': [('base_url', 'http://wiki.l-functions.org/')]})
# Prevent $..$, $$..$$, \(..\), \[..\] blocks from being processed by Markdown
md.inlinePatterns.add('mathjax$', IgnorePattern(r'(?<![\\\$])(\$[^\$].*?\$)'), '<escape')
md.inlinePatterns.add('mathjax$$', IgnorePattern(r'(?<![\\])(\$\$.+?\$\$)'), '<escape')
md.inlinePatterns.add('mathjax\\(', IgnorePattern(r'(\\\(.+?\\\))'), '<escape')
md.inlinePatterns.add('mathjax\\[', IgnorePattern(r'(\\\[.+?\\\])'), '<escape')
# Tell markdown to turn hashtags into search urls
hashtag_keywords_rex = r'#([a-zA-Z][a-zA-Z0-9-_]{1,})\b'
md.inlinePatterns.add('hashtag', HashTagPattern(hashtag_keywords_rex), '<escape')
# Tells markdown to process "wikistyle" knowls with optional title
# should cover [[[ KID ]]] and [[[ KID | title ]]]
knowltagtitle_regex = r'\[\[\[[ ]*([^\]]+)[ ]*\]\]\]'
md.inlinePatterns.add('knowltagtitle', KnowlTagPatternWithTitle(knowltagtitle_regex), '<escape')
# global (application wide) insertion of the variable "Knowl" to create
# lightweight Knowl objects inside the templates.
def first_bracketed_string(text, depth=0, lbrack="{", rbrack="}"):
"""If text is of the form {A}B, return {A},B.
Otherwise, return "",text.
"""
thetext = text.strip()
if not thetext:
logger.error("empty string sent to first_bracketed_string()")
return ""
previouschar = ""
# we need to keep track of the previous character becaause \{ does not
# count as a bracket
if depth == 0 and thetext[0] != lbrack:
return "",thetext
elif depth == 0:
firstpart = lbrack
depth = 1
thetext = thetext[1:]
else:
firstpart = "" # should be some number of brackets?
while depth > 0 and thetext:
currentchar = thetext[0]
if currentchar == lbrack and previouschar != "\\":
depth += 1
elif currentchar == rbrack and previouschar != "\\":
depth -= 1
firstpart += currentchar
if previouschar == "\\" and currentchar == "\\":
previouschar = "\n"
else:
previouschar = currentchar
thetext = thetext[1:]
if depth == 0:
return firstpart, thetext
else:
logger.error("no matching bracket %s in %s XX", lbrack, thetext)
return "",firstpart[1:] # firstpart should be everything
# but take away the bracket that doesn't match
def ref_to_link(txt):
""" Convert citations to links
In a future version the bibliographic entry will be downloaded and saved.
"""
text = txt.group(1) # because it was a match in a regular expression
thecite, everythingelse = first_bracketed_string(text)
thecite = thecite[1:-1] # strip curly brackets
thecite = thecite.replace("\\","") # \href --> href
refs = thecite.split(",")
ans = ""
# print "refs",refs
for ref in refs:
ref = ref.strip() # because \cite{A, B, C,D} can have spaces
this_link = ""
if ref.startswith("href"):
the_link = re.sub(".*{([^}]+)}{.*", r"\1", ref)
click_on = re.sub(".*}{([^}]+)}\s*", r"\1", ref)
this_link = '{{ LINK_EXT("' + click_on + '","' + the_link + '") | safe}}'
elif ref.startswith("doi"):
ref = ref.replace(":","") # could be doi:: or doi: or doi
the_doi = ref[3:] # remove the "doi"
this_link = '{{ LINK_EXT("' + the_doi + '","http://dx.doi.org/' + the_doi + '")| safe }}'
elif ref.lower().startswith("mr"):
ref = ref.replace(":","")
the_mr = ref[2:] # remove the "MR"
this_link = '{{ LINK_EXT("' + 'MR:' + the_mr + '", '
this_link += '"http://www.ams.org/mathscinet/search/publdoc.html?pg1=MR&s1='
this_link += the_mr + '") | safe}}'
elif ref.lower().startswith("arxiv"):
ref = ref.replace(":","")
the_arx = ref[5:] # remove the "arXiv"
this_link = '{{ LINK_EXT("' + 'arXiv:' + the_arx + '", '
this_link += '"http://arxiv.org/abs/'
this_link += the_arx + '")| safe}}'
if this_link:
if ans:
ans += ", "
ans += this_link
return '[' + ans + ']' + " " + everythingelse
def md_latex_accents(text):
"""
Convert \"o to ö and similar TeX-style markup.
"""
knowl_content = text
knowl_content = re.sub(r'\\"([a-zA-Z])',r"&\1uml;",knowl_content)
knowl_content = re.sub(r'\\"{([a-zA-Z])}',r"&\1uml;",knowl_content)
knowl_content = re.sub(r"\\'([a-zA-Z])",r"&\1acute;",knowl_content)
knowl_content = re.sub(r"\\'{([a-zA-Z])}",r"&\1acute;",knowl_content)
knowl_content = re.sub(r"\\`([a-zA-Z])",r"&\1grave;",knowl_content)
knowl_content = re.sub(r"\\`{([a-zA-Z])}",r"&\1grave;",knowl_content)
knowl_content = re.sub(r"``(?P<a>[\S\s]*?)''", r"“\1”", knowl_content)
return knowl_content
def md_preprocess(text):
"""
Markdown preprocessor: html paragraph breaks before display math,
\cite{MR:...} and \cite{arXiv:...} converted to links.
"""
knowl_content = text
# put a blank line above display equations so that knowls open in the correct location
knowl_content = re.sub(r"([^\n])\n\\begin{eq",r"\1\n\n\\begin{eq",knowl_content)
while "\\cite{" in knowl_content:
knowl_content = re.sub(r"\\cite({.*)",ref_to_link,knowl_content,0,re.DOTALL)
knowl_content = md_latex_accents(knowl_content)
return knowl_content
@app.context_processor
def ctx_knowledge():
return {'Knowl': Knowl, 'knowl_title': knowl_title, "KNOWL_EXISTS": knowl_exists}
@app.template_filter("render_knowl")
def render_knowl_in_template(knowl_content, **kwargs):
"""
This function does the actual rendering, for render and the template_filter
render_knowl_in_template (ultimately for KNOWL_INC)
"""
render_me = u"""\
{%% include "knowl-defs.html" %%}
{%% from "knowl-defs.html" import KNOWL with context %%}
{%% from "knowl-defs.html" import KNOWL_LINK with context %%}
{%% from "knowl-defs.html" import KNOWL_INC with context %%}
{%% from "knowl-defs.html" import TEXT_DATA with context %%}
%(content)s
"""
knowl_content = md_preprocess(knowl_content)
# markdown enabled
render_me = render_me % {'content': md.convert(knowl_content)}
# Pass the text on to markdown. Note, backslashes need to be escaped for
# this, but not for the javascript markdown parser
try:
return render_template_string(render_me, **kwargs)
except Exception, e:
return "ERROR in the template: %s. Please edit it to resolve the problem." % e
# a jinja test for figuring out if this is a knowl or not
# usage: {% if K is knowl_type %} ... {% endif %}
def test_knowl_type(k):
return isinstance(k, Knowl)
app.jinja_env.tests['knowl_type'] = test_knowl_type
from lmfdb.knowledge import knowledge_page
# blueprint specific definition of the body_class variable
@knowledge_page.context_processor
def body_class():
return {'body_class': 'knowl'}
def get_bread(breads=[]):
bc = [("Knowledge", url_for(".index"))]
for b in breads:
bc.append(b)
return bc
def searchbox(q="", clear=False):
"""returns the searchbox"""
searchbox = u"""\
<form id='knowl-search' action="%s" method="GET">
<input name="search" value="%s" />"""
if clear:
searchbox += '<a href="%s">clear</a>' % url_for(".index")
searchbox += '<button type="submit">Go</button>'
searchbox += "</form>"
return searchbox % (url_for(".index"), q)
@knowledge_page.route("/test")
def test():
"""
just a test page
"""
logger.info("test")
return render_template("knowl-test.html",
bread=get_bread([("Test", url_for(".test"))]),
title="Knowledge Test",
k1=Knowl("k1"))
@knowledge_page.route("/edit/<ID>")
@login_required
def edit(ID):
from pymongo.errors import OperationFailure
if not allowed_knowl_id.match(ID):
flask.flash("""Oops, knowl id '%s' is not allowed.
It must consist of lowercase characters,
no spaces, numbers or '.', '_' and '-'.""" % ID, "error")
return flask.redirect(url_for(".index"))
knowl = Knowl(ID)
from knowl import is_locked, set_locked
lock = False
if request.args.get("lock", "") != 'ignore':
try:
lock = is_locked(knowl.id)
except OperationFailure as e:
logger.info("Oops, failed to get the lock. Error: %s" %e)
pass;
# lock, if either lock is false or (lock is active), current user is editing again
author_edits = lock and lock['who'] == current_user.get_id()
logger.debug(author_edits)
if not lock or author_edits:
try:
set_locked(knowl, current_user.get_id())
except OperationFailure as e:
logger.info("Oops, failed to set the lock. Error: %s" %e)
pass;
if author_edits:
lock = False
b = get_bread([("Edit '%s'" % ID, url_for('.edit', ID=ID))])
return render_template("knowl-edit.html",
title="Edit Knowl '%s'" % ID,
k=knowl,
bread=b,
lock=lock)
@knowledge_page.route("/show/<ID>")
def show(ID):
k = Knowl(ID)
r = render(ID, footer="0", raw=True)
title = k.title or "'%s'" % k.id
b = get_bread([('%s' % title, url_for('.show', ID=ID))])
return render_template("knowl-show.html",
title=k.title,
k=k,
render=r,
bread=b)
@knowledge_page.route("/raw/<ID>")
def raw(ID):
data = render(ID, footer="0", raw=True)
resp = make_response(data)
# cache 2 minutes and allow CORS
resp.headers['Cache-Control'] = 'max-age=%s, public' % (2 * 60)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@knowledge_page.route("/history")
def history():
h_items = get_history()
bread = get_bread([("History", url_for('.history'))])
return render_template("knowl-history.html",
title="Knowledge History",
bread=bread,
history=h_items)
@knowledge_page.route("/delete/<ID>")
@admin_required
def delete(ID):
k = Knowl(ID)
k.delete()
flask.flash("Snif! Knowl %s deleted and gone forever :-(" % ID)
return flask.redirect(url_for(".index"))
@knowledge_page.route("/edit", methods=["POST"])
@login_required
def edit_form():
ID = request.form['id']
return flask.redirect(url_for(".edit", ID=ID))
@knowledge_page.route("/save", methods=["POST"])
@login_required
def save_form():
ID = request.form['id']
if not ID:
raise Exception("no id")
if not allowed_knowl_id.match(ID):
flask.flash("""Oops, knowl id '%s' is not allowed.
It must consist of lower/uppercase characters,
no spaces, numbers or '.', '_' and '-'.""" % ID, "error")
return flask.redirect(url_for(".index"))
k = Knowl(ID)
k.title = request.form['title']
k.content = request.form['content']
k.quality = request.form['quality']
k.timestamp = datetime.now()
k.save(who=current_user.get_id())
from knowl import save_history
save_history(k, current_user.get_id())
return flask.redirect(url_for(".show", ID=ID))
@knowledge_page.route("/render/<ID>", methods=["GET", "POST"])
def render(ID, footer=None, kwargs=None, raw=False):
"""
this method renders the given Knowl (ID) to insert it
dynamically in a website. It is intended to be used
by an AJAX call, but should do a similar job server-side
only, too.
Note, that the used knowl-render.html template is *not*
based on any globally defined website and just creates
a small and simple html snippet!
the keyword 'raw' is used in knowledge.show and knowl_inc to
include *just* the string and not the response object.
"""
try:
k = Knowl(ID)
except:
logger.critical("Failed to render knowl %s"%ID)
errmsg = "Sorry, the knowledge database is currently unavailable."
return errmsg if raw else make_response(errmsg)
# logger.debug("kwargs: %s", request.args)
kwargs = kwargs or dict(((k, v) for k, v in request.args.iteritems()))
# logger.debug("kwargs: %s" , kwargs)
# kw_params is inserted *verbatim* into the url_for(...) function inside the template
# the idea is to pass the keyword arguments of the knowl further along the chain
# of links, in this case the title and the permalink!
# so, this kw_params should be plain python, e.g. "a=1, b='xyz'"
kw_params = ', '.join(('%s="%s"' % (k, v) for k, v in kwargs.iteritems()))
logger.debug("kw_params: %s" % kw_params)
# this is a very simple template based on no other template to render one single Knowl
# for inserting into a website via AJAX or for server-side operations.
if request.method == "POST":
con = request.form['content']
foot = footer or request.form['footer']
elif request.method == "GET":
con = request.args.get("content", k.content)
foot = footer or request.args.get("footer", "1")
# authors = []
# for a in k.author_links():
# authors.append("<a href='%s'>%s</a>" %
# (url_for('users.profile', userid=a['_id']), a['full_name'] or a['_id'] ))
# authors = ', '.join(authors)
render_me = u"""\
{%% include "knowl-defs.html" %%}
{%% from "knowl-defs.html" import KNOWL with context %%}
{%% from "knowl-defs.html" import KNOWL_LINK with context %%}
{%% from "knowl-defs.html" import KNOWL_INC with context %%}
{%% from "knowl-defs.html" import TEXT_DATA with context %%}
<div class="knowl">"""
if foot == "1":
render_me += """\
<div class="knowl-header">
<a href="{{ url_for('.show', ID='%(ID)s', %(kw_params)s ) }}">%(title)s</a>
</div>""" % {'ID': k.id, 'title': (k.title or k.id), 'kw_params': kw_params}
render_me += """<div><div class="knowl-content">%(content)s</div></div>"""
if foot == "1":
render_me += """\
<div class="knowl-footer">
<a href="{{ url_for('.show', ID='%(ID)s', %(kw_params)s) }}">permalink</a>
{%% if user_is_authenticated %%}
·
<a href="{{ url_for('.edit', ID='%(ID)s') }}">edit</a>
{%% endif %%}
</div>"""
# """ · Authors: %(authors)s """
render_me += "</div>"
# render_me = render_me % {'content' : con, 'ID' : k.id }
con = md_preprocess(con)
# markdown enabled
render_me = render_me % {'content': md.convert(con), 'ID': k.id, 'kw_params': kw_params}
#, 'authors' : authors }
# Pass the text on to markdown. Note, backslashes need to be escaped for
# this, but not for the javascript markdown parser
# logger.debug("rendering template string:\n%s" % render_me)
# TODO improve the error message
# so that the user has a clue. Most likely, the {{ KNOWL('...') }} has the wrong syntax!
try:
data = render_template_string(render_me, k=k, **kwargs)
if raw:
# note, this is just internally for the .show method, raw rendering
# doesn't exist right now and will wrap this into a make_reponse!
return data
resp = make_response(data)
# cache 2 minutes if it is a usual GET
if request.method == 'GET':
resp.headers['Cache-Control'] = 'max-age=%s, public' % (2 * 60)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
except Exception, e:
return "ERROR in the template: %s. Please edit it to resolve the problem." % e
@knowledge_page.route("/_cleanup")
@housekeeping
def cleanup():
"""
reindexes knowls, also the list of categories. prunes history.
this is an internal task just for admins!
"""
from knowl import refresh_knowl_categories, extract_cat, make_keywords, get_knowls
cats = refresh_knowl_categories()
knowls = get_knowls()
q_knowls = knowls.find({}, ['content', 'title'])
for k in q_knowls:
kid = k['_id']
cat = extract_cat(kid)
search_keywords = make_keywords(k['content'], kid, k['title'])
knowls.update({'_id': kid},
{"$set": {
'cat': cat,
'_keywords': search_keywords
}})
hcount = 0
# max allowed history length
max_h = 50
q_knowls = knowls.find({'history': {'$exists': True}}, ['history'])
for k in q_knowls:
if len(k['history']) <= max_h:
continue
hcount += 1
knowls.update({'_id': k['_id']}, {'$set': {'history': k['history'][-max_h:]}})
return "categories: %s <br/>reindexed %s knowls<br/>pruned %s histories" % (cats, q_knowls.count(), hcount)
@knowledge_page.route("/")
def index():
# bypassing the Knowl objects to speed things up
from knowl import get_knowls
# See issue #1169
# try:
# get_knowls().ensure_index('_keywords')
# get_knowls().ensure_index('cat')
# except pymongo.errors.OperationFailure:
# pass
cur_cat = request.args.get("category", "")
qualities = []
defaults = "filter" not in request.args
filtermode = "filter" in request.args
searchmode = "search" in request.args
categorymode = "category" in request.args
from knowl import knowl_qualities
# TODO wrap this into a loop:
reviewed = request.args.get("reviewed", "") == "on" or defaults
ok = request.args.get("ok", "") == "on" or defaults
beta = request.args.get("beta", "") == "on" or defaults
if reviewed:
qualities.append("reviewed")
if ok:
qualities.append("ok")
if beta:
qualities.append("beta")
s_query = {}
if filtermode:
quality_q = {'$in': qualities}
s_query['quality'] = quality_q
keyword = request.args.get("search", "").lower()
if searchmode and keyword:
keywords = filter(lambda _: len(_) >= 3, keyword.split(" "))
# logger.debug("keywords: %s" % keywords)
keyword_q = {'_keywords': {"$all": keywords}}
s_query.update(keyword_q)
if categorymode:
s_query.update({'cat': cur_cat}) # { "$regex" : r"^%s\..+" % cur_cat }
logger.debug("search query: %s" % s_query)
knowls = get_knowls().find(s_query, ['title'])
def first_char(k):
t = k['title']
if len(t) == 0:
return "?"
if t[0] not in string.ascii_letters:
return "?"
return t[0].upper()
# way to additionally narrow down the search
# def incl(knwl):
# if keyword in knwl['_id'].lower(): return True
# if keyword in knwl['title'].lower(): return True
# return False
# if keyword: knowls = filter(incl, knowls)
from knowl import get_categories
cats = get_categories()
def knowl_sort_key(knowl):
'''sort knowls, special chars at the end'''
title = knowl['title']
if title and title[0] in string.ascii_letters:
return (0, title.lower())
else:
return (1, title.lower())
knowls = sorted(knowls, key=knowl_sort_key)
from itertools import groupby
knowls = groupby(knowls, first_char)
return render_template("knowl-index.html",
title="Knowledge Database",
bread=get_bread(),
knowls=knowls,
search=keyword,
searchbox=searchbox(request.args.get("search", ""), searchmode),
knowl_qualities=knowl_qualities,
searchmode=searchmode,
filters=(beta, ok, reviewed),
categories = cats,
cur_cat = cur_cat,
categorymode = categorymode)