add page leaderboard, fix issue with spoilers by monkey-patching

This commit is contained in:
Yusur 2022-05-21 23:33:23 +02:00
parent 78d938e266
commit f50511759b
2 changed files with 56 additions and 1 deletions

17
app.py
View file

@ -109,6 +109,10 @@ class StrikethroughPostprocessor(markdown.postprocessors.Postprocessor):
def convert(self, match):
return '<del>' + match.group(1) + '</del>'
# XXX ugly monkeypatch to make spoilers prevail over blockquotes
from markdown.blockprocessors import BlockQuoteProcessor
BlockQuoteProcessor.RE = re.compile(r'(^|\n)[ ]{0,3}>(?!!)[ ]?(.*)')
### XXX it currently only detects spoilers that are not at the beginning of the line. To be fixed.
class SpoilerExtension(markdown.extensions.Extension):
def extendMarkdown(self, md, md_globals):
@ -685,6 +689,19 @@ def view_random():
continue
return redirect(page.get_url())
@app.route('/p/leaderboard/')
def page_leaderboard():
headers = {
'Cache-Control': 'max-age=180, stale-while-revalidate=1800'
}
pages = []
for p in Page.select():
score = (p.latest.length >> 10) + p.forward_links.count() + p.back_links.count()
pages.append((p, score, p.back_links.count(), p.forward_links.count(), p.latest.length))
pages.sort(key = lambda x: (x[1], x[2], x[4], x[3]), reverse = True)
return render_template('leaderboard.html', pages=pages)
@app.route('/<slug:name>/')
def view_named(name):

View file

@ -0,0 +1,38 @@
{% extends "base.html" %}
{% block title %}Best pages - {{ app_name }}{% endblock %}
{% block meta %}
<meta name="robots" content="noindex,nofollow" />
{% endblock %}
{% block content %}
<h1>Best pages</h1>
<table>
<thead>
<tr>
<th>#</th>
<th>Page Name</th>
<th>Score</th>
<th><abbr title="Length in bytes">Len</abbr></th>
<th><abbr title="Backlinks">BL</abbr></th>
<th><abbr title="Forward links">FL</abbr></th>
</tr>
</thead>
<tbody>
{% set counters = namespace(row = 0) %}
{% for p, score, bklinks, fwlinks, length in pages %}
<tr>
{% set counters.row = counters.row + 1 %}
<th style="text-align: right">{{ counters.row }}</th>
<td><a href="{{ p.get_url() }}">{{ p.title }}</a> (#{{ p.id }})</td>
<td><strong>{{ score }}</strong></td>
<td>{{ length }}</td>
<td>{{ bklinks }}</td>
<td>{{ fwlinks }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}