2020-05-05 12:50:17 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
collection::Collection,
|
|
|
|
err::{AnkiError, Result},
|
2020-05-07 09:54:23 +02:00
|
|
|
notes::{NoteID, TransformNoteOutput},
|
2021-03-07 14:43:18 +01:00
|
|
|
prelude::*,
|
2020-05-06 12:06:42 +02:00
|
|
|
text::normalize_to_nfc,
|
2020-05-05 12:50:17 +02:00
|
|
|
};
|
|
|
|
use regex::Regex;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
pub struct FindReplaceContext {
|
|
|
|
nids: Vec<NoteID>,
|
|
|
|
search: Regex,
|
|
|
|
replacement: String,
|
|
|
|
field_name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FindReplaceContext {
|
|
|
|
pub fn new(
|
|
|
|
nids: Vec<NoteID>,
|
|
|
|
search_re: &str,
|
|
|
|
repl: impl Into<String>,
|
|
|
|
field_name: Option<String>,
|
|
|
|
) -> Result<Self> {
|
|
|
|
Ok(FindReplaceContext {
|
|
|
|
nids,
|
|
|
|
search: Regex::new(search_re).map_err(|_| AnkiError::invalid_input("invalid regex"))?,
|
|
|
|
replacement: repl.into(),
|
|
|
|
field_name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_text<'a>(&self, text: &'a str) -> Cow<'a, str> {
|
|
|
|
self.search.replace_all(text, self.replacement.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Collection {
|
2020-05-06 12:06:42 +02:00
|
|
|
pub fn find_and_replace(
|
|
|
|
&mut self,
|
|
|
|
nids: Vec<NoteID>,
|
|
|
|
search_re: &str,
|
|
|
|
repl: &str,
|
|
|
|
field_name: Option<String>,
|
2020-05-07 09:54:23 +02:00
|
|
|
) -> Result<usize> {
|
2020-05-06 12:06:42 +02:00
|
|
|
self.transact(None, |col| {
|
2021-03-07 14:43:18 +01:00
|
|
|
let norm = col.get_bool(BoolKey::NormalizeNoteText);
|
2020-05-06 12:06:42 +02:00
|
|
|
let search = if norm {
|
|
|
|
normalize_to_nfc(search_re)
|
|
|
|
} else {
|
|
|
|
search_re.into()
|
|
|
|
};
|
|
|
|
let ctx = FindReplaceContext::new(nids, &search, repl, field_name)?;
|
2020-05-07 09:54:23 +02:00
|
|
|
col.find_and_replace_inner(ctx)
|
2020-05-06 12:06:42 +02:00
|
|
|
})
|
2020-05-05 12:50:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 09:54:23 +02:00
|
|
|
fn find_and_replace_inner(&mut self, ctx: FindReplaceContext) -> Result<usize> {
|
|
|
|
let mut last_ntid = None;
|
|
|
|
let mut field_ord = None;
|
|
|
|
self.transform_notes(&ctx.nids, |note, nt| {
|
|
|
|
if last_ntid != Some(nt.id) {
|
|
|
|
field_ord = ctx.field_name.as_ref().and_then(|n| nt.get_field_ord(n));
|
|
|
|
last_ntid = Some(nt.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut changed = false;
|
|
|
|
match field_ord {
|
|
|
|
None => {
|
|
|
|
// all fields
|
2021-03-02 10:02:00 +01:00
|
|
|
for txt in note.fields_mut() {
|
2020-05-07 09:54:23 +02:00
|
|
|
if let Cow::Owned(otxt) = ctx.replace_text(txt) {
|
|
|
|
changed = true;
|
|
|
|
*txt = otxt;
|
2020-05-05 12:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 09:54:23 +02:00
|
|
|
}
|
|
|
|
Some(ord) => {
|
|
|
|
// single field
|
2021-03-02 10:02:00 +01:00
|
|
|
if let Some(txt) = note.fields_mut().get_mut(ord) {
|
2020-05-07 09:54:23 +02:00
|
|
|
if let Cow::Owned(otxt) = ctx.replace_text(txt) {
|
|
|
|
changed = true;
|
|
|
|
*txt = otxt;
|
2020-05-05 12:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:54:23 +02:00
|
|
|
Ok(TransformNoteOutput {
|
|
|
|
changed,
|
|
|
|
generate_cards: true,
|
|
|
|
mark_modified: true,
|
|
|
|
})
|
|
|
|
})
|
2020-05-05 12:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use crate::{collection::open_test_collection, decks::DeckID};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn findreplace() -> Result<()> {
|
|
|
|
let mut col = open_test_collection();
|
|
|
|
|
|
|
|
let nt = col.get_notetype_by_name("Basic")?.unwrap();
|
|
|
|
let mut note = nt.new_note();
|
2021-03-02 10:02:00 +01:00
|
|
|
note.set_field(0, "one aaa")?;
|
|
|
|
note.set_field(1, "two aaa")?;
|
2020-05-05 12:50:17 +02:00
|
|
|
col.add_note(&mut note, DeckID(1))?;
|
|
|
|
|
|
|
|
let nt = col.get_notetype_by_name("Cloze")?.unwrap();
|
|
|
|
let mut note2 = nt.new_note();
|
2021-03-02 10:02:00 +01:00
|
|
|
note2.set_field(0, "three aaa")?;
|
2020-05-05 12:50:17 +02:00
|
|
|
col.add_note(&mut note2, DeckID(1))?;
|
|
|
|
|
2020-05-19 03:27:02 +02:00
|
|
|
let nids = col.search_notes("")?;
|
2020-05-06 12:06:42 +02:00
|
|
|
let cnt = col.find_and_replace(nids.clone(), "(?i)AAA", "BBB", None)?;
|
2020-05-05 12:50:17 +02:00
|
|
|
assert_eq!(cnt, 2);
|
|
|
|
|
|
|
|
let note = col.storage.get_note(note.id)?.unwrap();
|
|
|
|
// but the update should be limited to the specified field when it was available
|
2021-03-02 10:02:00 +01:00
|
|
|
assert_eq!(¬e.fields()[..], &["one BBB", "two BBB"]);
|
2020-05-05 12:50:17 +02:00
|
|
|
|
|
|
|
let note2 = col.storage.get_note(note2.id)?.unwrap();
|
2021-03-02 10:02:00 +01:00
|
|
|
assert_eq!(¬e2.fields()[..], &["three BBB", ""]);
|
2020-05-05 12:50:17 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
col.storage.field_names_for_notes(&nids)?,
|
2020-05-07 00:31:49 +02:00
|
|
|
vec![
|
|
|
|
"Back".to_string(),
|
|
|
|
"Back Extra".into(),
|
|
|
|
"Front".into(),
|
|
|
|
"Text".into()
|
|
|
|
]
|
2020-05-05 12:50:17 +02:00
|
|
|
);
|
2020-08-26 10:56:57 +02:00
|
|
|
let cnt = col.find_and_replace(nids, "BBB", "ccc", Some("Front".into()))?;
|
2020-05-05 12:50:17 +02:00
|
|
|
// still 2, as the caller is expected to provide only note ids that have
|
|
|
|
// that field, and if we can't find the field we fall back on all fields
|
|
|
|
assert_eq!(cnt, 2);
|
|
|
|
|
|
|
|
let note = col.storage.get_note(note.id)?.unwrap();
|
|
|
|
// but the update should be limited to the specified field when it was available
|
2021-03-02 10:02:00 +01:00
|
|
|
assert_eq!(¬e.fields()[..], &["one ccc", "two BBB"]);
|
2020-05-05 12:50:17 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|