-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a simple tex parser to the project
- Loading branch information
Showing
7 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
spec/Mamazu/DocumentationParser/Parser/Parser/TexParserSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
\begin{lstlisting}[language=Python] | ||
print("Hello World") | ||
\end{lstlisting} |