Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different open and close delimiters #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ fn main() {
}
```

#### Custom close delimiter

The open and close delimiter are the same by default (`---`). You can change this by modifiying `close_delimiter` property of `Matter` struct

```rust
use gray_matter::{Matter, ParsedEntityStruct};
use gray_matter::engine::YAML;
use serde::Deserialize;

fn main() {
let mut 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)]
struct FrontMatter {
abc: String,
}

let result: ParsedEntityStruct<FrontMatter> = matter.parse_with_struct(
"<!--\nabc: xyz\n-->\nfoo\nbar\nbaz\n<!-- endexcerpt -->\ncontent",
).unwrap();
}
```

## Contributors
<a href="https://github.com/the-alchemists-of-arland/gray-matter-rs/graphs/contributors">
<img src="https://contrib.rocks/image?repo=the-alchemists-of-arland/gray-matter-rs" />
Expand Down
40 changes: 39 additions & 1 deletion src/matter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum Part {
/// handles parsing.
pub struct Matter<T: Engine> {
pub delimiter: String,
pub close_delimiter: Option<String>,
pub excerpt_delimiter: Option<String>,
engine: PhantomData<T>,
}
Expand All @@ -27,6 +28,7 @@ impl<T: Engine> Matter<T> {
pub fn new() -> Self {
Self {
delimiter: "---".to_string(),
close_delimiter: None,
excerpt_delimiter: None,
engine: PhantomData,
}
Expand Down Expand Up @@ -69,6 +71,10 @@ impl<T: Engine> Matter<T> {
.clone()
.unwrap_or_else(|| self.delimiter.clone());

let close_delimiter = self
.close_delimiter
.clone()
.unwrap_or_else(|| self.delimiter.clone());
// If first line starts with a delimiter followed by newline, we are looking at front
// matter. Else, we might be looking at an excerpt.
let (mut looking_at, lines) = match input.split_once('\n') {
Expand All @@ -83,7 +89,7 @@ impl<T: Engine> Matter<T> {
let line = line.trim_end();
match looking_at {
Part::Matter => {
if line == self.delimiter {
if line == self.delimiter || line == close_delimiter {
let matter = acc.trim().to_string();

if !matter.is_empty() {
Expand Down Expand Up @@ -203,6 +209,38 @@ mod tests {
assert!(result.data.is_none(), "should get no front matter");
}

#[test]
fn test_front_matter_with_different_delimiters() {
#[derive(serde::Deserialize, PartialEq, Debug)]
struct FrontMatter {
abc: String,
}
let front_matter = FrontMatter {
abc: "xyz".to_string(),
};
let mut matter: Matter<YAML> = Matter::new();
let result: ParsedEntityStruct<FrontMatter> =
matter.parse_with_struct("---\nabc: xyz\n---").unwrap();
assert!(
result.data == front_matter,
"{}",
"should get front matter as {front_matter:?}"
);
matter.delimiter = "<!--".to_string();
matter.close_delimiter = Some("-->".to_string());
let result = matter.parse("---\nabc: xyz\n---");
assert!(result.data.is_none(), "should get no front matter");
let result: ParsedEntityStruct<FrontMatter> =
matter.parse_with_struct("<!--\nabc: xyz\n-->").unwrap();
assert_eq!(
result.data, front_matter,
"{}",
"should get front matter by custom delimiter"
);
let result = matter.parse("\nabc: xyz\n~~~");
assert!(result.data.is_none(), "should get no front matter");
}

#[test]
pub fn test_empty_matter() {
let matter: Matter<YAML> = Matter::new();
Expand Down
Loading