-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextTools.py
More file actions
executable file
·73 lines (54 loc) · 1.71 KB
/
textTools.py
File metadata and controls
executable file
·73 lines (54 loc) · 1.71 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
#!/usr/bin/env python3
# Text tools (for better experience on plain text)
#
# by Magnetic-Fox, 13.07.2024 - 06.01.2026
#
# (C)2024-2026 Bartłomiej "Magnetic-Fox" Węgrzyn!
import htmlTools
# Simple new line characters counter
def countNewLines(data, position):
count = 0
while data.find("\n", position) == position:
count = count + 1
position = position + 1
return count
# Simple duplicated new line characters remover
def removeDuplicatedNewLines(data):
while data.find("\n\n\n") != -1:
count = countNewLines(data, data.find("\n\n\n"))
data = data.replace("\n" * count, "\n\n")
return data
# Simple spaces counter
def spacesCount(string, position):
for x in range(position, len(string)):
if(string[x] != ' '):
return x - position
if(position <= len(string)):
return len(string) - position
else:
return 0
# Multi spaces to returns
def multiSpacesToReturns(string):
while(string.find(" ") != -1):
# Not really optimized line, but I didn't want to use regexes ;)
string = string.replace(" " * spacesCount(string, string.find(" ")), "\n" * (spacesCount(string, string.find(" ")) - 1), 1)
return string
# Function for finding HTML-style amp character
def findAmpChar(string, position = 0):
ampPos=string.find('&', position)
endPos=string.find(';', ampPos)
if endPos > ampPos:
return string[ampPos:endPos+1]
else:
return ""
# Function for changing all HTML-style amp characters
def changeAmpChars(string):
position = 0
while(findAmpChar(string, position) != ""):
converter = htmlTools.HTMLFilter()
converter.feed(findAmpChar(string))
string = string.replace(findAmpChar(string), converter.text)
position = string.find("&", position + 1)
if position < 0:
break
return string