Skip to content

Commit

Permalink
If installed, prefer the ueberdosis/pandoc package over `ryakad/pan…
Browse files Browse the repository at this point in the history
…doc-php` (#108)

* If installed, prefer the ueberdosis/pandoc package over ryakad/pandoc-php

(cherry picked from commit 5490164)

* 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 82d8327)

* Mention the Pandoc wrapper package ueberdosis/pandoc as an alternative to ryakad/pandoc-php

(cherry picked from commit fdd7a6a)

* Add tests with alternative pandoc wrapper, fix CS, add PHP 5.6 support
  • Loading branch information
extracts committed Aug 25, 2023
1 parent ae82b39 commit d02d242
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/coding-standards.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
include:
- php-version: '8.0'
coverage: true
- php-version: '8.2'
with-ueberdosis-pandoc: true

steps:

Expand All @@ -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' }}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<details><summary>Usage</summary>

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 42 additions & 13 deletions src/Processor/LatexToUnicodeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,8 +24,8 @@ class LatexToUnicodeProcessor
{
use TagCoverageTrait;

/** @var Pandoc|null */
private $pandoc;
/** @var (callable(string): string)|null */
private $converter;

/**
* @return array
Expand Down Expand Up @@ -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"');
}
}

0 comments on commit d02d242

Please sign in to comment.