-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtemplating.py
More file actions
81 lines (59 loc) · 1.67 KB
/
Copy pathtemplating.py
File metadata and controls
81 lines (59 loc) · 1.67 KB
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
import re
import textwrap
from markdown import markdown
from markupsafe import Markup
from pyvecorg import app
@app.template_filter('markdown')
def convert_markdown(text):
text = textwrap.dedent(text)
result = Markup(markdown(text))
return result
@app.template_filter('url')
def normalize_url(url):
return re.sub(r'^https?://', '', url).rstrip('/')
def floor_number(n):
digits = len(str(int(n)))
if digits > 2:
order = 10 ** (digits - 2)
n = (n // order) * order
if digits == 2:
n = (n // 10) * 10
return n
def choose_plural(text, n):
"""
Chooses the Czech plural form based on n.
This either gets and immediately returns a string,
or it gets a list of 3 strings:
* for 1 item
* for 2-4 items
* for the rest
Picks the right one and returns it.
Uses the more modern option where e.g. 21 is NOT pluralized as 1
http://prirucka.ujc.cas.cz/?id=792
"""
if isinstance(text, str):
return text
# we don't expect negative numbers here, but just to be sure
n = abs(n)
if n == 1:
return text[0]
if 1 < n < 5:
return text[1]
return text[2]
@app.template_filter('format_number_text')
def format_number_text(number):
if 'sum' in number:
n = sum(number['sum'])
else:
n = number['value']
if not number.get('exactly'):
n = floor_number(n)
plus = '+'
else:
plus = ''
text = choose_plural(number['text'], n)
return f'{n}{plus} {text}'
@app.template_filter('mastodon_link')
def mastodon_link(handle) -> str:
username, server = handle[1:].split('@')
return f'https://{server}/@{username}'