From 71f90fc6bde5a391767b14c879d2fadcc07f32aa Mon Sep 17 00:00:00 2001 From: GRiker Date: Sun, 2 Jan 2011 16:38:24 -0700 Subject: [PATCH] GwR Description template fixes --- resources/catalog/template.xhtml | 8 ++-- src/calibre/library/catalog.py | 77 +++++++++++++------------------- 2 files changed, 34 insertions(+), 51 deletions(-) diff --git a/resources/catalog/template.xhtml b/resources/catalog/template.xhtml index 4df2a11c84..97da23243a 100644 --- a/resources/catalog/template.xhtml +++ b/resources/catalog/template.xhtml @@ -13,11 +13,11 @@

{formats}

- - + + - + @@ -36,6 +36,6 @@
{thumb}
{publisher}

-
+
{comments}
diff --git a/src/calibre/library/catalog.py b/src/calibre/library/catalog.py index dd1a9e2e7d..ae61d7cf52 100644 --- a/src/calibre/library/catalog.py +++ b/src/calibre/library/catalog.py @@ -14,6 +14,7 @@ from calibre.constants import preferred_encoding, DEBUG from calibre.customize import CatalogPlugin from calibre.customize.conversion import OptionRecommendation, DummyReporter from calibre.ebooks.BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, Tag, NavigableString +from calibre.ebooks.chardet import substitute_entites from calibre.ebooks.oeb.base import RECOVER_PARSER, XHTML_NS from calibre.ptempfile import PersistentTemporaryDirectory from calibre.utils.config import config_dir @@ -4284,12 +4285,11 @@ class EPUB_MOBI(CatalogPlugin): ''' Generate description header from template ''' - NBSP = ' ' - MIDDOT = '·' def generate_html(): args = dict( author=author, author_prefix=author_prefix, + comments=comments, css=css, formats=formats, genres=genres, @@ -4302,6 +4302,7 @@ class EPUB_MOBI(CatalogPlugin): rating=rating, series=series, series_index=series_index, + thumb=thumb, title=title, title_str=title_str, xmlns=XHTML_NS, @@ -4309,9 +4310,8 @@ class EPUB_MOBI(CatalogPlugin): generated_html = P('catalog/template.xhtml', data=True).decode('utf-8').format(**args) - - soup = BeautifulSoup(generated_html) - return soup.renderContents(None) + generated_html = substitute_entites(generated_html) + return BeautifulSoup(generated_html) if False: print "title metadata:\n%s" % ', '.join(sorted(book.keys())) @@ -4365,7 +4365,7 @@ class EPUB_MOBI(CatalogPlugin): genresTag.insert(gtc, aTag) gtc += 1 if i < len(book['tags'])-1: - genresTag.insert(gtc, NavigableString(' %s ' % MIDDOT)) + genresTag.insert(gtc, NavigableString(' · ')) gtc += 1 genres = genresTag.renderContents() @@ -4374,26 +4374,22 @@ class EPUB_MOBI(CatalogPlugin): if 'formats' in book: for format in sorted(book['formats']): formats.append(format.rpartition('.')[2].upper()) - formats = ' %s ' % MIDDOT.join(formats) + formats = ' · '.join(formats) pubdate = book['date'] pubmonth, pubyear = pubdate.split(' ') - ''' # Thumb - # This doesn't make it through the etree.fromstring parsing - _soup = BeautifulSoup('') - imgTag = Tag(_soup,"img") + _soup = BeautifulSoup('',selfClosingTags=['img']) + thumb = Tag(_soup,"img") if 'cover' in book: - imgTag['src'] = "../images/thumbnail_%d.jpg" % int(book['id']) + thumb['src'] = "../images/thumbnail_%d.jpg" % int(book['id']) else: - imgTag['src'] = "../images/thumbnail_default.jpg" - imgTag['alt'] = "cover thumbnail" - thumb = imgTag.renderContents() - ''' + thumb['src'] = "../images/thumbnail_default.jpg" + thumb['alt'] = "cover thumbnail" # Publisher - publisher = '' + publisher = ' ' if 'publisher' in book: publisher = book['publisher'] @@ -4412,10 +4408,14 @@ class EPUB_MOBI(CatalogPlugin): note_source = book['notes']['source'] note_content = book['notes']['content'] + # Comments + comments = '' + if 'description' in book and book['description'] > '': + comments = book['description'] + + # >>>> Populate the template <<<< - root = etree.fromstring(generate_html(), parser=RECOVER_PARSER) - header = etree.tostring(root, pretty_print=True, encoding='utf-8') - soup = BeautifulSoup(header, selfClosingTags=['mbp:pagebreak']) + soup = generate_html() # >>>> Post-process the template <<<< @@ -4441,6 +4441,10 @@ class EPUB_MOBI(CatalogPlugin): aTag['href'] = "%s.html#%s" % ("ByAlphaAuthor", self.generateAuthorAnchor(book['author'])) + if publisher == ' ': + publisherTag = body.find('td', attrs={'class':'publisher'}) + publisherTag.contents[0].replaceWith(' ') + if not genres: genresTag = body.find('p',attrs={'class':'genres'}) genresTag.extract() @@ -4451,34 +4455,13 @@ class EPUB_MOBI(CatalogPlugin): if note_content == '': tdTag = body.find('td', attrs={'class':'notes'}) - tdTag.contents[0].replaceWith(NBSP) + tdTag.contents[0].replaceWith(' ') - # Cover thumb - tdTag = body.find('td', attrs={'class':'thumbnail'}) - imgTag = Tag(soup,"img") - if 'cover' in book: - imgTag['src'] = "../images/thumbnail_%d.jpg" % int(book['id']) - else: - imgTag['src'] = "../images/thumbnail_default.jpg" - imgTag['alt'] = "cover thumbnail" - tdTag.insert(0,imgTag) - - ''' - # Rating - stars = int(book['rating']) / 2 - rating = '' - if stars: - star_string = self.FULL_RATING_SYMBOL * stars - empty_stars = self.EMPTY_RATING_SYMBOL * (5 - stars) - rating = '%s%s
' % (star_string,empty_stars) - ratingTag = body.find('td',attrs={'class':'rating'}) - ratingTag.insert(0,NavigableString(rating)) - ''' - - # The Blurb - if 'description' in book and book['description'] > '': - blurbTag = body.find(attrs={'class':'description'}) - blurbTag.insert(0,NavigableString(book['description'])) + emptyTags = body.findAll('td', attrs={'class':'empty'}) + for mt in emptyTags: + newEmptyTag = Tag(BeautifulSoup(),'td') + newEmptyTag.insert(0,NavigableString(' ')) + mt.replaceWith(newEmptyTag) if False: print soup.prettify()