diff --git a/composer.json b/composer.json index 81a9cb5..28e71f3 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..b99303c 100644 --- a/src/Processor/LatexToUnicodeProcessor.php +++ b/src/Processor/LatexToUnicodeProcessor.php @@ -11,8 +11,9 @@ namespace RenanBr\BibTexParser\Processor; +use Composer\InstalledVersions; +use Exception; use Pandoc\Pandoc; -use Pandoc\PandocException; use RenanBr\BibTexParser\Exception\ProcessorException; /** @@ -51,6 +52,16 @@ public function __invoke(array $entry) return $entry; } + /** + * Returns true if the ueberdosis/pandoc package is installed, otherwise returns false. + * + * @return bool + */ + private function isUeberdosisPandocAvailable() + { + return InstalledVersions::isInstalled('ueberdosis/pandoc'); + } + /** * @param mixed $text * @@ -63,12 +74,27 @@ private function decode($text) $this->pandoc = new Pandoc(); } - return $this->pandoc->runWith($text, [ - 'from' => 'latex', - 'to' => 'plain', - 'wrap' => 'none', - ]); - } catch (PandocException $exception) { + if ($this->isUeberdosisPandocAvailable()) { + // use ueberdosis/pandoc + $output = $this->pandoc->input($text)->execute([ + '--from', 'latex', + '--to', 'plain', + '--wrap', 'none', + ]); + + // remove newline character added by Pandoc conversion + $output = substr($output, 0, -1); + } else { + // use ryakad/pandoc-php + $output = $this->pandoc->runWith($text, [ + 'from' => 'latex', + 'to' => 'plain', + 'wrap' => 'none', + ]); + } + + return $output; + } catch (Exception $exception) { throw new ProcessorException(sprintf('Error while processing LaTeX to Unicode: %s', $exception->getMessage()), 0, $exception); } }