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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#Copyright (c) 2010, Loic Fejoz
#Copyright (c) 2010, Walter Bender
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import sys
class RtfException(Exception):
pass
plaintext = 1
control = 2
argument = 3
backslash = 4
escapedChar = 5
class RtfParser(object):
def __init__(self, unicode=False):
self.state = plaintext
self.arg = ''
self.token = ''
self.unicode = unicode
self.par = False
self.output = ''
def getChar(self, code):
""" called when an escaped char is found """
return chr(code)
def getNonBreakingSpace(self):
return ' '
def pushState(self):
pass
def popState(self):
pass
def putChar(self):
pass
def doControl(self,token,arg):
pass
def feed(self,txt):
for c in txt:
self.feedChar(c)
def feedChar(self,char):
if self.state == plaintext: # this is just normal user content
if char == '\\':
self.state = backslash
elif char == '{':
self.pushState()
elif char == '}':
self.popState()
else:
self.putChar(char)
elif self.state == backslash: # a command or escape
if char == '\\' or char == '{' or char == '}':
self.putChar(char)
self.state = plaintext
else:
if char.isalpha() or char in ('*', '-', '|'):
self.state = control
self.token = char
elif char == "'":
self.state = escapedChar
self.escapedChar = ''
elif char in ['\\', '{','}']:
self.putChar(char)
self.state = plaintext
elif char == "~": # non breaking space
self.putChar(self.getNonBreakingSpace())
self.state = plaintext
else:
raise RtfException,'unexpected %s after \\' % char
elif self.state == escapedChar:
self.escapedChar = self.escapedChar + char
if len(self.escapedChar) == 2:
char = self.getChar(int(self.escapedChar,16))
self.putChar(char)
self.state = plaintext
elif self.state == control: # collecting the command token
if char.isalpha():
self.token = self.token + char
elif char.isdigit() or char== '-':
self.state = argument
self.arg = char
else:
self.doControl(self.token,self.arg)
self.state = plaintext
if char == '\\':
self.state = backslash
elif char == '{':
self.pushState()
elif char == '}':
self.popState()
else:
if not char.isspace():
self.putChar(char)
elif self.state == argument: # collecting the optional command argument
if char.isdigit():
self.arg = self.arg + char
else:
self.state = plaintext
self.doControl(self.token,self.arg)
if char == '\\':
self.state = backslash
elif char == '{':
self.pushState()
elif char == '}':
self.popState()
else:
if not char.isspace():
self.putChar(char)
class RtfTextOnly(RtfParser):
def __init__(self):
RtfParser.__init__(self)
self.level = 0
def pushState(self):
self.level = self.level + 1
def popState(self):
self.level = self.level - 1
def putChar(self,ch):
if self.par:
self.output += ch
def doControl(self,token,arg):
if token[0:3] == 'par':
self.par = True
pass
if __name__ == '__main__':
text_only = RtfTextOnly()
if len(sys.argv) != 2:
print 'Usage : %s file.rtf' % sys.argv[0]
else:
for line in open(sys.argv[1], 'r'):
text_only.feed(line)
print text_only.output
|