| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
import re |
| 8 |
import string |
| 9 |
|
| 10 |
from fenrirscreenreader.core import debug |
| 11 |
|
| 12 |
|
| 13 |
class TextManager: |
| 14 |
def __init__(self): |
| 15 |
# https://regex101.com/ |
| 16 |
self.regExSingle = re.compile(r"(([^\w\s])\2{5,})") |
| 17 |
self.regExDouble = re.compile(r"([^\w\s]{2,}){5,}") |
| 18 |
|
| 19 |
def initialize(self, environment): |
| 20 |
self.env = environment |
| 21 |
|
| 22 |
def shutdown(self): |
| 23 |
pass |
| 24 |
|
| 25 |
def replace_head_lines(self, text): |
| 26 |
# fast len check for bigger typing echo |
| 27 |
if len(text) < 5: |
| 28 |
return text |
| 29 |
# more strong check, to not match if not needed: |
| 30 |
if ( |
| 31 |
len( |
| 32 |
text.strip( |
| 33 |
string.ascii_letters + string.digits + string.whitespace |
| 34 |
) |
| 35 |
) |
| 36 |
< 5 |
| 37 |
): |
| 38 |
return text |
| 39 |
result = "" |
| 40 |
new_text = "" |
| 41 |
last_pos = 0 |
| 42 |
for match in self.regExDouble.finditer(text): |
| 43 |
span = match.span() |
| 44 |
new_text += text[last_pos : span[0]] |
| 45 |
number_of_chars = len(text[span[0] : span[1]]) |
| 46 |
name = text[span[0] : span[1]][:2] |
| 47 |
if not self.env["runtime"]["PunctuationManager"].is_puctuation( |
| 48 |
name[0] |
| 49 |
): |
| 50 |
last_pos = span[1] |
| 51 |
continue |
| 52 |
if name[0] == name[1]: |
| 53 |
new_text += ( |
| 54 |
" " |
| 55 |
+ str(number_of_chars) |
| 56 |
+ " " |
| 57 |
+ self.env["runtime"][ |
| 58 |
"PunctuationManager" |
| 59 |
].proceed_punctuation(name[0], True) |
| 60 |
+ " " |
| 61 |
) |
| 62 |
else: |
| 63 |
new_text += ( |
| 64 |
" " |
| 65 |
+ self.env["runtime"][ |
| 66 |
"PunctuationManager" |
| 67 |
].proceed_punctuation(name[0], True) |
| 68 |
+ " " |
| 69 |
+ str(int(number_of_chars / 2)) |
| 70 |
+ " " |
| 71 |
+ self.env["runtime"][ |
| 72 |
"PunctuationManager" |
| 73 |
].proceed_punctuation(name[1], True) |
| 74 |
+ " " |
| 75 |
) |
| 76 |
last_pos = span[1] |
| 77 |
if last_pos != 0: |
| 78 |
new_text += " " |
| 79 |
new_text += text[last_pos:] |
| 80 |
last_pos = 0 |
| 81 |
for match in self.regExSingle.finditer(new_text): |
| 82 |
span = match.span() |
| 83 |
result += new_text[last_pos : span[0]] |
| 84 |
number_of_chars = len(new_text[span[0] : span[1]]) |
| 85 |
name = new_text[span[0] : span[1]][:2] |
| 86 |
if not self.env["runtime"]["PunctuationManager"].is_puctuation( |
| 87 |
name[0] |
| 88 |
): |
| 89 |
last_pos = span[1] |
| 90 |
continue |
| 91 |
if name[0] == name[1]: |
| 92 |
result += ( |
| 93 |
" " |
| 94 |
+ str(number_of_chars) |
| 95 |
+ " " |
| 96 |
+ self.env["runtime"][ |
| 97 |
"PunctuationManager" |
| 98 |
].proceed_punctuation(name[0], True) |
| 99 |
+ " " |
| 100 |
) |
| 101 |
else: |
| 102 |
result += ( |
| 103 |
" " |
| 104 |
+ self.env["runtime"][ |
| 105 |
"PunctuationManager" |
| 106 |
].proceed_punctuation(name[0], True) |
| 107 |
+ " " |
| 108 |
+ str(int(number_of_chars / 2)) |
| 109 |
+ " " |
| 110 |
+ self.env["runtime"][ |
| 111 |
"PunctuationManager" |
| 112 |
].proceed_punctuation(name[1], True) |
| 113 |
+ " " |
| 114 |
) |
| 115 |
last_pos = span[1] |
| 116 |
if last_pos != 0: |
| 117 |
result += " " |
| 118 |
result += new_text[last_pos:] |
| 119 |
return result |