From d02d2426822235f5179ecdf635ba710c9d6d2ddd Mon Sep 17 00:00:00 2001 From: Matthias Steffens Date: Fri, 25 Aug 2023 13:21:46 +0200 Subject: [PATCH] If installed, prefer the `ueberdosis/pandoc` package over `ryakad/pandoc-php` (#108) * If installed, prefer the ueberdosis/pandoc package over ryakad/pandoc-php (cherry picked from commit 5490164d4803e68cecdf2bfe29419bd222ae973d) * Creation of the Pandoc converter & checking for installed packages now only happens once, and throws an exception in case requirements aren't met (cherry picked from commit 82d8327164a10784e90de8f6f1a7c43ca5af3061) * Mention the Pandoc wrapper package ueberdosis/pandoc as an alternative to ryakad/pandoc-php (cherry picked from commit fdd7a6a5f4ad2fb343a1f2267152de50597b2e71) * Add tests with alternative pandoc wrapper, fix CS, add PHP 5.6 support --- .github/workflows/coding-standards.yaml | 2 + .github/workflows/tests.yaml | 8 ++++ README.md | 2 +- composer.json | 3 +- src/Processor/LatexToUnicodeProcessor.php | 55 +++++++++++++++++------ 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.github/workflows/coding-standards.yaml b/.github/workflows/coding-standards.yaml index 6f4a4a9..8d506a2 100644 --- a/.github/workflows/coding-standards.yaml +++ b/.github/workflows/coding-standards.yaml @@ -26,6 +26,8 @@ jobs: php-version: 7.4 tools: php-cs-fixer:2, cs2pr + - run: mkdir var + - name: Cache PHP Coding Standards uses: actions/cache@v2 with: diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 92dbdfe..6d4015a 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -21,6 +21,8 @@ jobs: include: - php-version: '8.0' coverage: true + - php-version: '8.2' + with-ueberdosis-pandoc: true steps: @@ -39,6 +41,12 @@ jobs: - name: Install Composer dependencies uses: ramsey/composer-install@v2 + - name: Install Pandoc wrapper + if: ${{ matrix.with-ueberdosis-pandoc }} + run: | + composer remove --dev ryakad/pandoc-php + composer require --dev ueberdosis/pandoc + - name: Run PHPUnit run: vendor/bin/phpunit --testdox ${{ matrix.coverage && '--coverage-clover ./coverage.xml' || '--no-coverage' }} diff --git a/README.md b/README.md index 9b7fe37..0a3860e 100644 --- a/README.md +++ b/README.md @@ -419,7 +419,7 @@ You might want to read them as unicode instead. The `LatexToUnicodeProcessor` class solves this problem, but before adding the processor to the listener you must: - [install Pandoc](http://pandoc.org/installing.html) in your system; and -- add [ryakad/pandoc-php](https://github.com/ryakad/pandoc-php) as a dependency of your project. +- add [ryakad/pandoc-php](https://github.com/ryakad/pandoc-php) or [ueberdosis/pandoc](https://github.com/ueberdosis/pandoc) as a dependency of your project.
Usage diff --git a/composer.json b/composer.json index 0d43038..2bbbabc 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "ryakad/pandoc-php": "^1.0" }, "suggest": { - "ryakad/pandoc-php": "Needed to support LaTeX decoder in class RenanBr\\BibTexParser\\Processor\\LatexToUnicodeProcessor" + "ryakad/pandoc-php": "Needed to support LaTeX decoder in class RenanBr\\BibTexParser\\Processor\\LatexToUnicodeProcessor", + "ueberdosis/pandoc": "Alternate Pandoc PHP package which (if available) will be preferred over ryakad/pandoc-php" }, "config": { "sort-packages": true diff --git a/src/Processor/LatexToUnicodeProcessor.php b/src/Processor/LatexToUnicodeProcessor.php index 5055484..f5eec61 100644 --- a/src/Processor/LatexToUnicodeProcessor.php +++ b/src/Processor/LatexToUnicodeProcessor.php @@ -11,9 +11,11 @@ namespace RenanBr\BibTexParser\Processor; +use Composer\InstalledVersions; +use Exception; use Pandoc\Pandoc; -use Pandoc\PandocException; use RenanBr\BibTexParser\Exception\ProcessorException; +use RuntimeException; /** * Translates LaTeX texts to unicode. @@ -22,8 +24,8 @@ class LatexToUnicodeProcessor { use TagCoverageTrait; - /** @var Pandoc|null */ - private $pandoc; + /** @var (callable(string): string)|null */ + private $converter; /** * @return array @@ -59,17 +61,44 @@ public function __invoke(array $entry) private function decode($text) { try { - if (!$this->pandoc) { - $this->pandoc = new Pandoc(); - } - - return $this->pandoc->runWith($text, [ - 'from' => 'latex', - 'to' => 'plain', - 'wrap' => 'none', - ]); - } catch (PandocException $exception) { + return \call_user_func($this->getConverter(), $text); + } catch (Exception $exception) { throw new ProcessorException(sprintf('Error while processing LaTeX to Unicode: %s', $exception->getMessage()), 0, $exception); } } + + /** + * @return (callable(string): string) + */ + private function getConverter() + { + if ($this->converter) { + return $this->converter; + } + + if (InstalledVersions::isInstalled('ueberdosis/pandoc')) { + $pandoc = new Pandoc(); + + return $this->converter = static function ($text) use ($pandoc) { + // @phpstan-ignore-next-line + return mb_substr($pandoc->input($text)->execute([ + '--from', 'latex', + '--to', 'plain', + '--wrap', 'none', + ]), 0, -1); + }; + } elseif (InstalledVersions::isInstalled('ryakad/pandoc-php')) { + $pandoc = new Pandoc(); + + return $this->converter = static function ($text) use ($pandoc) { + return $pandoc->runWith($text, [ + 'from' => 'latex', + 'to' => 'plain', + 'wrap' => 'none', + ]); + }; + } + + throw new RuntimeException('Pandoc wrapper not installed. Try running "composer require ueberdosis/pandoc"'); + } }