summaryrefslogtreecommitdiff
path: root/usuwanie_sierot.py
diff options
context:
space:
mode:
Diffstat (limited to 'usuwanie_sierot.py')
-rw-r--r--usuwanie_sierot.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/usuwanie_sierot.py b/usuwanie_sierot.py
new file mode 100644
index 0000000..149bd20
--- /dev/null
+++ b/usuwanie_sierot.py
@@ -0,0 +1,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)