Crates.io | varurl |
lib.rs | varurl |
version | |
source | src |
created_at | 2024-10-27 06:11:46.028809 |
updated_at | 2024-11-02 01:21:12.250364 |
description | varurl |
homepage | https://github.com/i18n-site/rust/tree/main/varurl |
repository | https://github.com/i18n-site/rust.git |
max_upload_size | |
id | 1424368 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
use mdli::parse;
use varurl::VarUrl;
fn create_varurl() -> VarUrl {
VarUrl::new(["https://i18n.com/"])
}
const FROM_LANG: &str = "/zh/";
const TO_LANG: &str = "/en/";
pub fn from_to(_pos: usize) -> Option<(&'static str, &'static str)> {
Some((FROM_LANG, TO_LANG))
}
#[test]
fn test() {
let input = r#"# Main Title
## Section 1\nHere's a paragraph with an ![inline image](https://i18n.com/zh/inline.png) and a [link](https://i18n.com/test/zh/doc.md) mixed in
"#;
let mut mdli = parse(input);
let varurl = create_varurl();
varurl.replace(&mut mdli, |_| Some((FROM_LANG, TO_LANG)));
let result = mdli.join();
assert_eq!(result, input.replace(FROM_LANG, TO_LANG));
let input = r#"
```markdown
![inline image](https://i18n.com/zh/inline.png)
"#; let mut mdli = parse(input); let varurl = create_varurl(); varurl.replace(&mut mdli, from_to); let result = mdli.join(); assert_eq!(result, input); }
fn test_replace(title: &str, prefixes: &[&str], input: &str, expected: &str) { let varurl = VarUrl::new(prefixes); let mut mdli = parse(input); varurl.replace(&mut mdli, from_to); let result = mdli.join();
if result != expected { assert_eq!( result, expected, "\n❌ {}\n预期 {:?}\n实际 {:?}\n", title, expected, result ); } }
#[test]
fn test_all_cases() {
let test_cases: &[(&str, &[&str], &str, &str)] = &[
// 1. 基础链接测试
(
"简单链接测试",
&["https://i18n.com/"],
"文档",
"文档",
),
(
"简单图片测试",
&["https://i18n.com/"],
"",
"",
),
// 2. 多域名测试
(
"多域名替换测试",
&[
"https://i18n.com/",
"https://docs.i18n.com/",
"https://img.i18n.com/",
"//cdn.i18n.com/",
"/docs/",
],
r#"主站点
文档
本地文档"#,
r#"主站点
文档
本地文档"#,
),
// 3. 不匹配测试
(
"不匹配域名测试",
&["https://i18n.com/"],
r#"其他站点
错误前缀
正确链接
无语言"#,
r#"其他站点
错误前缀
正确链接
无语言"#,
),
// 4. 边界情况测试
(
"URL边界情况测试",
&["https://i18n.com/", "/docs/"],
r#"[带空格](https://i18n.com/zh/has space.md)
带括号
带引号
带井号
带问号
[未闭合](https://i18n.com/zh/unclosed.md"#,
r#"[带空格](https://i18n.com/en/has space.md)
带括号
带引号
带井号
带问号
未闭合,
// 5. HTML属性测试
(
"HTML属性测试",
&["https://i18n.com/"],
r#"
"#,
r#"
"#,
),
// 6. 混合内容测试
(
"混合内容测试",
&["https://i18n.com/", "/docs/"],
r#"# 标题
链接和
代码中的[链接](https://i18n.com/zh/code.md)
引用中的链接"#, r#"# 标题 链接和
代码中的[链接](https://i18n.com/zh/code.md)
引用中的链接"#, ), // 7. 代码块测试 ( "代码块测试", &["https://i18n.com/"], "```markdown 代码块中的链接
[普通链接](https://i18n.com/zh/normal.md)
[xx](https://i18n.com/zh/indent.md)
",
"```markdown
[代码块中的链接](https://i18n.com/zh/code.md)
// 代码中的链接不替换
const url = 'https://i18n.com/zh/code.md';
表格 | 链接 |
---|---|
文档 | 链接1 |
图片 |
引用
嵌套引用", "--- title: 测试文档
// 代码中的链接不替换
const url = 'https://i18n.com/zh/code.md';
表格 | 链接 |
---|---|
文档 | 链接1 |
图片 |
引用
嵌套引用", ), ]; for (title, prefixes, input, expected) in test_cases.iter() { test_replace(title, prefixes, input, expected); } }
## About
This project is an open-source component of [i18n.site ⋅ Internationalization Solution](https://i18n.site).
* [i18 : MarkDown Command Line Translation Tool](https://i18n.site/i18)
The translation perfectly maintains the Markdown format.
It recognizes file changes and only translates the modified files.
The translated Markdown content is editable; if you modify the original text and translate it again, manually edited translations will not be overwritten (as long as the original text has not been changed).
* [i18n.site : MarkDown Multi-language Static Site Generator](https://i18n.site/i18n.site)
Optimized for a better reading experience
## 关于
本项目为 [i18n.site ⋅ 国际化解决方案](https://i18n.site) 的开源组件。
* [i18 : MarkDown命令行翻译工具](https://i18n.site/i18)
翻译能够完美保持 Markdown 的格式。能识别文件的修改,仅翻译有变动的文件。
Markdown 翻译内容可编辑;如果你修改原文并再次机器翻译,手动修改过的翻译不会被覆盖(如果这段原文没有被修改)。
* [i18n.site : MarkDown多语言静态站点生成器](https://i18n.site/i18n.site) 为阅读体验而优化。