Port usage of BeautifulSoup Tag constructor

This commit is contained in:
Kovid Goyal 2019-03-23 08:37:01 +05:30
parent 692230147c
commit c89b656df4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 215 additions and 217 deletions

View File

@ -201,7 +201,7 @@ class KINDLE(USBMS):
return bookmarked_books return bookmarked_books
def generate_annotation_html(self, bookmark): def generate_annotation_html(self, bookmark):
from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag from calibre.ebooks.BeautifulSoup import BeautifulSoup
# Returns <div class="user_annotations"> ... </div> # Returns <div class="user_annotations"> ... </div>
last_read_location = bookmark.last_read_location last_read_location = bookmark.last_read_location
timestamp = datetime.datetime.utcfromtimestamp(bookmark.timestamp) timestamp = datetime.datetime.utcfromtimestamp(bookmark.timestamp)
@ -209,11 +209,11 @@ class KINDLE(USBMS):
ka_soup = BeautifulSoup() ka_soup = BeautifulSoup()
dtc = 0 dtc = 0
divTag = Tag(ka_soup,'div') divTag = ka_soup.new_tag('div')
divTag['class'] = 'user_annotations' divTag['class'] = 'user_annotations'
# Add the last-read location # Add the last-read location
spanTag = Tag(ka_soup, 'span') spanTag = ka_soup.new_tag('span')
spanTag['style'] = 'font-weight:bold' spanTag['style'] = 'font-weight:bold'
if bookmark.book_format == 'pdf': if bookmark.book_format == 'pdf':
spanTag.insert(0,BeautifulSoup( spanTag.insert(0,BeautifulSoup(
@ -230,7 +230,7 @@ class KINDLE(USBMS):
divTag.insert(dtc, spanTag) divTag.insert(dtc, spanTag)
dtc += 1 dtc += 1
divTag.insert(dtc, Tag(ka_soup,'br')) divTag.insert(dtc, ka_soup.new_tag('br'))
dtc += 1 dtc += 1
if bookmark.user_notes: if bookmark.user_notes:
@ -268,7 +268,6 @@ class KINDLE(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 Tag
from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata import MetaInformation
bm = annotation bm = annotation
@ -288,7 +287,7 @@ class KINDLE(USBMS):
if set(mi.tags).intersection(ignore_tags): if set(mi.tags).intersection(ignore_tags):
return return
if mi.comments: if mi.comments:
hrTag = Tag(user_notes_soup,'hr') hrTag = user_notes_soup.new_tag('hr')
hrTag['class'] = 'annotations_divider' hrTag['class'] = 'annotations_divider'
user_notes_soup.insert(0, hrTag) user_notes_soup.insert(0, hrTag)

View File

@ -1194,7 +1194,7 @@ class KOBO(USBMS):
def generate_annotation_html(self, bookmark): def generate_annotation_html(self, bookmark):
import calendar import calendar
from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag from calibre.ebooks.BeautifulSoup import BeautifulSoup
# Returns <div class="user_annotations"> ... </div> # Returns <div class="user_annotations"> ... </div>
# last_read_location = bookmark.last_read_location # last_read_location = bookmark.last_read_location
# timestamp = bookmark.timestamp # timestamp = bookmark.timestamp
@ -1218,11 +1218,11 @@ class KOBO(USBMS):
# debug_print("Percent read: ", percent_read) # debug_print("Percent read: ", percent_read)
ka_soup = BeautifulSoup() ka_soup = BeautifulSoup()
dtc = 0 dtc = 0
divTag = Tag(ka_soup,'div') divTag = ka_soup.new_tag('div')
divTag['class'] = 'user_annotations' divTag['class'] = 'user_annotations'
# Add the last-read location # Add the last-read location
spanTag = Tag(ka_soup, 'span') spanTag = ka_soup.new_tag('span')
spanTag['style'] = 'font-weight:normal' spanTag['style'] = 'font-weight:normal'
if bookmark.book_format == 'epub': if bookmark.book_format == 'epub':
spanTag.insert(0,BeautifulSoup( spanTag.insert(0,BeautifulSoup(
@ -1239,7 +1239,7 @@ class KOBO(USBMS):
divTag.insert(dtc, spanTag) divTag.insert(dtc, spanTag)
dtc += 1 dtc += 1
divTag.insert(dtc, Tag(ka_soup,'br')) divTag.insert(dtc, ka_soup.new_tag('br'))
dtc += 1 dtc += 1
if bookmark.user_notes: if bookmark.user_notes:
@ -1301,7 +1301,6 @@ 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 Tag
bm = annotation bm = annotation
ignore_tags = {'Catalog', 'Clippings'} ignore_tags = {'Catalog', 'Clippings'}
@ -1320,7 +1319,7 @@ class KOBO(USBMS):
if set(mi.tags).intersection(ignore_tags): if set(mi.tags).intersection(ignore_tags):
return return
if mi.comments: if mi.comments:
hrTag = Tag(user_notes_soup,'hr') hrTag = user_notes_soup.new_tag('hr')
hrTag['class'] = 'annotations_divider' hrTag['class'] = 'annotations_divider'
user_notes_soup.insert(0, hrTag) user_notes_soup.insert(0, hrTag)

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import re import re
from calibre.constants import preferred_encoding from calibre.constants import preferred_encoding
from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag, NavigableString, \ from calibre.ebooks.BeautifulSoup import BeautifulSoup, NavigableString, \
CData, Comment, Declaration, ProcessingInstruction CData, Comment, Declaration, ProcessingInstruction
from calibre import prepare_string_for_xml from calibre import prepare_string_for_xml
from calibre.utils.html2text import html2text from calibre.utils.html2text import html2text
@ -95,20 +95,20 @@ def comments_to_html(comments):
all_tokens = list(soup.contents) all_tokens = list(soup.contents)
for token in all_tokens: for token in all_tokens:
if type(token) is NavigableString: if isinstance(token, NavigableString):
if not open_pTag: if not open_pTag:
pTag = Tag(result,'p') pTag = result.new_tag('p')
open_pTag = True open_pTag = True
ptc = 0 ptc = 0
pTag.insert(ptc,prepare_string_for_xml(token)) pTag.insert(ptc,prepare_string_for_xml(token))
ptc += 1 ptc += 1
elif type(token) in (CData, Comment, Declaration, elif isinstance(token, (CData, Comment, Declaration,
ProcessingInstruction): ProcessingInstruction)):
continue continue
elif token.name in ['br', 'b', 'i', 'em', 'strong', 'span', 'font', 'a', elif token.name in ['br', 'b', 'i', 'em', 'strong', 'span', 'font', 'a',
'hr']: 'hr']:
if not open_pTag: if not open_pTag:
pTag = Tag(result,'p') pTag = result.new_tag('p')
open_pTag = True open_pTag = True
ptc = 0 ptc = 0
pTag.insert(ptc, token) pTag.insert(ptc, token)