diff --git a/src/calibre/ebooks/epub/from_html.py b/src/calibre/ebooks/epub/from_html.py index 0c45a6de6f..6282da4b97 100644 --- a/src/calibre/ebooks/epub/from_html.py +++ b/src/calibre/ebooks/epub/from_html.py @@ -304,7 +304,10 @@ def convert(htmlfile, opts, notification=None): opf.add_path_to_manifest(os.path.join(tdir, 'content', 'resources', '_cover_.jpg'), 'image/jpeg') with open(opf_path, 'wb') as f: - f.write(opf.render()) + raw = opf.render() + if not raw.startswith('\n'+raw + f.write(raw) epub = initialize_container(opts.output) epub.add_dir(tdir) if opts.show_opf: diff --git a/src/calibre/ebooks/metadata/epub.py b/src/calibre/ebooks/metadata/epub.py index e5e41ba010..077bcf9440 100644 --- a/src/calibre/ebooks/metadata/epub.py +++ b/src/calibre/ebooks/metadata/epub.py @@ -235,7 +235,7 @@ def main(args=sys.argv): if changed: set_metadata(stream, mi) - print unicode(get_metadata(stream, extract_cover=False)) + print unicode(get_metadata(stream, extract_cover=False)).encode('utf-8') if mi.cover_data[1] is not None: cpath = os.path.splitext(os.path.basename(args[1]))[0] + '_cover.jpg' diff --git a/src/calibre/ebooks/metadata/meta.py b/src/calibre/ebooks/metadata/meta.py index ae1af1a47d..aff09281da 100644 --- a/src/calibre/ebooks/metadata/meta.py +++ b/src/calibre/ebooks/metadata/meta.py @@ -20,6 +20,7 @@ from calibre.ebooks.metadata.opf2 import OPF from calibre.ebooks.metadata.rtf import set_metadata as set_rtf_metadata from calibre.ebooks.lrf.meta import set_metadata as set_lrf_metadata from calibre.ebooks.metadata.epub import set_metadata as set_epub_metadata +from calibre.ebooks.metadata.pdf import set_metadata as set_pdf_metadata try: from calibre.libunrar import extract_member as rar_extract_first except OSError: @@ -122,8 +123,6 @@ def get_comic_cover(stream, type): ext = os.path.splitext(path)[1][1:] return (ext.lower(), data) - - def set_metadata(stream, mi, stream_type='lrf'): if stream_type: stream_type = stream_type.lower() if stream_type == 'lrf': @@ -132,6 +131,8 @@ def set_metadata(stream, mi, stream_type='lrf'): set_epub_metadata(stream, mi) elif stream_type == 'rtf': set_rtf_metadata(stream, mi) + #elif stream_type == 'pdf': + # set_pdf_metadata(stream, mi) def metadata_from_filename(name, pat=None): name = os.path.splitext(name)[0] diff --git a/src/calibre/ebooks/metadata/opf.py b/src/calibre/ebooks/metadata/opf.py index 69054571ec..21e0730c8c 100644 --- a/src/calibre/ebooks/metadata/opf.py +++ b/src/calibre/ebooks/metadata/opf.py @@ -519,6 +519,8 @@ class OPFCreator(MetaInformation): self.guide.set_basedir(self.base_path) opf = template.generate(__appname__=__appname__, mi=self, __version__=__version__).render('xml') + if not opf.startswith('\n'+opf opf_stream.write(opf) opf_stream.flush() toc = getattr(self, 'toc', None) diff --git a/src/calibre/ebooks/metadata/pdf.py b/src/calibre/ebooks/metadata/pdf.py index 84a38d9ee4..ad59351248 100644 --- a/src/calibre/ebooks/metadata/pdf.py +++ b/src/calibre/ebooks/metadata/pdf.py @@ -2,9 +2,9 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' '''Read meta information from PDF files''' -import sys, os +import sys, os, re -from calibre.ebooks.metadata import MetaInformation +from calibre.ebooks.metadata import MetaInformation, authors_to_string, get_parser from pyPdf import PdfFileReader def get_metadata(stream): @@ -28,16 +28,43 @@ def get_metadata(stream): msg = u'Couldn\'t read metadata from pdf: %s with error %s'%(mi.title, unicode(err)) print >>sys.stderr, msg.encode('utf8') return mi - + +def set_metadata(stream, mi): + stream.seek(0) + raw = stream.read() + if mi.title: + tit = mi.title.encode('utf-8') if isinstance(mi.title, unicode) else mi.title + raw = re.compile(r'<<.*?/Title\((.+?)\)', re.DOTALL).sub(lambda m: m.group().replace(m.group(1), tit), raw) + if mi.authors: + au = authors_to_string(mi.authors) + if isinstance(au, unicode): + au = au.encode('utf-8') + raw = re.compile(r'<<.*?/Author\((.+?)\)', re.DOTALL).sub(lambda m: m.group().replace(m.group(1), au), raw) + stream.seek(0) + stream.truncate() + stream.write(raw) + stream.seek(0) + +def option_parser(): + p = get_parser('pdf') + p.remove_option('--category') + p.remove_option('--comment') + return p def main(args=sys.argv): + #p = option_parser() + #opts, args = p.parse_args(args) if len(args) != 2: print >>sys.stderr, _('Usage: pdf-meta file.pdf') print >>sys.stderr, _('No filename specified.') return 1 - path = os.path.abspath(os.path.expanduser(args[1])) - print get_metadata(open(path, 'rb')) + stream = open(os.path.abspath(os.path.expanduser(args[1])), 'r+b') + #mi = MetaInformation(opts.title, opts.authors) + #if mi.title or mi.authors: + # set_metadata(stream, mi) + print unicode(get_metadata(stream)).encode('utf-8') + return 0 if __name__ == '__main__': diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 3d60ffc871..f53dc04d6a 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -414,7 +414,8 @@ except: class Application(QApplication): def __init__(self, args): - QApplication.__init__(self, args) + qargs = [i.encode('utf-8') if isinstance(i, unicode) else i for i in args] + QApplication.__init__(self, qargs) self.translator = QTranslator(self) lang = get_lang() if lang: diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index 77f94374f0..da12bc2715 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -667,9 +667,10 @@ class Main(MainWindow, Ui_MainWindow): self.library_view.model().refresh_ids([id]) for row in rows: - MetadataSingleDialog(self, row.row(), + d = MetadataSingleDialog(self, row.row(), self.library_view.model().db, accepted_callback=accepted) + d.exec_() def edit_bulk_metadata(self, checked): ''' diff --git a/src/calibre/library/server.py b/src/calibre/library/server.py index e494ddee2c..9a7d30244b 100644 --- a/src/calibre/library/server.py +++ b/src/calibre/library/server.py @@ -252,7 +252,7 @@ class LibraryServer(object): extra.append('RATING: %s
'%rating) tags = record[FIELD_MAP['tags']] if tags: - extra.append('TAGS: %s
'%', '.join(tags)) + extra.append('TAGS: %s
'%', '.join(tags.split(','))) series = record[FIELD_MAP['series']] if series: extra.append('SERIES: %s [%d]
'%(series, record[FIELD_MAP['series_index']])) diff --git a/src/calibre/library/static/index.html b/src/calibre/library/static/index.html index 19d8c42c51..15f5268f05 100644 --- a/src/calibre/library/static/index.html +++ b/src/calibre/library/static/index.html @@ -11,7 +11,7 @@