This commit is contained in:
llama 2025-11-06 15:21:07 +08:00 committed by GitHub
commit 353b55f7a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 10 deletions

View file

@ -1,6 +1,7 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// 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
use std::collections::HashSet;
use std::io::BufRead; use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read; use std::io::Read;
@ -106,6 +107,8 @@ struct ColumnContext {
notetype_column: Option<usize>, notetype_column: Option<usize>,
/// Source column indices for the fields of a notetype /// Source column indices for the fields of a notetype
field_source_columns: FieldSourceColumns, field_source_columns: FieldSourceColumns,
/// Metadata column indices (1-based)
meta_columns: HashSet<usize>,
/// How fields are converted to strings. Used for escaping HTML if /// How fields are converted to strings. Used for escaping HTML if
/// appropriate. /// appropriate.
stringify: fn(&str) -> String, stringify: fn(&str) -> String,
@ -119,6 +122,7 @@ impl ColumnContext {
deck_column: metadata.deck()?.column(), deck_column: metadata.deck()?.column(),
notetype_column: metadata.notetype()?.column(), notetype_column: metadata.notetype()?.column(),
field_source_columns: metadata.field_source_columns()?, field_source_columns: metadata.field_source_columns()?,
meta_columns: metadata.meta_columns(),
stringify: stringify_fn(metadata.is_html), stringify: stringify_fn(metadata.is_html),
}) })
} }
@ -166,11 +170,19 @@ impl ColumnContext {
} }
fn gather_note_fields(&self, record: &csv::StringRecord) -> Vec<Option<String>> { fn gather_note_fields(&self, record: &csv::StringRecord) -> Vec<Option<String>> {
let stringify = self.stringify; let op = |i| record.get(i - 1).map(self.stringify);
self.field_source_columns if !self.field_source_columns.is_empty() {
.iter() self.field_source_columns
.map(|opt| opt.and_then(|idx| record.get(idx - 1)).map(stringify)) .iter()
.collect() .map(|opt| opt.and_then(op))
.collect()
} else {
// notetype column provided, assume all non-metadata columns are notetype fields
(1..=record.len())
.filter(|i| !self.meta_columns.contains(i))
.map(op)
.collect()
}
} }
} }

View file

@ -291,11 +291,8 @@ impl CsvMetadataHelpers for CsvMetadata {
.map(|&i| (i > 0).then_some(i as usize)) .map(|&i| (i > 0).then_some(i as usize))
.collect(), .collect(),
CsvNotetype::NotetypeColumn(_) => { CsvNotetype::NotetypeColumn(_) => {
let meta_columns = self.meta_columns(); // each row's notetype could have varying number of fields
(1..self.column_labels.len() + 1) vec![]
.filter(|idx| !meta_columns.contains(idx))
.map(Some)
.collect()
} }
}) })
} }