Skip to content

Commit

Permalink
add new option property instead of changing the main
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawell authored and yuchanns committed May 17, 2024
1 parent b0b3fc0 commit 63b1b78
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ use serde::Deserialize;

fn main() {
let mut matter: Matter<YAML> = Matter::new();
matter.delimiter = ["~~~".to_owned(), "~~~".to_owned()];
matter.delimiter = "~~~".to_owned();
matter.excerpt_delimiter = Some("<!-- endexcerpt -->".to_owned());

#[derive(Deserialize, Debug)]
Expand All @@ -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
26 changes: 16 additions & 10 deletions src/matter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ enum Part {
/// Coupled with an [`Engine`](crate::engine::Engine) of choice, `Matter` stores delimiter(s) and
/// handles parsing.
pub struct Matter<T: Engine> {
pub delimiter: [String; 2],
pub delimiter: String,
pub close_delimiter: Option<String>,
pub excerpt_delimiter: Option<String>,
engine: PhantomData<T>,
}
Expand All @@ -26,7 +27,8 @@ impl<T: Engine> Default for Matter<T> {
impl<T: Engine> Matter<T> {
pub fn new() -> Self {
Self {
delimiter: ["---".to_string(), "---".to_string()],
delimiter: "---".to_string(),
close_delimiter: None,
excerpt_delimiter: None,
engine: PhantomData,
}
Expand Down Expand Up @@ -58,22 +60,25 @@ impl<T: Engine> Matter<T> {
matter: String::new(),
};

let [open_delimiter, close_delimiter] = &self.delimiter;

// Check if input is empty or shorter than the delimiter
if input.is_empty() || input.len() <= open_delimiter.len() {
if input.is_empty() || input.len() <= self.delimiter.len() {
return parsed_entity;
}

// If excerpt delimiter is given, use it. Otherwise, use normal delimiter
let excerpt_delimiter = self
.excerpt_delimiter
.clone()
.unwrap_or_else(|| close_delimiter.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') {
Some((first_line, rest)) if first_line.trim_end() == open_delimiter => {
Some((first_line, rest)) if first_line.trim_end() == self.delimiter => {
(Part::Matter, rest.lines())
}
_ => (Part::MaybeExcerpt, input.lines()),
Expand All @@ -84,7 +89,7 @@ impl<T: Engine> Matter<T> {
let line = line.trim_end();
match looking_at {
Part::Matter => {
if line == open_delimiter || line == close_delimiter {
if line == self.delimiter || line == close_delimiter {
let matter = acc.trim().to_string();

if !matter.is_empty() {
Expand Down Expand Up @@ -190,7 +195,7 @@ mod tests {
"{}",
"should get front matter as {front_matter:?}",
);
matter.delimiter = ["~~~".to_string(), "~~~".to_string()];
matter.delimiter = "~~~".to_string();
let result = matter.parse("---\nabc: xyz\n---");
assert!(result.data.is_none(), "should get no front matter");
let result: ParsedEntityStruct<FrontMatter> =
Expand Down Expand Up @@ -221,7 +226,8 @@ mod tests {
"{}",
"should get front matter as {front_matter:?}"
);
matter.delimiter = ["<!--".to_string(), "-->".to_string()];
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> =
Expand Down

0 comments on commit 63b1b78

Please sign in to comment.