From e68a40f13e1fbfb57e4625959ad8f54a04216bce Mon Sep 17 00:00:00 2001 From: Andreas Reis Date: Tue, 10 Nov 2020 14:50:17 +0100 Subject: [PATCH] cleanup / renames MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・ soundRegexps → sound_regexps ・ htmlRegexps → html_media_regexps ・ HTML_TAGS → HTML_MEDIA_TAGS ・ escapeImages → escape_media_filenames + alias ・ strip_html_preserving_image_filenames → strip_html_preserving_media_filenames --- pylib/anki/media.py | 14 +++++++++----- pylib/tests/test_media.py | 2 +- qt/aqt/editor.py | 11 +++++++---- qt/aqt/main.py | 2 +- qt/aqt/utils.py | 2 +- rslib/src/notes.rs | 12 ++++++------ rslib/src/search/sqlwriter.rs | 4 ++-- rslib/src/text.rs | 16 ++++++++-------- 8 files changed, 35 insertions(+), 28 deletions(-) diff --git a/pylib/anki/media.py b/pylib/anki/media.py index 28abac172..2d9a2478e 100644 --- a/pylib/anki/media.py +++ b/pylib/anki/media.py @@ -33,8 +33,8 @@ def media_paths_from_col_path(col_path: str) -> Tuple[str, str]: class MediaManager: - soundRegexps = [r"(?i)(\[sound:(?P[^]]+)\])"] - htmlRegexps = [ + sound_regexps = [r"(?i)(\[sound:(?P[^]]+)\])"] + html_media_regexps = [ # src element quoted case r"(?i)(<[img|audio][^>]* src=(?P[\"'])(?P[^>]+?)(?P=str)[^>]*>)", # unquoted case @@ -44,7 +44,7 @@ class MediaManager: # unquoted case r"(?i)(]* data=(?!['\"])(?P[^ >]+)[^>]*?>)", ] - regexps = soundRegexps + htmlRegexps + regexps = sound_regexps + html_media_regexps def __init__(self, col: anki.collection.Collection, server: bool) -> None: self.col = col.weakref() @@ -163,7 +163,11 @@ class MediaManager: return txt def escapeImages(self, string: str, unescape: bool = False) -> str: - "Apply or remove percent encoding to image filenames." + "escape_media_filenames alias for compatibility with add-ons." + return self.escape_media_filenames(string, unescape) + + def escape_media_filenames(self, string: str, unescape: bool = False) -> str: + "Apply or remove percent encoding to filenames in html tags (audio, image, object)." fn: Callable if unescape: fn = urllib.parse.unquote @@ -177,7 +181,7 @@ class MediaManager: return tag return tag.replace(fname, fn(fname)) - for reg in self.htmlRegexps: + for reg in self.html_media_regexps: string = re.sub(reg, repl, string) return string diff --git a/pylib/tests/test_media.py b/pylib/tests/test_media.py index 5f17af373..2d10eab38 100644 --- a/pylib/tests/test_media.py +++ b/pylib/tests/test_media.py @@ -46,7 +46,7 @@ def test_strings(): assert sp("aoeu") == "aoeu" assert sp("aoeu[sound:foo.mp3]aoeu") == "aoeuaoeu" assert sp("aoeu") == "aoeu" - es = col.media.escapeImages + es = col.media.escape_media_filenames assert es("aoeu") == "aoeu" assert es("") == "" assert es('') == '' diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index ce5f044b1..47df8fb66 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -450,7 +450,8 @@ class Editor: return data = [ - (fld, self.mw.col.media.escapeImages(val)) for fld, val in self.note.items() + (fld, self.mw.col.media.escape_media_filenames(val)) + for fld, val in self.note.items() ] self.widget.show() self.updateTags() @@ -547,11 +548,13 @@ class Editor: if html.find(">") > -1: # filter html through beautifulsoup so we can strip out things like a # leading - html_escaped = self.mw.col.media.escapeImages(html) + html_escaped = self.mw.col.media.escape_media_filenames(html) with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) html_escaped = str(BeautifulSoup(html_escaped, "html.parser")) - html = self.mw.col.media.escapeImages(html_escaped, unescape=True) + html = self.mw.col.media.escape_media_filenames( + html_escaped, unescape=True + ) self.note.fields[field] = html if not self.addMode: self.note.flush() @@ -1231,7 +1234,7 @@ def remove_null_bytes(txt, editor): def reverse_url_quoting(txt, editor): # reverse the url quoting we added to get images to display - return editor.mw.col.media.escapeImages(txt, unescape=True) + return editor.mw.col.media.escape_media_filenames(txt, unescape=True) gui_hooks.editor_will_use_font_for_field.append(fontMungeHack) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index a20ea514e..405b16f08 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -483,7 +483,7 @@ close the profile or restart Anki.""" return anki.sound.strip_av_refs(text) def prepare_card_text_for_display(self, text: str) -> str: - text = self.col.media.escapeImages(text) + text = self.col.media.escape_media_filenames(text) text = self._add_play_buttons(text) return text diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 98e8d6148..35c0f948c 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -548,7 +548,7 @@ def restore_combo_history(comboBox: QComboBox, name: str): def mungeQA(col, txt): print("mungeQA() deprecated; use mw.prepare_card_text_for_display()") - txt = col.media.escapeImages(txt) + txt = col.media.escape_media_filenames(txt) return txt diff --git a/rslib/src/notes.rs b/rslib/src/notes.rs index 94eaface0..e20bb5eb6 100644 --- a/rslib/src/notes.rs +++ b/rslib/src/notes.rs @@ -10,7 +10,7 @@ use crate::{ err::{AnkiError, Result}, notetype::{CardGenContext, NoteField, NoteType, NoteTypeID}, template::field_is_empty, - text::{ensure_string_in_nfc, normalize_to_nfc, strip_html_preserving_image_filenames}, + text::{ensure_string_in_nfc, normalize_to_nfc, strip_html_preserving_media_filenames}, timestamp::TimestampSecs, types::Usn, }; @@ -100,12 +100,12 @@ impl Note { } } - let field1_nohtml = strip_html_preserving_image_filenames(&self.fields()[0]); + let field1_nohtml = strip_html_preserving_media_filenames(&self.fields()[0]); let checksum = field_checksum(field1_nohtml.as_ref()); let sort_field = if nt.config.sort_field_idx == 0 { field1_nohtml } else { - strip_html_preserving_image_filenames( + strip_html_preserving_media_filenames( self.fields .get(nt.config.sort_field_idx as usize) .map(AsRef::as_ref) @@ -208,7 +208,7 @@ impl From for Note { } } -/// Text must be passed to strip_html_preserving_image_filenames() by +/// Text must be passed to strip_html_preserving_media_filenames() by /// caller prior to passing in here. pub(crate) fn field_checksum(text: &str) -> u32 { let digest = sha1::Sha1::from(text).digest().bytes(); @@ -429,7 +429,7 @@ impl Collection { } else { field1.into() }; - let stripped = strip_html_preserving_image_filenames(&field1); + let stripped = strip_html_preserving_media_filenames(&field1); if stripped.trim().is_empty() { Ok(DuplicateState::Empty) } else { @@ -438,7 +438,7 @@ impl Collection { self.storage .note_fields_by_checksum(note.id, note.notetype_id, csum)? { - if strip_html_preserving_image_filenames(&field) == stripped { + if strip_html_preserving_media_filenames(&field) == stripped { return Ok(DuplicateState::Duplicate); } } diff --git a/rslib/src/search/sqlwriter.rs b/rslib/src/search/sqlwriter.rs index 611f3adce..28a763ed3 100644 --- a/rslib/src/search/sqlwriter.rs +++ b/rslib/src/search/sqlwriter.rs @@ -10,7 +10,7 @@ use crate::{ notes::field_checksum, notetype::NoteTypeID, text::{matches_wildcard, text_to_re}, - text::{normalize_to_nfc, strip_html_preserving_image_filenames, without_combining}, + text::{normalize_to_nfc, strip_html_preserving_media_filenames, without_combining}, timestamp::TimestampSecs, }; use lazy_static::lazy_static; @@ -424,7 +424,7 @@ impl SqlWriter<'_> { } fn write_dupes(&mut self, ntid: NoteTypeID, text: &str) { - let text_nohtml = strip_html_preserving_image_filenames(text); + let text_nohtml = strip_html_preserving_media_filenames(text); let csum = field_checksum(text_nohtml.as_ref()); write!( self.sql, diff --git a/rslib/src/text.rs b/rslib/src/text.rs index 6d8fa5322..f29d251dd 100644 --- a/rslib/src/text.rs +++ b/rslib/src/text.rs @@ -32,7 +32,7 @@ lazy_static! { )) .unwrap(); - static ref HTML_TAGS: Regex = Regex::new( + static ref HTML_MEDIA_TAGS: Regex = Regex::new( r#"(?xsi) # the start of the image, audio, or object tag <\b(?:img|audio|object)\b[^>]+\b(?:src|data)\b= @@ -149,7 +149,7 @@ pub(crate) struct MediaRef<'a> { pub(crate) fn extract_media_refs(text: &str) -> Vec { let mut out = vec![]; - for caps in HTML_TAGS.captures_iter(text) { + for caps in HTML_MEDIA_TAGS.captures_iter(text) { let fname = caps .get(1) .or_else(|| caps.get(2)) @@ -213,8 +213,8 @@ fn tts_tag_from_string<'a>(field_text: &'a str, args: &'a str) -> AVTag { } } -pub fn strip_html_preserving_image_filenames(html: &str) -> Cow { - let without_fnames = HTML_TAGS.replace_all(html, r" ${1}${2}${3} "); +pub fn strip_html_preserving_media_filenames(html: &str) -> Cow { + let without_fnames = HTML_MEDIA_TAGS.replace_all(html, r" ${1}${2}${3} "); let without_html = HTML.replace_all(&without_fnames, ""); // no changes? if let Cow::Borrowed(b) = without_html { @@ -306,7 +306,7 @@ mod test { use super::matches_wildcard; use crate::text::without_combining; use crate::text::{ - extract_av_tags, strip_av_tags, strip_html, strip_html_preserving_image_filenames, AVTag, + extract_av_tags, strip_av_tags, strip_html, strip_html_preserving_media_filenames, AVTag, }; use std::borrow::Cow; @@ -317,14 +317,14 @@ mod test { assert_eq!(strip_html("some"), "some"); assert_eq!( - strip_html_preserving_image_filenames(""), + strip_html_preserving_media_filenames(""), " foo.jpg " ); assert_eq!( - strip_html_preserving_image_filenames(""), + strip_html_preserving_media_filenames(""), " foo.jpg " ); - assert_eq!(strip_html_preserving_image_filenames(""), ""); + assert_eq!(strip_html_preserving_media_filenames(""), ""); } #[test]