From 34931d4f734a0aed693ff689bce97f1a0af73030 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 27 Jan 2011 16:30:06 -0700 Subject: [PATCH] BiBTeX Catalog: Add option to include file paths in the catalog. Fixes #8589 (Support for SciPlore MindMapping - Enhance Bib Catalogue) --- src/calibre/gui2/catalog/catalog_bibtex.py | 8 ++- src/calibre/gui2/catalog/catalog_bibtex.ui | 9 ++- src/calibre/library/catalog.py | 74 ++++++++++++++-------- 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/calibre/gui2/catalog/catalog_bibtex.py b/src/calibre/gui2/catalog/catalog_bibtex.py index 7b7739bb46..ebfcc6e546 100644 --- a/src/calibre/gui2/catalog/catalog_bibtex.py +++ b/src/calibre/gui2/catalog/catalog_bibtex.py @@ -19,7 +19,9 @@ class PluginWidget(QWidget, Ui_Form): ('bib_entry', 0), #mixed ('bibfile_enc', 0), #utf-8 ('bibfile_enctag', 0), #strict - ('impcit', True) ] + ('impcit', True), + ('addfiles', False), + ] sync_enabled = False formats = set(['bib']) @@ -49,7 +51,7 @@ class PluginWidget(QWidget, Ui_Form): opt_value = gprefs.get(self.name + '_' + opt[0], opt[1]) if opt[0] in ['bibfile_enc', 'bibfile_enctag', 'bib_entry']: getattr(self, opt[0]).setCurrentIndex(opt_value) - elif opt[0] == 'impcit' : + elif opt[0] in ['impcit', 'addfiles'] : getattr(self, opt[0]).setChecked(opt_value) else: getattr(self, opt[0]).setText(opt_value) @@ -76,7 +78,7 @@ class PluginWidget(QWidget, Ui_Form): for opt in self.OPTION_FIELDS: if opt[0] in ['bibfile_enc', 'bibfile_enctag', 'bib_entry']: opt_value = getattr(self,opt[0]).currentIndex() - elif opt[0] == 'impcit' : + elif opt[0] in ['impcit', 'addfiles'] : opt_value = getattr(self, opt[0]).isChecked() else : opt_value = unicode(getattr(self, opt[0]).text()) diff --git a/src/calibre/gui2/catalog/catalog_bibtex.ui b/src/calibre/gui2/catalog/catalog_bibtex.ui index 7f4920655d..8712d40148 100644 --- a/src/calibre/gui2/catalog/catalog_bibtex.ui +++ b/src/calibre/gui2/catalog/catalog_bibtex.ui @@ -47,7 +47,7 @@ - + @@ -141,6 +141,13 @@ + + + Add files path with formats? + + + + Expression to form the BibTeX citation tag: diff --git a/src/calibre/library/catalog.py b/src/calibre/library/catalog.py index f0e4778de4..e20eebc517 100644 --- a/src/calibre/library/catalog.py +++ b/src/calibre/library/catalog.py @@ -24,10 +24,9 @@ from calibre.utils.logging import default_log as log from calibre.utils.zipfile import ZipFile, ZipInfo from calibre.utils.magick.draw import thumbnail -FIELDS = ['all', 'author_sort', 'authors', 'comments', - 'cover', 'formats', 'id', 'isbn', 'ondevice', 'pubdate', 'publisher', 'rating', - 'series_index', 'series', 'size', 'tags', 'timestamp', 'title', - 'uuid'] +FIELDS = ['all', 'title', 'author_sort', 'authors', 'comments', + 'cover', 'formats','id', 'isbn', 'ondevice', 'pubdate', 'publisher', + 'rating', 'series_index', 'series', 'size', 'tags', 'timestamp', 'uuid'] #Allowed fields for template TEMPLATE_ALLOWED_FIELDS = [ 'author_sort', 'authors', 'id', 'isbn', 'pubdate', @@ -252,6 +251,15 @@ class BIBTEX(CatalogPlugin): # {{{ "Default: '%default'\n" "Applies to: BIBTEX output format")), + Option('--add-files-path', + default = 'True', + dest = 'addfiles', + action = None, + help = _('Create a file entry if formats is selected for BibTeX entries.\n' + 'Boolean value: True, False\n' + "Default: '%default'\n" + "Applies to: BIBTEX output format")), + Option('--citation-template', default = '{authors}{id}', dest = 'bib_cit', @@ -298,7 +306,7 @@ class BIBTEX(CatalogPlugin): # {{{ from calibre.utils.bibtex import BibTeX def create_bibtex_entry(entry, fields, mode, template_citation, - bibtexdict, citation_bibtex = True): + bibtexdict, citation_bibtex=True, calibre_files=True): #Bibtex doesn't like UTF-8 but keep unicode until writing #Define starting chain or if book valid strict and not book return a Fail string @@ -360,8 +368,13 @@ class BIBTEX(CatalogPlugin): # {{{ bibtex_entry.append(u'isbn = "%s"' % re.sub(u'[\D]', u'', item)) elif field == 'formats' : - item = u', '.join([format.rpartition('.')[2].lower() for format in item]) - bibtex_entry.append(u'formats = "%s"' % item) + #Add file path if format is selected + formats = [format.rpartition('.')[2].lower() for format in item] + bibtex_entry.append(u'formats = "%s"' % u', '.join(formats)) + if calibre_files: + files = [u':%s:%s' % (format, format.rpartition('.')[2].upper())\ + for format in item] + bibtex_entry.append(u'files = "%s"' % u', '.join(files)) elif field == 'series_index' : bibtex_entry.append(u'volume = "%s"' % int(item)) @@ -510,32 +523,41 @@ class BIBTEX(CatalogPlugin): # {{{ citation_bibtex= True else : citation_bibtex= opts.impcit + + #Check add file entry and go to default in case of bad CLI + if isinstance(opts.addfiles, (StringType, UnicodeType)) : + if opts.addfiles == 'False' : + addfiles_bibtex = False + elif opts.addfiles == 'True' : + addfiles_bibtex = True + else : + log(" WARNING: incorrect --add-files-path, revert to default") + addfiles_bibtex= True + else : + addfiles_bibtex = opts.addfiles #Preprocess for error and light correction template_citation = preprocess_template(opts.bib_cit) #Open output and write entries - outfile = codecs.open(path_to_output, 'w', bibfile_enc, bibfile_enctag) + with codecs.open(path_to_output, 'w', bibfile_enc, bibfile_enctag)\ + as outfile: + #File header + nb_entries = len(data) + #check in book strict if all is ok else throw a warning into log + if bib_entry == 'book' : + nb_books = len(filter(check_entry_book_valid, data)) + if nb_books < nb_entries : + log(" WARNING: only %d entries in %d are book compatible" % (nb_books, nb_entries)) + nb_entries = nb_books - #File header - nb_entries = len(data) + outfile.write(u'%%%Calibre catalog\n%%%{0} entries in catalog\n\n'.format(nb_entries)) + outfile.write(u'@preamble{"This catalog of %d entries was generated by calibre on %s"}\n\n' + % (nb_entries, nowf().strftime("%A, %d. %B %Y %H:%M").decode(preferred_encoding))) - #check in book strict if all is ok else throw a warning into log - if bib_entry == 'book' : - nb_books = len(filter(check_entry_book_valid, data)) - if nb_books < nb_entries : - log(" WARNING: only %d entries in %d are book compatible" % (nb_books, nb_entries)) - nb_entries = nb_books - - outfile.write(u'%%%Calibre catalog\n%%%{0} entries in catalog\n\n'.format(nb_entries)) - outfile.write(u'@preamble{"This catalog of %d entries was generated by calibre on %s"}\n\n' - % (nb_entries, nowf().strftime("%A, %d. %B %Y %H:%M").decode(preferred_encoding))) - - for entry in data: - outfile.write(create_bibtex_entry(entry, fields, bib_entry, template_citation, - bibtexc, citation_bibtex)) - - outfile.close() + for entry in data: + outfile.write(create_bibtex_entry(entry, fields, bib_entry, template_citation, + bibtexc, citation_bibtex, addfiles_bibtex)) # }}} class EPUB_MOBI(CatalogPlugin):