Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A tool for easily extracting front matter out of a string. It is a fast Rust implementation of gray-matter. Parses YAML, JSON, TOML and support for custom parsers. Use it and let me know by giving it a star!

License

NotificationsYou must be signed in to change notification settings

the-alchemists-of-arland/gray-matter-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest VersionDocumentation

gray_matter is a tool for easily extracting front matter out of a string. It is a fast Rust implementation ofgray-matter. It supports the following front matter formats:

  • TOML
  • YAML
  • JSON

It also has anEngine trait interface for implementing your own parsers that work with gray_matter.

Usage

Addgray_matter as a dependency

Append this crate to theCargo.toml:

[dependencies]# other dependencies...gray_matter ="0.2"

Basic parsing

use gray_matter::Matter;use gray_matter::engine::YAML;use serde::Deserialize;constINPUT:&str =r#"---title: gray-matter-rstags:  - gray-matter  - rust---Some excerpt---Other stuff"#;fnmain(){// Select one parser engine, such as YAML, and parse it// into gray_matter's custom data type: `Pod`let matter =Matter::<YAML>::new();let result = matter.parse(INPUT);// You can now inspect the data from gray_matter.assert_eq!(result.content,"Some excerpt\n---\nOther stuff");assert_eq!(result.excerpt,Some("Some excerpt".to_owned()));assert_eq!(result.data.as_ref().unwrap()["title"].as_string(),Ok("gray-matter-rs".to_string()));assert_eq!(result.data.as_ref().unwrap()["tags"][0].as_string(),Ok("gray-matter".to_string()));assert_eq!(result.data.as_ref().unwrap()["tags"][1].as_string(),Ok("rust".to_string()));// The `Pod` data type can be a bit unwieldy, so// you can also deserialize it into a custom struct#[derive(Deserialize,Debug)]structFrontMatter{title:String,tags:Vec<String>}// Deserialize `result` manually:let front_matter:FrontMatter = result.data.unwrap().deserialize().unwrap();println!("{:?}", front_matter);// FrontMatter { title: "gray-matter-rs", tags: ["gray-matter", "rust"] }// ...or skip a step, by using `parse_with_struct`.let result_with_struct = matter.parse_with_struct::<FrontMatter>(INPUT).unwrap();println!("{:?}", result_with_struct.data)// FrontMatter { title: "gray-matter-rs", tags: ["gray-matter", "rust"] }}

Custom Delimiters

The default delimiter is---, both for front matter and excerpts. You can change this by modifiying theMatter struct.

use gray_matter::{Matter,ParsedEntityStruct};use gray_matter::engine::YAML;use serde::Deserialize;fnmain(){letmut matter:Matter<YAML> =Matter::new();    matter.delimiter ="~~~".to_owned();    matter.excerpt_delimiter =Some("<!-- endexcerpt -->".to_owned());#[derive(Deserialize,Debug)]structFrontMatter{abc:String,}let result:ParsedEntityStruct<FrontMatter> = matter.parse_with_struct("~~~\nabc: xyz\n~~~\nfoo\nbar\nbaz\n<!-- endexcerpt -->\ncontent",).unwrap();}

Custom close delimiter

The open and close delimiter are the same by default (---). You can change this by modifiyingclose_delimiter property ofMatter struct

use gray_matter::{Matter,ParsedEntityStruct};use gray_matter::engine::YAML;use serde::Deserialize;fnmain(){letmut matter:Matter<YAML> =Matter::new();    matter.delimiter ="<!--".to_owned();    matter.close_delimiter =Some("-->".to_owned());    matter.excerpt_delimiter =Some("<!-- endexcerpt -->".to_owned());#[derive(Deserialize,Debug)]structFrontMatter{abc:String,}let result:ParsedEntityStruct<FrontMatter> = matter.parse_with_struct("<!--\nabc: xyz\n-->\nfoo\nbar\nbaz\n<!-- endexcerpt -->\ncontent",).unwrap();}

Contributors

Contribution

If you need more parser engines, feel free to create aPR to help me complete this crate.

About

A tool for easily extracting front matter out of a string. It is a fast Rust implementation of gray-matter. Parses YAML, JSON, TOML and support for custom parsers. Use it and let me know by giving it a star!

Topics

Resources

License

Stars

Watchers

Forks

Contributors8

Languages


[8]ページ先頭

©2009-2025 Movatter.jp