Skip to content

Commit d1d5a9a

Browse files
committed
Adding rst-support
1 parent 5eefa66 commit d1d5a9a

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ script:
1919
- vendor/bin/phpstan analyse
2020
- vendor/bin/phpspec run --format dot -vvv --no-interaction
2121
- bin/doc-parser tests/test.md
22+
- bin/doc-parser tests/test.rst

bin/doc-parser

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ if(file_exists($autoloadPath)) {
1414
include __DIR__.'/../../../autoload.php';
1515
}
1616

17+
use Gregwar\RST\Parser;
1718
use Mamazu\DocumentationParser\Application;
1819
use Mamazu\DocumentationParser\CLI;
1920
use Mamazu\DocumentationParser\FileList;
2021
use Mamazu\DocumentationParser\Output\TextFormatter;
2122
use Mamazu\DocumentationParser\Parser\Parser\MarkdownParser;
23+
use Mamazu\DocumentationParser\Parser\Parser\RstParser;
2224
use Mamazu\DocumentationParser\Validator\CompositeValidator;
2325
use Mamazu\DocumentationParser\Validator\Php\PhpClassExistsValidator;
2426
use Mamazu\DocumentationParser\Validator\XML\XMLValidValidator;
@@ -34,6 +36,7 @@ $formatter = new TextFormatter();
3436
$application = new Application(
3537
[
3638
new MarkdownParser(),
39+
new RstParser(new Parser()),
3740
],
3841
[
3942
'php' =>

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
"thecodingmachine/safe": "^0.1.16",
1919
"nikic/php-parser": "^4.2",
20-
"symfony/yaml": "^4"
20+
"symfony/yaml": "^4",
21+
"gregwar/rst": "dev-line-numbers"
2122
},
2223
"require-dev": {
2324
"phpspec/phpspec": "^5.1|^6.0",
@@ -43,5 +44,11 @@
4344
"vendor/bin/phpstan analyse"
4445
]
4546
},
46-
"bin": [ "bin/doc-parser" ]
47+
"bin": [ "bin/doc-parser" ],
48+
"repositories": [
49+
{
50+
"type": "vcs",
51+
"url": "https://github.com/mamazu/RST/"
52+
}
53+
]
4754
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace spec\Mamazu\DocumentationParser\Parser\Parser;
4+
5+
use Gregwar\RST\Document;
6+
use Gregwar\RST\Environment;
7+
use Gregwar\RST\ErrorManager;
8+
use Gregwar\RST\HTML\Nodes\CodeNode;
9+
use Gregwar\RST\Parser;
10+
use Mamazu\DocumentationParser\Parser\Parser\ParserInterface;
11+
use PhpSpec\ObjectBehavior;
12+
use Prophecy\Argument;
13+
14+
class RstParserSpec extends ObjectBehavior
15+
{
16+
public function let(Parser $parser): void
17+
{
18+
$this->beConstructedWith($parser);
19+
}
20+
21+
public function it_is_a_parser(): void
22+
{
23+
$this->shouldImplement(ParserInterface::class);
24+
}
25+
26+
public function it_only_allows_rst_files(): void
27+
{
28+
$this->canParse('docs.rst')->shouldReturn(true);
29+
$this->canParse('hello.py')->shouldReturn(false);
30+
$this->canParse('hello.RsT')->shouldReturn(true);
31+
}
32+
33+
public function it_parses_code_blocks(
34+
Parser $parser,
35+
Document $document,
36+
Environment $environment,
37+
ErrorManager $errorManager,
38+
CodeNode $codeNode
39+
): void
40+
{
41+
$parser->getEnvironment()->willReturn($environment);
42+
$environment->getErrorManager()->willReturn($errorManager);
43+
44+
$errorManager->abortOnError(false)->shouldBeCalled();
45+
46+
$parser->parseFile('documentation.rst')->shouldBeCalled()->willReturn($document);
47+
$document->getNodes(Argument::type(\Closure::class))->willReturn([ $codeNode ]);
48+
$codeNode->getValue()->willReturn('echo "PHP";');
49+
$codeNode->getStartingLineNumber()->willReturn(12);
50+
$codeNode->getLanguage()->willReturn('php');
51+
52+
$result = $this->parse('documentation.rst');
53+
$result->shouldHaveCount(1);
54+
$result[0]->getFileName()->shouldBe('documentation.rst');
55+
$result[0]->getContent()->shouldBe('echo "PHP";');
56+
$result[0]->getRelativeLineNumber()->shouldBe(12);
57+
$result[0]->getType()->shouldBe('php');
58+
}
59+
}

src/Parser/Parser/RstParser.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
4+
namespace Mamazu\DocumentationParser\Parser\Parser;
5+
6+
use Gregwar\RST\Nodes\CodeNode;
7+
use Gregwar\RST\Nodes\Node;
8+
use Gregwar\RST\Parser;
9+
use Mamazu\DocumentationParser\Parser\Block;
10+
11+
class RstParser implements ParserInterface
12+
{
13+
/** @var Parser */
14+
private $rstParser;
15+
16+
public function __construct(Parser $parser)
17+
{
18+
$this->rstParser = $parser;
19+
}
20+
21+
public function canParse(string $fileName): bool
22+
{
23+
return strtolower(substr($fileName, -3)) === 'rst';
24+
}
25+
26+
public function parse(string $fileName): array
27+
{
28+
$this->rstParser->getEnvironment()->getErrorManager()->abortOnError(false);
29+
$parsedOutput = $this->rstParser->parseFile($fileName);
30+
31+
/** @var CodeNode[] $codeNodes */
32+
$codeNodes = $parsedOutput->getNodes(
33+
static function (Node $node) { return $node instanceof CodeNode; }
34+
);
35+
36+
$blocks = [];
37+
foreach($codeNodes as $codeNode) {
38+
if($codeNode->getLanguage() === null) {
39+
continue;
40+
}
41+
42+
$blocks[] = new Block(
43+
$fileName,
44+
$codeNode->getValue(),
45+
$codeNode->getStartingLineNumber(),
46+
$codeNode->getLanguage()
47+
);
48+
}
49+
50+
return $blocks;
51+
}
52+
}

tests/test.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Hello world
2+
===========
3+
4+
What is it?
5+
----------
6+
This is a **RST** document!
7+
8+
Where can I get it?
9+
-------------------
10+
You can get it on the `GitHub page <https://github.com/Gregwar/RST>`_
11+
12+
13+
.. code-block:: php
14+
:linenos:
15+
:encoding: utf-8
16+
17+
<?php
18+
echo "Hello world";
19+
20+
I was just about to get angry

0 commit comments

Comments
 (0)