|
1
|
|
|
from re import sub |
|
2
|
|
|
from uuid import uuid4 |
|
3
|
|
|
|
|
4
|
|
|
# noinspection PyPackageRequirements |
|
5
|
|
|
from pylatexenc.latexencode import unicode_to_latex # type: ignore |
|
6
|
|
|
|
|
7
|
|
|
from .illustrations.illustration import Illustration # type: ignore |
|
8
|
|
|
from .questions.helpers import format_latex_as_wikitext # type: ignore |
|
9
|
|
|
from .quiz_element import QuizElement # type: ignore |
|
10
|
|
|
from .illustrations.state_of_illustrations import StateOfIllustrations # type: ignore |
|
11
|
|
|
from .questions.question import Question # type: ignore |
|
12
|
|
|
from .questions.question_types import QuestionType # type: ignore |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def prettify(text: str) -> str: |
|
16
|
|
|
text = strip_whitespaces(text) |
|
17
|
|
|
text = format_latex_as_wikitext(text) |
|
18
|
|
|
return text |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def strip_whitespaces(text: str) -> str: |
|
22
|
|
|
text = text.strip("., \n") |
|
23
|
|
|
text = sub(r" \n|\r\n|\s{2}", " ", text) |
|
24
|
|
|
return text |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def truncate_filename(element_text: str, extension: str, quiz_name: str) -> str: |
|
28
|
|
|
number_of_element_text_words = 5 |
|
29
|
|
|
while number_of_element_text_words > 1: |
|
30
|
|
|
split_element_text = element_text.split() |
|
31
|
|
|
split_truncated_element_text = " ".join( |
|
32
|
|
|
split_element_text[:number_of_element_text_words] |
|
33
|
|
|
) |
|
34
|
|
|
upload_filename = create_upload_filename( |
|
35
|
|
|
quiz_name, split_truncated_element_text + "…", extension |
|
36
|
|
|
) |
|
37
|
|
|
if not filename_too_long(upload_filename): |
|
38
|
|
|
return upload_filename |
|
39
|
|
|
number_of_element_text_words -= 1 |
|
40
|
|
|
|
|
41
|
|
|
# noinspection PyUnboundLocalVariable |
|
42
|
|
|
split_truncated_element_text = split_truncated_element_text[:15] |
|
|
|
|
|
|
43
|
|
|
upload_filename = create_upload_filename( |
|
44
|
|
|
quiz_name, split_truncated_element_text + "…", extension |
|
45
|
|
|
) |
|
46
|
|
|
return upload_filename |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def filename_too_long(upload_filename) -> bool: |
|
50
|
|
|
return len(upload_filename) > 100 |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def create_upload_filename( |
|
54
|
|
|
quiz_name: str, name_of_illustration: str, extension: str, make_unique: bool = False |
|
55
|
|
|
) -> str: |
|
56
|
|
|
upload_filename = f'"{name_of_illustration}"' |
|
57
|
|
|
if make_unique: |
|
58
|
|
|
random_hash = uuid4().hex[:6] |
|
59
|
|
|
upload_filename += f" – válaszlehetőség {random_hash}" |
|
60
|
|
|
upload_filename += f" ({quiz_name}){extension}" |
|
61
|
|
|
return upload_filename |
|
62
|
|
|
|