Skip to content

Commit 7bebcad

Browse files
Some documentation and reduce copy/paste (#3917)
* NF: document that cloze number are kept as they are in the field I needed to know because {{c1 generate card 0 for example. And storing the card ordinal would have been another consistent choice. * NF: introduce method that return the cloze number in fields This slightly reduce code duplication. * Apply suggestions from code review
1 parent fe2c151 commit 7bebcad

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

rslib/src/cloze.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod mathjax_caps {
4343

4444
#[derive(Debug)]
4545
enum Token<'a> {
46+
// The parameter is the cloze number as is appears in the field content.
4647
OpenCloze(u16),
4748
Text(&'a str),
4849
CloseCloze,
@@ -114,6 +115,7 @@ enum TextOrCloze<'a> {
114115

115116
#[derive(Debug)]
116117
struct ExtractedCloze<'a> {
118+
// `ordinal` is the cloze number as is appears in the field content.
117119
ordinal: u16,
118120
nodes: Vec<TextOrCloze<'a>>,
119121
hint: Option<&'a str>,
@@ -409,12 +411,14 @@ pub fn expand_clozes_to_reveal_latex(text: &str) -> String {
409411
buf
410412
}
411413

414+
// Whether `text` contains any cloze number above 0
412415
pub(crate) fn contains_cloze(text: &str) -> bool {
413416
parse_text_with_clozes(text)
414417
.iter()
415418
.any(|node| matches!(node, TextOrCloze::Cloze(e) if e.ordinal != 0))
416419
}
417420

421+
/// Returns the set of cloze number as they appear in the fields's content.
418422
pub fn cloze_numbers_in_string(html: &str) -> HashSet<u16> {
419423
let mut set = HashSet::with_capacity(4);
420424
add_cloze_numbers_in_string(html, &mut set);
@@ -432,11 +436,21 @@ fn add_cloze_numbers_in_text_with_clozes(nodes: &[TextOrCloze], set: &mut HashSe
432436
}
433437
}
434438

439+
/// Add to `set` the cloze numbers as they appear in `field`.
435440
#[allow(clippy::implicit_hasher)]
436441
pub fn add_cloze_numbers_in_string(field: &str, set: &mut HashSet<u16>) {
437442
add_cloze_numbers_in_text_with_clozes(&parse_text_with_clozes(field), set)
438443
}
439444

445+
/// The set of cloze numbers as they appear in any of the fields from `fields`.
446+
pub fn cloze_number_in_fields(fields: impl IntoIterator<Item: AsRef<str>>) -> HashSet<u16> {
447+
let mut set = HashSet::with_capacity(4);
448+
for field in fields {
449+
add_cloze_numbers_in_string(field.as_ref(), &mut set);
450+
}
451+
set
452+
}
453+
440454
fn strip_html_inside_mathjax(text: &str) -> Cow<str> {
441455
MATHJAX.replace_all(text, |caps: &Captures| -> String {
442456
format!(

rslib/src/notes/service.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright: Ankitects Pty Ltd and contributors
22
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
3-
use std::collections::HashSet;
4-
5-
use crate::cloze::add_cloze_numbers_in_string;
3+
use crate::cloze::cloze_number_in_fields;
64
use crate::collection::Collection;
75
use crate::decks::DeckId;
86
use crate::error;
@@ -128,10 +126,7 @@ impl crate::services::NotesService for Collection {
128126
&mut self,
129127
note: anki_proto::notes::Note,
130128
) -> error::Result<anki_proto::notes::ClozeNumbersInNoteResponse> {
131-
let mut set = HashSet::with_capacity(4);
132-
for field in &note.fields {
133-
add_cloze_numbers_in_string(field, &mut set);
134-
}
129+
let set = cloze_number_in_fields(note.fields);
135130
Ok(anki_proto::notes::ClozeNumbersInNoteResponse {
136131
numbers: set.into_iter().map(|n| n as u32).collect(),
137132
})

rslib/src/notetype/cardgen.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rand::Rng;
1111
use rand::SeedableRng;
1212

1313
use super::Notetype;
14-
use crate::cloze::add_cloze_numbers_in_string;
14+
use crate::cloze::cloze_number_in_fields;
1515
use crate::notetype::NotetypeKind;
1616
use crate::prelude::*;
1717
use crate::template::ParsedTemplate;
@@ -148,10 +148,7 @@ impl<N: Deref<Target = Notetype>> CardGenContext<N> {
148148
extracted: &ExtractedCardInfo,
149149
) -> Vec<CardToGenerate> {
150150
// gather all cloze numbers
151-
let mut set = HashSet::with_capacity(4);
152-
for field in note.fields() {
153-
add_cloze_numbers_in_string(field, &mut set);
154-
}
151+
let set = cloze_number_in_fields(note.fields());
155152
set.into_iter()
156153
.filter_map(|cloze_ord| {
157154
let card_ord = cloze_ord.saturating_sub(1).min(499);

rslib/src/template.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use nom::combinator::map;
1515
use nom::sequence::delimited;
1616
use regex::Regex;
1717

18-
use crate::cloze::add_cloze_numbers_in_string;
18+
use crate::cloze::cloze_number_in_fields;
1919
use crate::error::AnkiError;
2020
use crate::error::Result;
2121
use crate::error::TemplateError;
@@ -673,11 +673,7 @@ pub fn render_card(
673673
}
674674

675675
fn cloze_is_empty(field_map: &HashMap<&str, Cow<str>>, card_ord: u16) -> bool {
676-
let mut set = HashSet::with_capacity(4);
677-
for field in field_map.values() {
678-
add_cloze_numbers_in_string(field.as_ref(), &mut set);
679-
}
680-
!set.contains(&(card_ord + 1))
676+
!cloze_number_in_fields(field_map.values()).contains(&(card_ord + 1))
681677
}
682678

683679
// Field requirements

0 commit comments

Comments
 (0)