Skip to content

Commit

Permalink
Added a bridge to the league/commonmark Markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Mar 9, 2015
1 parent 067aece commit 642ac04
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ An implementation of YAML Front matter for PHP. Can parse the YAML *and* the Mar

Require the project with Composer:

```json
{
"require": {
"mnapoli/front-yaml": "*"
}
}
```
composer require mnapoli/front-yaml
```

## Usage
Expand Down Expand Up @@ -80,3 +76,11 @@ interface MarkdownParser
```

FrontYAML uses by default [Parsedown Markdown parser](http://parsedown.org/).

An adapter to [League CommonMark](https://github.com/thephpleague/commonmark) is also included (you need to require the `league/commonmark` though):

```php
use \Mni\FrontYAML\Bridge\CommonMark\CommonMarkParser;

$parser = new Mni\FrontYAML\Parser(null, new CommonMarkParser());
```
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
"Mni\\FrontYAML\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mni\\FrontYAML\\Test\\": "tests/"
}
},
"require": {
"symfony/yaml": "~2.1",
"erusev/parsedown": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
"phpunit/phpunit": "~4.5",
"league/commonmark": "~0.7"
}
}
33 changes: 33 additions & 0 deletions src/Bridge/CommonMark/CommonMarkParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* FrontYAML
*
* @copyright Matthieu Napoli http://mnapoli.fr
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/

namespace Mni\FrontYAML\Bridge\CommonMark;

use League\CommonMark\CommonMarkConverter;
use Mni\FrontYAML\Markdown\MarkdownParser;

/**
* Bridge to the League CommonMark parser
*
* @author Matthieu Napoli <[email protected]>
*/
class CommonMarkParser implements MarkdownParser
{
public function __construct(CommonMarkConverter $commonMarkConverter = null)
{
$this->parser = $commonMarkConverter ?: new CommonMarkConverter();
}

/**
* {@inheritdoc}
*/
public function parse($markdown)
{
return $this->parser->convertToHtml($markdown);
}
}
33 changes: 33 additions & 0 deletions tests/Bridge/CommonMark/CommonMarkParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mni\FrontYAML\Test\Bridge\CommonMark;

use Mni\FrontYAML\Bridge\CommonMark\CommonMarkParser;

class CommonMarkParserTest extends \PHPUnit_Framework_TestCase
{
public function testParseWithDefaultParser()
{
$parser = new CommonMarkParser();

$html = $parser->parse('# This is a title');

$this->assertEquals("<h1>This is a title</h1>\n", $html);
}

public function testParseWithGivenParser()
{
$markdown = '# This is a title';
$html = "<h1>This is a title</h1>\n";

$commonMark = $this->getMock('League\CommonMark\CommonMarkConverter');
$commonMark->expects($this->once())
->method('convertToHtml')
->with($markdown)
->willReturn($html);

$parser = new CommonMarkParser($commonMark);

$this->assertEquals($html, $parser->parse($markdown));
}
}
2 changes: 1 addition & 1 deletion tests/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Test\Mni\FrontYAML\Parser;
namespace Mni\FrontYAML\Test;

use Mni\FrontYAML\Bridge\Parsedown\ParsedownParser;
use Mni\FrontYAML\Bridge\Symfony\SymfonyYAMLParser;
Expand Down
2 changes: 1 addition & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Test\Mni\FrontYAML\Parser;
namespace Mni\FrontYAML\Test;

use Mni\FrontYAML\Parser;

Expand Down

0 comments on commit 642ac04

Please sign in to comment.