summaryrefslogtreecommitdiff
path: root/usuwanie_sierot.py
blob: 149bd20c10a17b4c798fc416f683ce34c45729b3 (plain)
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
import sys
from TexSoup import *

soup = TexSoup(sys.stdin.read())

spójniki = [
	"oraz", "albo", "bądź", "czy", "lub", "ani", "ni", "ale", "jednak",
	"lecz", "zaś", "czyli", "i", "więc", "zatem", "choć", "czy", "że"
]

def remove_orphans(text):
	out = ""

	for i, line in enumerate(text.split("\n")):
		if i:
			out += "\n"

		lines = line.split(" ")
		for j, word in enumerate(lines):
			if j == len(lines) - 1:
				out += word
			elif word.strip() != "" and word != "&" \
			     and not word.startswith("\\") \
			     and len(word.strip()) <= 2 or word in spójniki:
				out += word + "~"
			else:
				out += word + " "

	return out

def mutate_r(root):
	if type(root.expr) == data.TexEnv:
		if root.expr.name == "equation":
			return

	try:
		for node in root.all:
			if not isinstance(node, str):
				mutate_r(node)
				continue
			else:
				node.text = remove_orphans(node.text)
	except:
		pass

mutate_r(soup)
print(soup)