2017 © Pedro PelĂĄez
 

library frontmatter

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture

image

hkod/frontmatter

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture

  • Sunday, February 11, 2018
  • by hanneskod
  • Repository
  • 1 Watchers
  • 1 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 4 % Grown

The README.md

frontmatter

Packagist Version Build Status, (*1)

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture., (*2)

Front matter is metadata located at the top of a file wrapped in delimiting line tokens, usually ---. The front matter may be formatted using YAML, Json or any ohter simliar format such as NEON or TOML., (*3)

Supported formats:, (*4)

  • Json
  • INI
  • YAML (require symfony/yaml)
  • Markdown (require erusev/parsedown)
  • Mustache (require mustache/mustache)

Parsers are simple callables, super easy to add more formats., (*5)

Installation

composer require hkod/frontmatter

Usage

A standard parser with yaml frontmatter and markdown body:, (*6)

, (*7)

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\YamlParser,
    new \hkod\frontmatter\MarkdownParser
);

$result = $parser->parse("---
key: value
---
This is a **template**
");

// value
echo $result->getFrontmatter()['key'];

// 

This is a template, (*8)

echo $result->getBody();

Specify the front matter delimiter

Note that the delimiting tokens always represents full lines., (*9)

You may set the delimiters when creating the block parser., (*10)

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\BlockParser('***', '***')
);

$result = $parser->parse("***
frontmatter
***
body
");

// frontmatter
echo $result->getFrontmatter();

Putting the frontmatter last

Note that since the delimiting tokens represent a line the last line must end whit a new line (or similar) or it won't be recognized by the parser., (*11)

Frontmatter also supports an inverted block parser, where the frontmatter is expected to bee last instead of first., (*12)

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\InvertedBlockParser
);

$result = $parser->parse("
This is a the body
---
This is the frontmatter
---
");

// "This is the frontmatter"
echo $result->getFrontmatter();

Passing a context

When parsing you may pass a context to the parser and it will in turn be passed along to all subsequent parsers. Context dependet parsers may for example expand templates..., (*13)

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\MustacheParser
);

$context = ['variable' => 'foobar'];

$result = $parser->parse("{{variable}}", $context);

// foobar
echo $result->getBody();

Creating complex parsers

, (*14)

$parser = (new \hkod\frontmatter\ParserBuilder)
    ->addFrontmatterPass(new \hkod\frontmatter\MustacheParser)
    ->addFrontmatterPass(new \hkod\frontmatter\YamlParser)
    ->addBodyPass(new \hkod\frontmatter\MustacheParser)
    ->addBodyPass(new \hkod\frontmatter\MarkdownParser)
    ->setBlockParser(new \hkod\frontmatter\BlockParser('***', '***'))
    ->buildParser();

$document = "***
key: {{variable}}
***
This is a **{{text}}** template
";

$context = ['variable' => 'value', 'text' => 'markdown'];

$result = $parser->parse($document, $context);

// value
echo $result->getFrontmatter()['key'];

// 

This is a markdown template, (*15)

echo $result->getBody();

The Versions

11/02 2018

dev-master

9999999-dev https://github.com/hanneskod/frontmatter

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture

  Sources   Download

Unlicense

The Requires

  • php >=7.0

 

The Development Requires

by Hannes ForsgÄrd

json ini markdown yaml mustache metadata frontmatter

11/02 2018

1.1.0

1.1.0.0 https://github.com/hanneskod/frontmatter

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture

  Sources   Download

Unlicense

The Requires

  • php >=7.0

 

The Development Requires

by Hannes ForsgÄrd

metadata frontmatter

09/02 2018

1.0.0

1.0.0.0 https://github.com/hanneskod/frontmatter

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture

  Sources   Download

Unlicense

The Requires

  • php >=7.0

 

The Development Requires

by Hannes ForsgÄrd

metadata frontmatter