mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
E-book viewer: When doing a search do not match text in comments and element attributes
This commit is contained in:
parent
eced07af18
commit
5f9e987b27
@ -19,8 +19,7 @@ from calibre.ebooks.metadata.opf2 import OPF
|
|||||||
from calibre.ptempfile import TemporaryDirectory
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
from calibre.utils.config import DynamicConfig
|
from calibre.utils.config import DynamicConfig
|
||||||
from calibre.utils.logging import default_log
|
from calibre.utils.logging import default_log
|
||||||
from calibre import (guess_type, prepare_string_for_xml,
|
from calibre import guess_type, prepare_string_for_xml
|
||||||
xml_replace_entities)
|
|
||||||
from calibre.ebooks.oeb.transforms.cover import CoverManager
|
from calibre.ebooks.oeb.transforms.cover import CoverManager
|
||||||
from calibre.ebooks.oeb.iterator.spine import (SpineItem, create_indexing_data)
|
from calibre.ebooks.oeb.iterator.spine import (SpineItem, create_indexing_data)
|
||||||
from calibre.ebooks.oeb.iterator.bookmarks import BookmarksMixin
|
from calibre.ebooks.oeb.iterator.bookmarks import BookmarksMixin
|
||||||
@ -61,19 +60,32 @@ class EbookIterator(BookmarksMixin):
|
|||||||
self.ebook_ext = ext.replace('original_', '')
|
self.ebook_ext = ext.replace('original_', '')
|
||||||
|
|
||||||
def search(self, text, index, backwards=False):
|
def search(self, text, index, backwards=False):
|
||||||
text = prepare_string_for_xml(text.lower())
|
from calibre.ebooks.oeb.polish.parsing import parse
|
||||||
pmap = [(i, path) for i, path in enumerate(self.spine)]
|
pmap = [(i, path) for i, path in enumerate(self.spine)]
|
||||||
if backwards:
|
if backwards:
|
||||||
pmap.reverse()
|
pmap.reverse()
|
||||||
|
q = text.lower()
|
||||||
for i, path in pmap:
|
for i, path in pmap:
|
||||||
if (backwards and i < index) or (not backwards and i > index):
|
if (backwards and i < index) or (not backwards and i > index):
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
raw = f.read().decode(path.encoding)
|
raw = f.read().decode(path.encoding)
|
||||||
try:
|
root = parse(raw)
|
||||||
raw = xml_replace_entities(raw)
|
fragments = []
|
||||||
except:
|
def serialize(elem):
|
||||||
pass
|
if elem.text:
|
||||||
if text in raw.lower():
|
fragments.append(elem.text.lower())
|
||||||
|
if elem.tail:
|
||||||
|
fragments.append(elem.tail.lower())
|
||||||
|
for child in elem.iterchildren():
|
||||||
|
if hasattr(getattr(child, 'tag', None), 'rpartition') and child.tag.rpartition('}')[-1] not in {'script', 'style', 'del'}:
|
||||||
|
serialize(child)
|
||||||
|
elif getattr(child, 'tail', None):
|
||||||
|
fragments.append(child.tail.lower())
|
||||||
|
for body in root.xpath('//*[local-name() = "body"]'):
|
||||||
|
body.tail = None
|
||||||
|
serialize(body)
|
||||||
|
|
||||||
|
if q in ''.join(fragments):
|
||||||
return i
|
return i
|
||||||
|
|
||||||
def __enter__(self, processed=False, only_input_plugin=False,
|
def __enter__(self, processed=False, only_input_plugin=False,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user