Skip to content

Commit

Permalink
Adding a simple tex parser to the project
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Jan 9, 2022
1 parent de7dc36 commit f66d1ab
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ You can also configure it to use a directory: `bin/doc-parser -i <extension-dir>
> .. code-block:: php
>
> echo "ABC";
* Latex parser:
> \begin{lstlisting}[language=Python]
>
> print("Hello World")
>
> \end{lstlisting}
### Validators
* CompositeValidator: Validates all of its children passed into the constructor
Expand Down
2 changes: 2 additions & 0 deletions bin/doc-parser
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use Mamazu\DocumentationParser\FileList;
use Mamazu\DocumentationParser\Output\TextFormatter;
use Mamazu\DocumentationParser\Parser\Parser\MarkdownParser;
use Mamazu\DocumentationParser\Parser\Parser\RstParser;
use Mamazu\DocumentationParser\Parser\Parser\TexParser;
use Mamazu\DocumentationParser\Validator\Bash\BashValidator;
use Mamazu\DocumentationParser\Validator\CompositeValidator;
use Mamazu\DocumentationParser\Validator\Php\PhpClassExistsValidator;
Expand All @@ -38,6 +39,7 @@ $application = new Application(
[
new MarkdownParser(),
new RstParser(new Parser()),
new TexParser(),
],
[
'php' =>
Expand Down
79 changes: 79 additions & 0 deletions spec/Mamazu/DocumentationParser/Parser/Parser/TexParserSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace spec\Mamazu\DocumentationParser\Parser\Parser;

use Mamazu\DocumentationParser\Parser\Parser\ParserInterface;
use org\bovigo\vfs\vfsStream;
use PhpSpec\ObjectBehavior;

class TexParserSpec extends ObjectBehavior
{
/** @var vfsStreamDirectory */
public $workDir;

public function let(): void
{
$this->workDir = vfsStream::setup('workDir');
}

public function it_is_a_parser(): void
{
$this->shouldImplement(ParserInterface::class);
}

public function it_only_allows_rst_files(): void
{
$this->canParse('docs.tex')->shouldReturn(true);
$this->canParse('hello.py')->shouldReturn(false);
$this->canParse('hello.TEX')->shouldReturn(true);
}

public function it_parses_a_file_without_code(): void {
# Setting up the file
$file = vfsStream::newFile('simple_file.tex');
$file->setContent('\\begin{document}\\end{document}');
$this->workDir->addChild($file);

$this->parse('vfs://workDir/simple_file.tex')->shouldHaveCount(0);
}

public function it_parses_a_file_with_python_code(): void
{
# Setting up the file
$file = vfsStream::newFile('multiple_code.tex');
$file->setContent(<<<LATEX
\\begin{document}
\\begin{lstlisting}[language=Python]
print("Hello World")
\\end{lstlisting}
\\end{document}
LATEX);
$this->workDir->addChild($file);

$result = $this->parse('vfs://workDir/multiple_code.tex');
$result->shouldHaveCount(1);
$result[0]->getFileName()->shouldContain('multiple_code.tex');
$result[0]->getRelativeLineNumber()->shouldBe(2);
$result[0]->getContent()->shouldBe('print("Hello World")');
$result[0]->getType()->shouldBe('python');
}

public function it_parses_a_single_line_code(): void
{
# Setting up the file
$file = vfsStream::newFile('single_line_code.tex');
$file->setContent(<<<LATEX
\\begin{document}
\\begin{lstlisting}[language=PHP] echo "Testing"; \\end{lstlisting}
\\end{document}
LATEX);
$this->workDir->addChild($file);

$result = $this->parse('vfs://workDir/single_line_code.tex');
$result->shouldHaveCount(1);
$result[0]->getFileName()->shouldContain('single_line_code.tex');
$result[0]->getRelativeLineNumber()->shouldBe(2);
$result[0]->getContent()->shouldBe('echo "Testing";');
$result[0]->getType()->shouldBe('php');
}
}
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ private function getDocumentationBlocks(string $fileName): array
}
}

trigger_error('There was no parser found for file '. $fileName, E_USER_WARNING);

return [];
}

Expand Down
56 changes: 56 additions & 0 deletions src/Parser/Parser/TexParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Mamazu\DocumentationParser\Parser\Parser;

use Mamazu\DocumentationParser\Parser\Block;

class TexParser implements ParserInterface {
public function canParse(string $fileName): bool
{
return strtolower(substr($fileName, -3)) === 'tex';
}

public function parse(string $fileName): array
{
$fileContent = file($fileName, FILE_IGNORE_NEW_LINES);

$codeBlockBegin = null;
$type = '';
$content = '';
$blocks = [];

foreach($fileContent as $lineIndex => $line) {
$matches = [];

//trying to match the following pattern: \begin{lstlisting}[language=Python]
if (preg_match('/\\\\begin{lstlisting}(\\[language=(?<lang>\w+)\\])?\\s?(?<rest>[^\\\\]*)/', $line, $matches)) {
$codeBlockBegin = $lineIndex;
$type = strtolower($matches['lang']);
$content = $matches['rest'];
}

if ($codeBlockBegin !== null && strpos($line, '\\end{lstlisting}') !== false) {
$blocks[] = new Block(
$fileName,
rtrim($content),
$codeBlockBegin + 1,
$type
);
$codeBlockBegin = null;
$type = '';
}

if ($codeBlockBegin !== null && $codeBlockBegin !== $lineIndex) {
$content .= $line . "\n";
}
}

if ($codeBlockBegin !== null) {
throw new \InvalidArgumentException(
'You have an unopend codeblock in your code starting on line: '. ($codeBlockBegin + 1)
);
}

return $blocks;
}
}
1 change: 0 additions & 1 deletion src/Validator/Bash/BashValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Mamazu\DocumentationParser\Error\Error;
use Mamazu\DocumentationParser\Parser\Block;
use Mamazu\DocumentationParser\Utils\PhpCodeEnsurer;
use Mamazu\DocumentationParser\Validator\ValidatorInterface;
use Symfony\Component\Filesystem\Filesystem;

Expand Down
3 changes: 3 additions & 0 deletions tests/test.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\begin{lstlisting}[language=Python]
print("Hello World")
\end{lstlisting}

0 comments on commit f66d1ab

Please sign in to comment.