Coder Social home page Coder Social logo

stscoundrel / harlaw-rs Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 41 KB

Transform DSL files to JSON in Rust. Formatting options available for custom output.

Home Page: https://docs.rs/harlaw

License: MIT License

Rust 97.36% ASL 2.64%
dictionary dsl lingvo rust transformer

harlaw-rs's Introduction

Harlaw

Transform DSL (Lingvo Dictionary File) files to JSON. Formatting options available for custom output.

There are many dictionaries available as .dsl, but very few in easily consumable formats. Harlaw formats the dsl files to json with decent search/replace/remove options.

Rust port of original Node.js library.

Install

Add this to your Cargo.toml:

[dependencies]
harlaw = "1.1.1"

Usage

Harlaw can transform DSL dictionaries either to JSON files, or to in-memory structures.

Reading DSL dictionary into memory.

Dictionaries can be read with two default transform: markup, or no markup.

// 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.

Creating JSON file from DSL file.

JSON files can be created with two default settings: markup, or no markup.

// 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.
}

Custom transform settings.

If you have custom formatting needs, you can also create custom settings object for transforms. It allows user to set custom search/replaces and removes.

Custom settings can be used with both JSON & in-memory methods.

// 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.

You can also use default settings as a base for your own settings.

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.

What's in the name?

In G.R.R Martins "A Song Of Ice And Fire", there is a character named Rodrik Harlaw. He is mockingly called "The Reader". That is what my Harlaw does too; reads things no one else cares about.

harlaw-rs's People

Stargazers

 avatar

Watchers

 avatar  avatar

harlaw-rs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.