mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
Fix writing metadata to RTF files with no info block
This commit is contained in:
parent
6650029e89
commit
65681cf015
@ -69,7 +69,7 @@ def get_metadata(stream):
|
|||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
if stream.read(5) != r'{\rtf':
|
if stream.read(5) != r'{\rtf':
|
||||||
raise Exception('Not a valid RTF file')
|
raise Exception('Not a valid RTF file')
|
||||||
block, pos = get_document_info(stream)
|
block = get_document_info(stream)[0]
|
||||||
if not block:
|
if not block:
|
||||||
return MetaInformation(None, None)
|
return MetaInformation(None, None)
|
||||||
title, author, comment, category = None, None, None, None
|
title, author, comment, category = None, None, None, None
|
||||||
@ -90,6 +90,29 @@ def get_metadata(stream):
|
|||||||
mi.category = category
|
mi.category = category
|
||||||
return mi
|
return mi
|
||||||
|
|
||||||
|
|
||||||
|
def create_metadata(stream, options):
|
||||||
|
md = r'{\info'
|
||||||
|
if options.title:
|
||||||
|
title = options.title.encode('ascii', 'replace')
|
||||||
|
md += r'{\title %s}'%(title,)
|
||||||
|
if options.authors:
|
||||||
|
author = options.authors.encode('ascii', 'replace')
|
||||||
|
md += r'{\author %s}'%(author,)
|
||||||
|
if options.category:
|
||||||
|
category = options.category.encode('ascii', 'replace')
|
||||||
|
md += r'{\category %s}'%(category,)
|
||||||
|
if options.comment:
|
||||||
|
comment = options.comment.encode('ascii', 'replace')
|
||||||
|
md += r'{\subject %s}'%(comment,)
|
||||||
|
if len(md) > 6:
|
||||||
|
md += '}'
|
||||||
|
stream.seek(0)
|
||||||
|
src = stream.read()
|
||||||
|
ans = src[:6] + md + src[6:]
|
||||||
|
stream.seek(0)
|
||||||
|
stream.write(ans)
|
||||||
|
|
||||||
def set_metadata(stream, options):
|
def set_metadata(stream, options):
|
||||||
'''
|
'''
|
||||||
Modify/add RTF metadata in stream
|
Modify/add RTF metadata in stream
|
||||||
@ -99,7 +122,11 @@ def set_metadata(stream, options):
|
|||||||
index = src.rindex('}')
|
index = src.rindex('}')
|
||||||
return src[:index] + r'{\ '[:-1] + name + ' ' + val + '}}'
|
return src[:index] + r'{\ '[:-1] + name + ' ' + val + '}}'
|
||||||
src, pos = get_document_info(stream)
|
src, pos = get_document_info(stream)
|
||||||
|
if not src:
|
||||||
|
create_metadata(stream, options)
|
||||||
|
else:
|
||||||
olen = len(src)
|
olen = len(src)
|
||||||
|
|
||||||
base_pat = r'\{\\name(.*?)(?<!\\)\}'
|
base_pat = r'\{\\name(.*?)(?<!\\)\}'
|
||||||
title = options.title
|
title = options.title
|
||||||
if title != None:
|
if title != None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user