diff --git a/src/libprs500/ebooks/metadata/rtf.py b/src/libprs500/ebooks/metadata/rtf.py index a1b264ecca..03bde7ccfb 100644 --- a/src/libprs500/ebooks/metadata/rtf.py +++ b/src/libprs500/ebooks/metadata/rtf.py @@ -69,7 +69,7 @@ def get_metadata(stream): stream.seek(0) if stream.read(5) != r'{\rtf': raise Exception('Not a valid RTF file') - block, pos = get_document_info(stream) + block = get_document_info(stream)[0] if not block: return MetaInformation(None, None) title, author, comment, category = None, None, None, None @@ -90,6 +90,29 @@ def get_metadata(stream): mi.category = category 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): ''' Modify/add RTF metadata in stream @@ -99,46 +122,50 @@ def set_metadata(stream, options): index = src.rindex('}') return src[:index] + r'{\ '[:-1] + name + ' ' + val + '}}' src, pos = get_document_info(stream) - olen = len(src) - base_pat = r'\{\\name(.*?)(?