mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
add full search to ignore formatting
This commit is contained in:
parent
4a568a83be
commit
2243b691cc
3 changed files with 32 additions and 9 deletions
|
@ -638,8 +638,8 @@ update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res
|
||||||
# Finding cards
|
# Finding cards
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
def findCards(self, query):
|
def findCards(self, query, full=False):
|
||||||
return anki.find.Finder(self).findCards(query)
|
return anki.find.Finder(self).findCards(query, full)
|
||||||
|
|
||||||
def findReplace(self, fids, src, dst, regex=None, field=None, fold=True):
|
def findReplace(self, fids, src, dst, regex=None, field=None, fold=True):
|
||||||
return anki.find.findReplace(self, fids, src, dst, regex, field, fold)
|
return anki.find.findReplace(self, fids, src, dst, regex, field, fold)
|
||||||
|
|
28
anki/find.py
28
anki/find.py
|
@ -3,7 +3,7 @@
|
||||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from anki.utils import ids2str, splitFields, joinFields
|
from anki.utils import ids2str, splitFields, joinFields, stripHTML
|
||||||
|
|
||||||
SEARCH_TAG = 0
|
SEARCH_TAG = 0
|
||||||
SEARCH_TYPE = 1
|
SEARCH_TYPE = 1
|
||||||
|
@ -37,9 +37,10 @@ class Finder(object):
|
||||||
def __init__(self, deck):
|
def __init__(self, deck):
|
||||||
self.deck = deck
|
self.deck = deck
|
||||||
|
|
||||||
def findCards(self, query):
|
def findCards(self, query, full=False):
|
||||||
"Return a list of card ids for QUERY."
|
"Return a list of card ids for QUERY."
|
||||||
self.query = query
|
self.query = query
|
||||||
|
self.full = full
|
||||||
self._findLimits()
|
self._findLimits()
|
||||||
if not self.lims['valid']:
|
if not self.lims['valid']:
|
||||||
return []
|
return []
|
||||||
|
@ -161,9 +162,19 @@ order by %s""" % (lim, sort)
|
||||||
def _findText(self, val, neg, c):
|
def _findText(self, val, neg, c):
|
||||||
val = val.replace("*", "%")
|
val = val.replace("*", "%")
|
||||||
extra = "not" if neg else ""
|
extra = "not" if neg else ""
|
||||||
self.lims['args']["_text_%d"%c] = "%"+val+"%"
|
if not self.full:
|
||||||
self.lims['fact'].append("flds %s like :_text_%d escape '\\'" % (
|
self.lims['args']["_text_%d"%c] = "%"+val+"%"
|
||||||
extra, c))
|
self.lims['fact'].append("flds %s like :_text_%d escape '\\'" % (
|
||||||
|
extra, c))
|
||||||
|
else:
|
||||||
|
# in the future we may want to apply this at the end to speed up
|
||||||
|
# the case where there are other limits
|
||||||
|
fids = []
|
||||||
|
for fid, flds in self.deck.db.execute(
|
||||||
|
"select id, flds from facts"):
|
||||||
|
if val in stripHTML(flds):
|
||||||
|
fids.append(fid)
|
||||||
|
self.lims['fact'].append("id in " + ids2str(fids))
|
||||||
|
|
||||||
def _findFids(self, val):
|
def _findFids(self, val):
|
||||||
self.lims['fact'].append("id in (%s)" % val)
|
self.lims['fact'].append("id in (%s)" % val)
|
||||||
|
@ -229,10 +240,13 @@ order by %s""" % (lim, sort)
|
||||||
select id, mid, flds from facts
|
select id, mid, flds from facts
|
||||||
where mid in %s and flds like ? escape '\\'""" % (
|
where mid in %s and flds like ? escape '\\'""" % (
|
||||||
ids2str(mods.keys())),
|
ids2str(mods.keys())),
|
||||||
value):
|
"%" if self.full else value):
|
||||||
flds = splitFields(flds)
|
flds = splitFields(flds)
|
||||||
ord = mods[mid][1]
|
ord = mods[mid][1]
|
||||||
if re.search(regex, flds[ord]):
|
str = flds[ord]
|
||||||
|
if self.full:
|
||||||
|
str = stripHTML(str)
|
||||||
|
if re.search(regex, str):
|
||||||
fids.append(id)
|
fids.append(id)
|
||||||
extra = "not" if isNeg else ""
|
extra = "not" if isNeg else ""
|
||||||
self.lims['fact'].append("id %s in %s" % (extra, ids2str(fids)))
|
self.lims['fact'].append("id %s in %s" % (extra, ids2str(fids)))
|
||||||
|
|
|
@ -101,6 +101,15 @@ def test_findCards():
|
||||||
assert len(deck.findCards("group:default")) == 5
|
assert len(deck.findCards("group:default")) == 5
|
||||||
assert len(deck.findCards("-group:default")) == 0
|
assert len(deck.findCards("-group:default")) == 0
|
||||||
assert len(deck.findCards("-group:foo")) == 5
|
assert len(deck.findCards("-group:foo")) == 5
|
||||||
|
# full search
|
||||||
|
f = deck.newFact()
|
||||||
|
f['Front'] = u'hello<b>world</b>'
|
||||||
|
f['Back'] = u''
|
||||||
|
deck.addFact(f)
|
||||||
|
assert len(deck.findCards("helloworld")) == 0
|
||||||
|
assert len(deck.findCards("helloworld", full=True)) == 1
|
||||||
|
assert len(deck.findCards("front:helloworld")) == 0
|
||||||
|
assert len(deck.findCards("front:helloworld", full=True)) == 1
|
||||||
|
|
||||||
def test_findReplace():
|
def test_findReplace():
|
||||||
deck = getEmptyDeck()
|
deck = getEmptyDeck()
|
||||||
|
|
Loading…
Reference in a new issue