feat: 支持在模板中对文本进行截断,避免路径过长错误 (#73)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2024-04-23 23:21:29 +08:00
committed by GitHub
parent badaeed104
commit 2366c36462
2 changed files with 35 additions and 2 deletions
+25 -2
View File
@@ -643,15 +643,38 @@ async fn generate_nfo(serializer: NFOSerializer<'_>, nfo_path: PathBuf) -> Resul
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use handlebars::handlebars_helper;
use super::*; use super::*;
#[test] #[test]
fn test_template_usage() { fn test_template_usage() {
let mut template = handlebars::Handlebars::new(); let mut template = handlebars::Handlebars::new();
let _ = template.register_template_string("video", "{{bvid}}"); handlebars_helper!(truncate: |s: String, len: usize| {
if s.chars().count() > len {
s.chars().take(len).collect::<String>()
} else {
s.to_string()
}
});
template.register_helper("truncate", Box::new(truncate));
let _ = template.register_template_string("video", "test{{bvid}}test");
let _ = template.register_template_string("test_truncate", "哈哈,{{ truncate title 30 }}");
assert_eq!( assert_eq!(
template.render("video", &json!({"bvid": "BV1b5411h7g7"})).unwrap(), template.render("video", &json!({"bvid": "BV1b5411h7g7"})).unwrap(),
"BV1b5411h7g7" "testBV1b5411h7g7test"
);
assert_eq!(
template
.render(
"test_truncate",
&json!({"title": "你说得对,但是 Rust 是由 Mozilla 自主研发的一款全新的编译期格斗游戏。\
编译将发生在一个被称作「Cargo」的构建系统中。在这里,被引用的指针将被授予「生命周期」之力,导引对象安全。\
你将扮演一位名为「Rustacean」的神秘角色, 在与「Rustc」的搏斗中邂逅各种骨骼惊奇的傲娇报错。\
征服她们、通过编译同时,逐步发掘「C++」程序崩溃的真相。"})
)
.unwrap(),
"哈哈,你说得对,但是 Rust 是由 Mozilla 自主研发的一"
); );
} }
} }
+10
View File
@@ -4,6 +4,7 @@ use std::path::Path;
use anyhow::Result; use anyhow::Result;
use entity::*; use entity::*;
use filenamify::filenamify; use filenamify::filenamify;
use handlebars::handlebars_helper;
use migration::OnConflict; use migration::OnConflict;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use quick_xml::events::{BytesCData, BytesText}; use quick_xml::events::{BytesCData, BytesText};
@@ -21,6 +22,14 @@ use crate::config::CONFIG;
pub static TEMPLATE: Lazy<handlebars::Handlebars> = Lazy::new(|| { pub static TEMPLATE: Lazy<handlebars::Handlebars> = Lazy::new(|| {
let mut handlebars = handlebars::Handlebars::new(); let mut handlebars = handlebars::Handlebars::new();
handlebars_helper!(truncate: |s: String, len: usize| {
if s.chars().count() > len {
s.chars().take(len).collect::<String>()
} else {
s.to_string()
}
});
handlebars.register_helper("truncate", Box::new(truncate));
handlebars handlebars
.register_template_string("video", &CONFIG.video_name) .register_template_string("video", &CONFIG.video_name)
.unwrap(); .unwrap();
@@ -468,6 +477,7 @@ impl<'a> NFOSerializer<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[tokio::test] #[tokio::test]
async fn test_generate_nfo() { async fn test_generate_nfo() {
let video = video::Model { let video = video::Model {