Skip to content

Commit

Permalink
If installed, prefer the ueberdosis/pandoc package over ryakad/pandoc…
Browse files Browse the repository at this point in the history
…-php
  • Loading branch information
extracts committed Jul 13, 2023
1 parent d873dcd commit 5490164
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
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
40 changes: 33 additions & 7 deletions src/Processor/LatexToUnicodeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace RenanBr\BibTexParser\Processor;

use Composer\InstalledVersions;
use Exception;
use Pandoc\Pandoc;
use Pandoc\PandocException;
use RenanBr\BibTexParser\Exception\ProcessorException;

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 5490164

Please sign in to comment.