-
Notifications
You must be signed in to change notification settings - Fork 4
/
eaf_pyqterm_utils.py
46 lines (36 loc) · 1.27 KB
/
eaf_pyqterm_utils.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
# Copyright (C) 2023 by Mumulhl <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
# https://github.com/emacs-eaf/eaf-pdf-viewer/blob/021a4eae8f64b886ac97c10c1417816ed86f4a95/eaf_pdf_utils.py#L38
def generate_random_key(count: int, letters: str) -> list[str]:
import math
import random
key_list = []
key_len = 1 if count == 1 else math.ceil(math.log(count) / math.log(len(letters)))
while count > 0:
key = "".join(random.choices(letters, k=key_len))
if key not in key_list:
key_list.append(key)
count -= 1
return key_list
import re
LINK_PATTERN = re.compile(r"(https?://(?:[\w-]+\.)+[\w-]+(?:/[\w/?%&=-]*)?)")
WORD_PATTERN = re.compile(r"[\s,\._()=*\"'\[\]/-]")
SYMBOL_PATTERN = re.compile(r"\s")
def match_link(text: str) -> (dict[int, str], int):
start = 0
count = 0
links = {}
while True:
match_pattern = LINK_PATTERN.search(text, start)
if not match_pattern:
break
else:
count += 1
links[match_pattern.start()] = match_pattern.group()
start = match_pattern.end()
return links, count
def get_regexp(thing: str):
if thing == "word":
return WORD_PATTERN
elif thing == "symbol":
return SYMBOL_PATTERN