3 个稳定版本
1.1.1 | 2022 年 1 月 6 日 |
---|---|
1.1.0 | 2021 年 11 月 20 日 |
1.0.0 | 2021 年 10 月 21 日 |
#1149 in 编码
32KB
380 代码行
Harlaw
将 DSL (Lingvo Dictionary File) 文件转换为 JSON。支持自定义输出格式。
有许多字典以 .dsl 格式提供,但易于消费的格式很少。Harlaw 将 dsl 文件格式化为 json,并提供良好的搜索/替换/删除选项。
Rust 版本的原始 Node.js 库。
安装
将以下内容添加到您的 Cargo.toml
[dependencies]
harlaw = "1.0.0"
用法
Harlaw 可以将 DSL 字典转换为 JSON 文件或内存中的结构。
将 DSL 字典读入内存。
字典可以使用两种默认转换:带标记或不带标记。
// Default methods come with two variations: with or without markup.
use harlaw::{get_dictionary, get_dictionary_without_markup, DictionaryEntry};
// Path to DSL file to transform.
let my_dictionary = "./my-dictionary.dsl";
// Standard method transforms [i], [b] etc tags to their html equilevants <i>, <strong>
let result = get_dictionary(my_dictionary);
// No-markup version. Removes all additional markup.
let no_markup_result = get_no_markup_dictionary();
// Both methods return Result, which either contains Vec<DictionaryEntry> or error message.
let dictionary_content: Vec<DictionaryEntry> = result.unwrap();
let no_markup_dictionary_content: Vec<DictionaryEntry> = no_markup_result.unwrap();
// Do what you want with dictionary data.
从 DSL 文件创建 JSON 文件。
可以创建带两种默认设置的 JSON 文件:带标记或不带标记。
// Default methods comes with two variations: with or without markup.
use harlaw::{to_json, to_json_no_markup};
// Paths to original DSL library & JSON file to be created.
let input = "./my-dictionary.dsl";
let ouput = "./my-dictionary.json";
// Standard method with default markup transforms.
let result = to_json(input, output);
// No-markup version. Removes all additional markup.
let result_no_markup = to_json_no_markup(input, output);
// Both methods return Result -> Either Ok or Err message.
if result.is_ok() {
// JSON was created in output location.
}
自定义转换设置。
如果您有自定义格式化需求,您还可以为转换创建自定义设置对象。它允许用户设置自定义搜索/替换和删除。
自定义设置可以与 JSON 和内存方法一起使用。
// Custom settings methods available for both getters.
use harlaw::{get_dictionary_with_custom_settings, to_json_with_custom_settings};
// Structs used to build custom settings.
use harlaw::{ContentReplace, HarlawSettings};
// Paths to original DSL library & JSON file to be created.
let input = "./my-dictionary.dsl";
let ouput = "./my-dictionary.json";
// Settings accept vectors of removes, and search/replace structs.
let settings = HarlawSettings {
removes: vec!["[m1]", "[m2]", "[/m]", "\t", "my-custom-remove"],
replaces: vec![
ContentReplace {
search: "[b]",
replace: "<TUHTI>",
},
ContentReplace {
search: "[/b]",
replace: "</TUHTI>",
},
ContentReplace {
search: "[i]",
replace: "<VINO>",
},
ContentReplace {
search: "[/i]",
replace: "</VINO>",
}
],
};
// In memory.
let result = get_dictionary_with_custom_settings(input, settings);
// To json.
let json_result = to_json_with_custom_settings(input, output, settings);
// Method returns match their non-custom counterparts.
您还可以将默认设置用作您自己的设置的基。
use harlaw::{get_default_settings, get_no_markup_settings, ContentReplace};
// Use default settings, or no-markup settings as a base.
let mut settings = get_default_settings();
// Append some custom search replace.
settings.replaces.push(
ContentReplace {
search: "foo",
replace: "bar",
}
);
// Append some custom remove
settings.removes.push("baz");
// Use your own settings in one of the custom methods.
名字的含义是什么?
在 G.R.R. Martin 的 "冰与火之歌" 中,有一个名叫 Rodrik Harlaw 的角色,他被戏称为 "The Reader"。这就是我的 Harlaw 所做的;读取别人不关心的事情。
依赖项
~0.7–1.5MB
~34K SLoC