mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add pict.rtf if debugging + simplify extract images in RTFinput
This commit is contained in:
parent
5ffbfc89b5
commit
a335d86cd5
@ -127,35 +127,52 @@ class RTFInput(InputFormatPlugin):
|
|||||||
|
|
||||||
def extract_images(self, picts):
|
def extract_images(self, picts):
|
||||||
self.log('Extracting images...')
|
self.log('Extracting images...')
|
||||||
|
|
||||||
count = 0
|
|
||||||
raw = open(picts, 'rb').read()
|
raw = open(picts, 'rb').read()
|
||||||
starts = []
|
picts = filter(len, re.findall(r'\{\\pict([^}]+)\}', raw))
|
||||||
for match in re.finditer(r'\{\\pict([^}]+)\}', raw):
|
hex = re.compile(r'[^a-zA-Z0-9]')
|
||||||
starts.append(match.start(1))
|
encs = [hex.sub('', pict) for pict in picts]
|
||||||
|
|
||||||
|
count = 0
|
||||||
imap = {}
|
imap = {}
|
||||||
|
for enc in encs:
|
||||||
for start in starts:
|
|
||||||
pos, bc = start, 1
|
|
||||||
while bc > 0:
|
|
||||||
if raw[pos] == '}': bc -= 1
|
|
||||||
elif raw[pos] == '{': bc += 1
|
|
||||||
pos += 1
|
|
||||||
pict = raw[start:pos+1]
|
|
||||||
enc = re.sub(r'[^a-zA-Z0-9]', '', pict)
|
|
||||||
if len(enc) % 2 == 1:
|
if len(enc) % 2 == 1:
|
||||||
enc = enc[:-1]
|
enc = enc[:-1]
|
||||||
data = enc.decode('hex')
|
data = enc.decode('hex')
|
||||||
count += 1
|
count += 1
|
||||||
name = (('%4d'%count).replace(' ', '0'))+'.wmf'
|
name = '%04d.wmf' % count
|
||||||
open(name, 'wb').write(data)
|
open(name, 'wb').write(data)
|
||||||
imap[count] = name
|
imap[count] = name
|
||||||
#open(name+'.hex', 'wb').write(enc)
|
#open(name+'.hex', 'wb').write(enc)
|
||||||
return self.convert_images(imap)
|
return self.convert_images(imap)
|
||||||
|
|
||||||
|
# count = 0
|
||||||
|
# raw = open(picts, 'rb').read()
|
||||||
|
# starts = []
|
||||||
|
# for match in re.finditer(r'\{\\pict([^}]+)\}', raw):
|
||||||
|
# starts.append(match.start(1))
|
||||||
|
|
||||||
|
# imap = {}
|
||||||
|
# for start in starts:
|
||||||
|
# pos, bc = start, 1
|
||||||
|
# while bc > 0:
|
||||||
|
# if raw[pos] == '}': bc -= 1
|
||||||
|
# elif raw[pos] == '{': bc += 1
|
||||||
|
# pos += 1
|
||||||
|
# pict = raw[start:pos+1]
|
||||||
|
# enc = re.sub(r'[^a-zA-Z0-9]', '', pict)
|
||||||
|
# if len(enc) % 2 == 1:
|
||||||
|
# enc = enc[:-1]
|
||||||
|
# data = enc.decode('hex')
|
||||||
|
# count += 1
|
||||||
|
# name = (('%4d'%count).replace(' ', '0'))+'.wmf'
|
||||||
|
# open(name, 'wb').write(data)
|
||||||
|
# imap[count] = name
|
||||||
|
# #open(name+'.hex', 'wb').write(enc)
|
||||||
|
# return self.convert_images(imap)
|
||||||
|
|
||||||
def convert_images(self, imap):
|
def convert_images(self, imap):
|
||||||
for count, val in imap.items():
|
for count, val in imap.iteritems():
|
||||||
try:
|
try:
|
||||||
imap[count] = self.convert_image(val)
|
imap[count] = self.convert_image(val)
|
||||||
except:
|
except:
|
||||||
|
@ -146,7 +146,8 @@ class Pict:
|
|||||||
|
|
||||||
def process_pict(self):
|
def process_pict(self):
|
||||||
self.__make_dir()
|
self.__make_dir()
|
||||||
with open(self.__file) as read_obj, open(self.__write_to, 'w') as write_obj:
|
with open(self.__file) as read_obj, \
|
||||||
|
open(self.__write_to, 'w') as write_obj:
|
||||||
for line in read_obj:
|
for line in read_obj:
|
||||||
self.__token_info = line[:16]
|
self.__token_info = line[:16]
|
||||||
if self.__token_info == 'ob<nu<open-brack':
|
if self.__token_info == 'ob<nu<open-brack':
|
||||||
@ -167,6 +168,7 @@ class Pict:
|
|||||||
copy_obj = copy.Copy(bug_handler = self.__bug_handler)
|
copy_obj = copy.Copy(bug_handler = self.__bug_handler)
|
||||||
if self.__copy:
|
if self.__copy:
|
||||||
copy_obj.copy_file(self.__write_to, "pict.data")
|
copy_obj.copy_file(self.__write_to, "pict.data")
|
||||||
|
copy_obj.copy_file(self.__pict_file, "pict.rtf")
|
||||||
copy_obj.rename(self.__write_to, self.__file)
|
copy_obj.rename(self.__write_to, self.__file)
|
||||||
os.remove(self.__write_to)
|
os.remove(self.__write_to)
|
||||||
if self.__pict_count == 0:
|
if self.__pict_count == 0:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user