mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
native struct for CardTemplate
This commit is contained in:
parent
9080f602b1
commit
a17ddfdccd
6 changed files with 75 additions and 29 deletions
|
@ -513,7 +513,7 @@ message CardTemplateConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
message CardTemplate {
|
message CardTemplate {
|
||||||
uint32 ord = 1;
|
OptionalUInt32 ord = 1;
|
||||||
string name = 2;
|
string name = 2;
|
||||||
uint32 mtime_secs = 3;
|
uint32 mtime_secs = 3;
|
||||||
sint32 usn = 4;
|
sint32 usn = 4;
|
||||||
|
|
|
@ -4,14 +4,16 @@
|
||||||
mod fields;
|
mod fields;
|
||||||
mod schema11;
|
mod schema11;
|
||||||
mod stock;
|
mod stock;
|
||||||
|
mod templates;
|
||||||
|
|
||||||
pub use crate::backend_proto::{
|
pub use crate::backend_proto::{
|
||||||
card_requirement::CardRequirementKind, CardRequirement, CardTemplate, CardTemplateConfig,
|
card_requirement::CardRequirementKind, CardRequirement, CardTemplateConfig, NoteFieldConfig,
|
||||||
NoteFieldConfig, NoteType as NoteTypeProto, NoteTypeConfig, NoteTypeKind,
|
NoteType as NoteTypeProto, NoteTypeConfig, NoteTypeKind,
|
||||||
};
|
};
|
||||||
pub use fields::NoteField;
|
pub use fields::NoteField;
|
||||||
pub use schema11::{CardTemplateSchema11, NoteFieldSchema11, NoteTypeSchema11};
|
pub use schema11::{CardTemplateSchema11, NoteFieldSchema11, NoteTypeSchema11};
|
||||||
pub use stock::all_stock_notetypes;
|
pub use stock::all_stock_notetypes;
|
||||||
|
pub use templates::CardTemplate;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
define_newtype,
|
define_newtype,
|
||||||
|
@ -112,7 +114,7 @@ impl NoteType {
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(ord, tmpl)| {
|
.map(|(ord, tmpl)| {
|
||||||
let conf = tmpl.config.as_ref().unwrap();
|
let conf = &tmpl.config;
|
||||||
let normalized = without_legacy_template_directives(&conf.q_format);
|
let normalized = without_legacy_template_directives(&conf.q_format);
|
||||||
if let Ok(tmpl) = ParsedTemplate::from_text(normalized.as_ref()) {
|
if let Ok(tmpl) = ParsedTemplate::from_text(normalized.as_ref()) {
|
||||||
let mut req = match tmpl.requirements(&field_map) {
|
let mut req = match tmpl.requirements(&field_map) {
|
||||||
|
@ -167,15 +169,7 @@ impl NoteType {
|
||||||
S2: Into<String>,
|
S2: Into<String>,
|
||||||
S3: Into<String>,
|
S3: Into<String>,
|
||||||
{
|
{
|
||||||
let mut config = CardTemplateConfig::default();
|
self.templates.push(CardTemplate::new(name, qfmt, afmt));
|
||||||
config.q_format = qfmt.into();
|
|
||||||
config.a_format = afmt.into();
|
|
||||||
|
|
||||||
let mut tmpl = CardTemplate::default();
|
|
||||||
tmpl.name = name.into();
|
|
||||||
tmpl.config = Some(config);
|
|
||||||
|
|
||||||
self.templates.push(tmpl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn prepare_for_adding(&mut self) {
|
pub(crate) fn prepare_for_adding(&mut self) {
|
||||||
|
@ -194,7 +188,7 @@ impl From<NoteType> for NoteTypeProto {
|
||||||
usn: nt.usn.0,
|
usn: nt.usn.0,
|
||||||
config: Some(nt.config),
|
config: Some(nt.config),
|
||||||
fields: nt.fields.into_iter().map(Into::into).collect(),
|
fields: nt.fields.into_iter().map(Into::into).collect(),
|
||||||
templates: nt.templates,
|
templates: nt.templates.into_iter().map(Into::into).collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,11 +276,11 @@ pub struct CardTemplateSchema11 {
|
||||||
impl From<CardTemplateSchema11> for CardTemplate {
|
impl From<CardTemplateSchema11> for CardTemplate {
|
||||||
fn from(t: CardTemplateSchema11) -> Self {
|
fn from(t: CardTemplateSchema11) -> Self {
|
||||||
CardTemplate {
|
CardTemplate {
|
||||||
ord: t.ord as u32,
|
ord: Some(t.ord as u32),
|
||||||
name: t.name,
|
name: t.name,
|
||||||
mtime_secs: 0,
|
mtime_secs: TimestampSecs(0),
|
||||||
usn: 0,
|
usn: Usn(0),
|
||||||
config: Some(CardTemplateConfig {
|
config: CardTemplateConfig {
|
||||||
q_format: t.qfmt,
|
q_format: t.qfmt,
|
||||||
a_format: t.afmt,
|
a_format: t.afmt,
|
||||||
q_format_browser: t.bqfmt,
|
q_format_browser: t.bqfmt,
|
||||||
|
@ -289,17 +289,19 @@ impl From<CardTemplateSchema11> for CardTemplate {
|
||||||
browser_font_name: t.bfont,
|
browser_font_name: t.bfont,
|
||||||
browser_font_size: t.bsize as u32,
|
browser_font_size: t.bsize as u32,
|
||||||
other: other_to_bytes(&t.other),
|
other: other_to_bytes(&t.other),
|
||||||
}),
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fixme: make sure we don't call this when ord not set
|
||||||
|
|
||||||
impl From<CardTemplate> for CardTemplateSchema11 {
|
impl From<CardTemplate> for CardTemplateSchema11 {
|
||||||
fn from(p: CardTemplate) -> Self {
|
fn from(p: CardTemplate) -> Self {
|
||||||
let conf = p.config.unwrap();
|
let conf = p.config;
|
||||||
CardTemplateSchema11 {
|
CardTemplateSchema11 {
|
||||||
name: p.name,
|
name: p.name,
|
||||||
ord: p.ord as u16,
|
ord: p.ord.unwrap() as u16,
|
||||||
qfmt: conf.q_format,
|
qfmt: conf.q_format,
|
||||||
afmt: conf.a_format,
|
afmt: conf.a_format,
|
||||||
bqfmt: conf.q_format_browser,
|
bqfmt: conf.q_format_browser,
|
||||||
|
|
|
@ -67,7 +67,7 @@ pub(crate) fn basic_typing(i18n: &I18n) -> NoteType {
|
||||||
nt.name = i18n.tr(TR::NotetypesBasicTypeAnswerName).into();
|
nt.name = i18n.tr(TR::NotetypesBasicTypeAnswerName).into();
|
||||||
let front = i18n.tr(TR::NotetypesFrontField);
|
let front = i18n.tr(TR::NotetypesFrontField);
|
||||||
let back = i18n.tr(TR::NotetypesBackField);
|
let back = i18n.tr(TR::NotetypesBackField);
|
||||||
let tmpl = nt.templates[0].config.as_mut().unwrap();
|
let tmpl = &mut nt.templates[0].config;
|
||||||
tmpl.q_format = format!("{}\n\n{{{{type:{}}}}}", fieldref(front.as_ref()), back);
|
tmpl.q_format = format!("{}\n\n{{{{type:{}}}}}", fieldref(front.as_ref()), back);
|
||||||
tmpl.a_format = format!(
|
tmpl.a_format = format!(
|
||||||
"{}\n\n<hr id=answer>\n\n{{{{type:{}}}}}",
|
"{}\n\n<hr id=answer>\n\n{{{{type:{}}}}}",
|
||||||
|
@ -101,7 +101,7 @@ pub(crate) fn basic_optional_reverse(i18n: &I18n) -> NoteType {
|
||||||
nt.name = i18n.tr(TR::NotetypesBasicOptionalReversedName).into();
|
nt.name = i18n.tr(TR::NotetypesBasicOptionalReversedName).into();
|
||||||
let addrev = i18n.tr(TR::NotetypesAddReverseField);
|
let addrev = i18n.tr(TR::NotetypesAddReverseField);
|
||||||
nt.add_field(addrev.as_ref());
|
nt.add_field(addrev.as_ref());
|
||||||
let tmpl = nt.templates[1].config.as_mut().unwrap();
|
let tmpl = &mut nt.templates[1].config;
|
||||||
tmpl.q_format = format!("{{{{#{}}}}}{}{{{{/{}}}}}", addrev, tmpl.q_format, addrev);
|
tmpl.q_format = format!("{{{{#{}}}}}{}{{{{/{}}}}}", addrev, tmpl.q_format, addrev);
|
||||||
nt.prepare_for_adding();
|
nt.prepare_for_adding();
|
||||||
nt
|
nt
|
||||||
|
|
54
rslib/src/notetype/templates.rs
Normal file
54
rslib/src/notetype/templates.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
backend_proto::{CardTemplate as CardTemplateProto, CardTemplateConfig, OptionalUInt32},
|
||||||
|
timestamp::TimestampSecs,
|
||||||
|
types::Usn,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct CardTemplate {
|
||||||
|
pub ord: Option<u32>,
|
||||||
|
pub mtime_secs: TimestampSecs,
|
||||||
|
pub usn: Usn,
|
||||||
|
pub name: String,
|
||||||
|
pub config: CardTemplateConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CardTemplate> for CardTemplateProto {
|
||||||
|
fn from(t: CardTemplate) -> Self {
|
||||||
|
CardTemplateProto {
|
||||||
|
ord: t.ord.map(|n| OptionalUInt32 { val: n }),
|
||||||
|
mtime_secs: t.mtime_secs.0 as u32,
|
||||||
|
usn: t.usn.0,
|
||||||
|
name: t.name,
|
||||||
|
config: Some(t.config),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CardTemplate {
|
||||||
|
pub fn new<S1, S2, S3>(name: S1, qfmt: S2, afmt: S3) -> Self
|
||||||
|
where
|
||||||
|
S1: Into<String>,
|
||||||
|
S2: Into<String>,
|
||||||
|
S3: Into<String>,
|
||||||
|
{
|
||||||
|
CardTemplate {
|
||||||
|
ord: None,
|
||||||
|
name: name.into(),
|
||||||
|
mtime_secs: TimestampSecs(0),
|
||||||
|
usn: Usn(0),
|
||||||
|
config: CardTemplateConfig {
|
||||||
|
q_format: qfmt.into(),
|
||||||
|
a_format: afmt.into(),
|
||||||
|
q_format_browser: "".into(),
|
||||||
|
a_format_browser: "".into(),
|
||||||
|
target_deck_id: 0,
|
||||||
|
browser_font_name: "".into(),
|
||||||
|
browser_font_size: 0,
|
||||||
|
other: vec![],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -67,7 +67,7 @@ impl SqliteStorage {
|
||||||
name: row.get(1)?,
|
name: row.get(1)?,
|
||||||
mtime_secs: row.get(2)?,
|
mtime_secs: row.get(2)?,
|
||||||
usn: row.get(3)?,
|
usn: row.get(3)?,
|
||||||
config: Some(config),
|
config,
|
||||||
})
|
})
|
||||||
})?
|
})?
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -130,11 +130,7 @@ impl SqliteStorage {
|
||||||
.prepare_cached(include_str!("update_templates.sql"))?;
|
.prepare_cached(include_str!("update_templates.sql"))?;
|
||||||
for (ord, template) in templates.iter().enumerate() {
|
for (ord, template) in templates.iter().enumerate() {
|
||||||
let mut config_bytes = vec![];
|
let mut config_bytes = vec![];
|
||||||
template
|
template.config.encode(&mut config_bytes)?;
|
||||||
.config
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.encode(&mut config_bytes)?;
|
|
||||||
stmt.execute(params![
|
stmt.execute(params![
|
||||||
ntid,
|
ntid,
|
||||||
ord as u32,
|
ord as u32,
|
||||||
|
|
Loading…
Reference in a new issue