Fix usages of soup.prettify()

This commit is contained in:
Kovid Goyal 2019-03-24 14:35:58 +05:30
parent 5e097e3ac7
commit 9395c717ed
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 28 additions and 26 deletions

View File

@ -267,6 +267,7 @@ class KINDLE(USBMS):
def add_annotation_to_library(self, db, db_id, annotation): def add_annotation_to_library(self, db, db_id, annotation):
from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata import MetaInformation
from calibre.ebooks.BeautifulSoup import prettify
bm = annotation bm = annotation
ignore_tags = {'Catalog', 'Clippings'} ignore_tags = {'Catalog', 'Clippings'}
@ -289,9 +290,9 @@ class KINDLE(USBMS):
hrTag['class'] = 'annotations_divider' hrTag['class'] = 'annotations_divider'
user_notes_soup.insert(0, hrTag) user_notes_soup.insert(0, hrTag)
mi.comments += unicode_type(user_notes_soup.prettify()) mi.comments += prettify(user_notes_soup)
else: else:
mi.comments = unicode_type(user_notes_soup.prettify()) mi.comments = prettify(user_notes_soup)
# Update library comments # Update library comments
db.set_comment(db_id, mi.comments) db.set_comment(db_id, mi.comments)

View File

@ -1299,6 +1299,7 @@ class KOBO(USBMS):
return ka_soup return ka_soup
def add_annotation_to_library(self, db, db_id, annotation): def add_annotation_to_library(self, db, db_id, annotation):
from calibre.ebooks.BeautifulSoup import prettify
bm = annotation bm = annotation
ignore_tags = {'Catalog', 'Clippings'} ignore_tags = {'Catalog', 'Clippings'}
@ -1321,9 +1322,9 @@ class KOBO(USBMS):
hrTag['class'] = 'annotations_divider' hrTag['class'] = 'annotations_divider'
user_notes_soup.insert(0, hrTag) user_notes_soup.insert(0, hrTag)
mi.comments += unicode_type(user_notes_soup.prettify()) mi.comments += prettify(user_notes_soup)
else: else:
mi.comments = unicode_type(user_notes_soup.prettify()) mi.comments = prettify(user_notes_soup)
# Update library comments # Update library comments
db.set_comment(db_id, mi.comments) db.set_comment(db_id, mi.comments)

View File

@ -26,6 +26,13 @@ def parse_html(markup):
return parse(markup, return_root=False) return parse(markup, return_root=False)
def prettify(soup):
ans = soup.prettify()
if isinstance(ans, bytes):
ans = ans.decode('utf-8')
return ans
def BeautifulSoup(markup='', *a, **kw): def BeautifulSoup(markup='', *a, **kw):
return parse_html(markup) return parse_html(markup)

View File

@ -13,7 +13,7 @@ from calibre import (
from calibre.constants import isosx, cache_dir from calibre.constants import isosx, cache_dir
from calibre.customize.conversion import DummyReporter from calibre.customize.conversion import DummyReporter
from calibre.customize.ui import output_profiles from calibre.customize.ui import output_profiles
from calibre.ebooks.BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, NavigableString from calibre.ebooks.BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, NavigableString, prettify
from calibre.ebooks.chardet import substitute_entites from calibre.ebooks.chardet import substitute_entites
from calibre.ebooks.metadata import author_to_author_sort from calibre.ebooks.metadata import author_to_author_sort
from calibre.library.catalogs import AuthorSortMismatchException, EmptyCatalogException, \ from calibre.library.catalogs import AuthorSortMismatchException, EmptyCatalogException, \
@ -1621,9 +1621,8 @@ class CatalogBuilder(object):
# Write the generated file to content_dir # Write the generated file to content_dir
outfile_spec = "%s/ByAlphaAuthor.html" % (self.content_dir) outfile_spec = "%s/ByAlphaAuthor.html" % (self.content_dir)
outfile = open(outfile_spec, 'w') with open(outfile_spec, 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
self.html_filelist_1.append("content/ByAlphaAuthor.html") self.html_filelist_1.append("content/ByAlphaAuthor.html")
def generate_html_by_date_added(self): def generate_html_by_date_added(self):
@ -1885,9 +1884,8 @@ class CatalogBuilder(object):
# Write the generated file to content_dir # Write the generated file to content_dir
outfile_spec = "%s/ByDateAdded.html" % (self.content_dir) outfile_spec = "%s/ByDateAdded.html" % (self.content_dir)
outfile = open(outfile_spec, 'w') with open(outfile_spec, 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
self.html_filelist_2.append("content/ByDateAdded.html") self.html_filelist_2.append("content/ByDateAdded.html")
def generate_html_by_date_read(self): def generate_html_by_date_read(self):
@ -2062,9 +2060,8 @@ class CatalogBuilder(object):
# Write the generated file to content_dir # Write the generated file to content_dir
outfile_spec = "%s/ByDateRead.html" % (self.content_dir) outfile_spec = "%s/ByDateRead.html" % (self.content_dir)
outfile = open(outfile_spec, 'w') with open(outfile_spec, 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
self.html_filelist_2.append("content/ByDateRead.html") self.html_filelist_2.append("content/ByDateRead.html")
def generate_html_by_genres(self): def generate_html_by_genres(self):
@ -2309,9 +2306,8 @@ class CatalogBuilder(object):
dtc += 1 dtc += 1
# Write the generated file to content_dir # Write the generated file to content_dir
outfile = open(outfile, 'w') with open(outfile, 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
if len(books) > 1: if len(books) > 1:
titles_spanned = [(books[0]['author'], books[0]['title']), (books[-1]['author'], books[-1]['title'])] titles_spanned = [(books[0]['author'], books[0]['title']), (books[-1]['author'], books[-1]['title'])]
@ -2469,9 +2465,8 @@ class CatalogBuilder(object):
# Write the generated file to content_dir # Write the generated file to content_dir
outfile_spec = "%s/BySeries.html" % (self.content_dir) outfile_spec = "%s/BySeries.html" % (self.content_dir)
outfile = open(outfile_spec, 'w') with open(outfile_spec, 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
self.html_filelist_1.append("content/BySeries.html") self.html_filelist_1.append("content/BySeries.html")
def generate_html_by_title(self): def generate_html_by_title(self):
@ -2624,9 +2619,8 @@ class CatalogBuilder(object):
# Write the volume to content_dir # Write the volume to content_dir
outfile_spec = "%s/ByAlphaTitle.html" % (self.content_dir) outfile_spec = "%s/ByAlphaTitle.html" % (self.content_dir)
outfile = open(outfile_spec, 'w') with open(outfile_spec, 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
self.html_filelist_1.append("content/ByAlphaTitle.html") self.html_filelist_1.append("content/ByAlphaTitle.html")
def generate_html_description_header(self, book): def generate_html_description_header(self, book):
@ -2845,9 +2839,8 @@ class CatalogBuilder(object):
soup = self.generate_html_description_header(title) soup = self.generate_html_description_header(title)
# Write the book entry to content_dir # Write the book entry to content_dir
outfile = open("%s/book_%d.html" % (self.content_dir, int(title['id'])), 'w') with open("%s/book_%d.html" % (self.content_dir, int(title['id'])), 'wb') as outfile:
outfile.write(soup.prettify()) outfile.write(prettify(soup).encode('utf-8'))
outfile.close()
def generate_html_empty_header(self, title): def generate_html_empty_header(self, title):
""" Return a boilerplate HTML header. """ Return a boilerplate HTML header.