extract-frontmatter 2.1.0

A library that allows a user to extract an arbitrary number of lines of 'front-matter' from the start of any string
Documentation

extract-frontmatter

Overview

A Rust library that allows a user to extract an arbitrary number of lines of "front-matter" from the start of any multiline string.

Note that absolutely no parsing of extracted front-matter is performed; this is designed to output its results for another library to then parse.

docs.io documentation crates.io version crates.io downloads Gitlab pipeline status Gitlab code coverage Lines of code Dependents License

Versioning

This project follows Semantic Versioning principals starting with v1.0.0

Repository information

This repository is located on GitLab.com.

Usage

Input:

root:
  child1: true
  child2: two
---

# Example

This is an example Markdown file.

Extracting front-matter

  1. Instantiate an instance of Extractor
  2. Call a selector method on it
  3. Call zero or more modifier methods on it
  4. Call:
    • extract (for a String), or
    • collect (for a Vec<&str>)

Example

Code:

use extract_frontmatter::Extractor;

let mut extractor = Extractor::new(input);
extractor.select_by_terminator("---");

let output: String = extractor.extract();

Output:

root:
  child1: true
  child2: two

Removing front-matter

  1. Instantiate an instance of Extractor
  2. Call a selector method on it
  3. Call remove on it

Example

Code:

use extract_frontmatter::Extractor;

let mut extractor = Extractor::new(input);
extractor.select_by_terminator("---");

let output: &str = extractor.remove().trim();

Output:

# Example

This is an example Markdown file.

Split document

  1. Instantiate an instance of Extractor
  2. Call a selector method on it
  3. Call zero or more modifier methods on it
  4. Call split on it

Example

Code:

use extract_frontmatter::Extractor;

let mut extractor = Extractor::new(input);
extractor.select_by_terminator("---");

let (front_matter, document): (Vec<&str>, &str) = extractor.split();

let front_matter = front_matter.join("\n");
let document = document.trim();

Front matter:

root:
  child1: true
  child2: two

Document:

# Example

This is an example Markdown file.

Function overview

Selectors

Method Description
select_by_terminator(terminator: &str) Selects lines until the first one equal to terminator is found
select_by_prefix(prefix: &str) Selects consecutive lines starting with prefix
select_by_line_count(count: usize) Selects count lines

Modifiers

Method Description
strip_prefix(prefix: &str) Strips the given prefix from all returned lines
strip_whitespace() Strips leading and trailing whitespace from all returned lines
discard_first_line() Skips the first line to be returned
discard_last_line() Skips the last line to be returned