Parsing Documents

Parsing documents using Kurenai couldn't be simpler. First, let's create a document parser.

<?php

use Kurenai\Parser;
use Kurenai\Parsers\Metadata\JsonParser;
use Kurenai\Parsers\Content\MarkdownParser;

$parser = new Parser(new JsonParser, new MarkdownParser);

Here we've created a parser for documents which have JSON metadata, and Markdown content.

For alternative parsers, simply check the xxxx.

Let's use our newly created parser to parse a document.

<?php

$document = $parser->parse('path/to/document.txt');

// or...

$document = $parser->parse($documentAsString);

You can specify either a path to a document (with any extension) or provide the document content directly to the parse() method.

Now that we have a document, let's see what we can do with it.

<?php

$document->getRaw();                 // Get the raw document content.

$document->getMetdata();             // Get document metadata as array.

$document->getContent();             // Get the rendered document content.

$document->get('foo.bar');           // Use dot-notation to access metadata.

$document->get('foo', 'default');    // Provide a default value.

Unless you provide a default value, the get() method will return null should an array key fail to exist.


Did this page help you?