-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrtf.py
43 lines (37 loc) · 1.25 KB
/
drtf.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
#!/usr/bin/env python3
"Print RTF file as a tree"
from __future__ import print_function
import re, argparse
class RtfBlock:
def __init__(self, level, text):
self.level = level
self.text = text
class RtfFile:
def __init__(self, path):
self.path = path
self.blocks = []
def decodeChar(self, m):
return bytes([int(m.group(1), 16)]).decode("windows-1251")
def load(self):
self.level = 0
text = " ".join(open(self.path).read().split("\n"))
for m in re.finditer("([{}])([^{}]*)", text):
b, t = m.group(1, 2)
if b == "{":
self.level += 1
else:
self.level -= 1
if t != "":
t = re.sub("\\\\'([0-9a-fA-F]{2})", self.decodeChar, t)
self.blocks.append(RtfBlock(self.level, t))
return self
def print(self):
for b in self.blocks:
head = " " * (b.level - 1) if b.level < 10 else\
"%d " % b.level
print("%s'%s'" % (head, b.text))
print("Final level:%d" % self.level)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("INPUT")
args = parser.parse_args()
RtfFile(args.INPUT).load().print()