Giter VIP home page Giter VIP logo

front-matter's Introduction

Front Matter

Packagist Quality Gate Status Coverage

The most universal Front matter (yaml, json, neon, toml) parser and dumper for PHP. Front matter allows page-specific variables to be included at the top of a page.

Installation

This library can be installed via Composer:

composer require webuni/front-matter

Usage

Automatic front matter detection and parsing

This library can parse all form of front matter:

YAML (Neon)TOMLTwigPugJson
---
foo: bar
---

# Section

Text
+++
foo = bar
+++

# Section

Text
{#---
foo: bar
---#}

<h1>Section</h1>

<p>{{ foo }}</p>
--
  foo: bar

h1 Section

p= foo
{
  "foo": "bar"
}

# Section

Text

The following code will automatically detect the above front matter types:

<?php

$frontMatter = \Webuni\FrontMatter\FrontMatterChain::create();
$document = $frontMatter->parse($string);

$data = $document->getData();
$content = $document->getContent();

Parse an arbitrary string

<?php

$frontMatter = new \Webuni\FrontMatter\FrontMatter();

$document = $frontMatter->parse($string);

$data = $document->getData();
$content = $document->getContent();

Check if a string has front matter

<?php

$frontMatter = new \Webuni\FrontMatter\FrontMatter();

$hasFrontMatter = $frontMatter->exists($string);

Twig loader

If you want to store metadata about twig template, e.g.:

{#---
title: Hello world
menu: main
weight: 20
---#}
{% extend layout.html.twig %}
{% block content %}
Hello world!
{% endblock %}

you can use FrontMatterLoader, that decorates another Twig loader:

$frontMatter = \Webuni\FrontMatter\Twig\TwigCommentFrontMatter::create();
$loader = new \Twig\Loader\FilesystemLoader(['path/to/templates']);
$loader = new \Webuni\FrontMatter\Twig\FrontMatterLoader($frontMatter, $loader);

$twig = new \Twig\Environment($loader);
$content = $twig->render('template', []);
// rendered the valid twig template without front matter

It is possible to inject front matter to Twig template as variables:

// …
$converter = \Webuni\FrontMatter\Twig\DataToTwigConvertor::vars();
$loader = new \Webuni\FrontMatter\Twig\FrontMatterLoader($frontMatter, $loader, $converter);
// …

Loaded Twig template has this code:

{% set title = "Hello world" %}
{% set menu = "main" %}
{% set weight = 20 %}
{% line 5 %}
{% extend layout.html.twig %}
{% block content %}
Hello world!
{% endblock %}

Available converters:

Converter Twig
DataToTwigConvertor::nothing()
DataToTwigConvertor::vars() {% set key1 = value1 %}
DataToTwigConvertor::vars(false) {% set key1 = key1 is defined ? key1 : value1 %}
DataToTwigConvertor::var('name') {% set name = {key1: value1, key2: value2} %}
DataToTwigConvertor::var('name', false) {% set name = name is defined ? name : {key1: value1} %}

Markdown

The most commonly used front matter is for markdown files:

---
layout: post
title: I Love Markdown
tags:
  - test
  - example
---

# Hello World!

This library can be used with league/commonmark:

$frontMatter = new \Webuni\FrontMatter\FrontMatter();
$extension = new \Webuni\FrontMatter\Markdown\FrontMatterLeagueCommonMarkExtension($frontMatter);

$converter = new \League\CommonMark\CommonMarkConverter([]);
$converter->getEnvironment()->addExtension($extension);
$html = $converter->convertToHtml('markdown'); // html without front matter

Alternatives

front-matter's People

Contributors

hason avatar localheinz avatar peter279k avatar szepeviktor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

front-matter's Issues

The TwigLoader should inject a {% line %} tag to preserve line numbers in errors

Twig supports a special {% line %} tag (which is undocumented IIRC, but it exists since before Twig 1.0.0), which allows to override the line numbering (the line on which the tag is set gets considered as re-numbered (and so all following lines too). This would allow to preserve original line numbers in error messages even when the frontmatter is removed:

---
some: frontmatter
---
{# Beginning of content line 4 of the file #}

would become

{% line 3 %}
{# Beginning of content line 4 of the file #}

This makes debugging a lot easier when line numbers in the file correspond to the source file rather than the file without the frontmatter.

Adding typehints

What do you think about adding PHP 7.2 native parameter/return typehints?

Symfony 7 compatibility

The current version is not compatible with symfony 7.

I don't use the library and started a fresh symfony project and came here because I wanted to manage some pages with front matter.

composer require webuni/front-matter

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires webuni/front-matter ^1.5 -> satisfiable by webuni/front-matter[1.5.0].
    - webuni/front-matter 1.5.0 requires symfony/yaml ^3.4.31 || ^4.3.4 || ^5.0 || ^6.0 -> found symfony/yaml[v3.4.31, ..., v3.4.47, v4.3.4, ..., v4.4.45, v5.0.0, ..., v5.4.31, v6.0.0, ..., v6.4.0] but it conflicts with your root composer.json require (7.0.*).

Please upgrade symfony dependencies to 4.x

Hi

Could you please upgrade your dependencies on this project. At the moment this package is being used by daux.io and I would really like to see it's dependencies upgraded.

Nevertheless, I'm still having issues because of a nested dependency to your project (outside usage of daux.io).

Tag new release

👋 Y'all made the thing I dreamed up in my head, loving it so far! Can you tag the latest release as 1.3 or 1.2.1? It looks like there are some new classes and features not yet available via the current 1.2.0 on Composer/Packagist

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.