mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/SyntaxHighlight_GeSHi
synced 2024-11-23 22:13:40 +00:00
Merge "fetchLexers: Pass '--json' to Pygmentize"
This commit is contained in:
commit
14cb8415da
6
README
6
README
|
@ -6,8 +6,8 @@ for updated information:
|
|||
|
||||
== Requirements ==
|
||||
|
||||
This version of the extension has been tested with Pygments 2.8.0 and
|
||||
MediaWiki 1.35. To get releases of this extension compatible
|
||||
This version of the extension has been tested with Pygments 2.11.2 and
|
||||
MediaWiki 1.36. To get releases of this extension compatible
|
||||
with earlier versions of MediaWiki, visit:
|
||||
|
||||
https://www.mediawiki.org/wiki/Special:ExtensionDistributor/SyntaxHighlight_GeSHi
|
||||
|
@ -18,7 +18,7 @@ Add this line to your LocalSettings.php:
|
|||
|
||||
wfLoadExtension( 'SyntaxHighlight_GeSHi' );
|
||||
|
||||
By default, this extension will use a bundled copy of Pygments 2.8.0. If you
|
||||
By default, this extension will use a bundled copy of Pygments 2.11.2. If you
|
||||
would like to use a different copy of the library, you can set
|
||||
$wgPygmentizePath to point to the path to the 'pygmentize' binary.
|
||||
|
||||
|
|
|
@ -188,6 +188,16 @@ class Pygmentize {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the pygments command line supports the --json option
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function pygmentsSupportsJsonOutput(): bool {
|
||||
$version = self::getVersion();
|
||||
return ( version_compare( $version, '2.11.0' ) !== -1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Shell out to get supported lexers by pygments
|
||||
*
|
||||
|
@ -195,8 +205,13 @@ class Pygmentize {
|
|||
* @return array
|
||||
*/
|
||||
public static function fetchLexers(): array {
|
||||
$cliParams = [ self::getPath(), '-L', 'lexer' ];
|
||||
if ( self::pygmentsSupportsJsonOutput() ) {
|
||||
$cliParams[] = '--json';
|
||||
}
|
||||
|
||||
$result = self::boxedCommand()
|
||||
->params( self::getPath(), '-L', 'lexer' )
|
||||
->params( $cliParams )
|
||||
->includeStderr()
|
||||
->execute();
|
||||
self::recordShellout( 'fetch_lexers' );
|
||||
|
@ -205,9 +220,50 @@ class Pygmentize {
|
|||
throw new PygmentsException( $output );
|
||||
}
|
||||
|
||||
// Post-process the output, ideally pygments would output this in a
|
||||
// machine-readable format (https://github.com/pygments/pygments/issues/1437)
|
||||
$output = $result->getStdout();
|
||||
if ( self::pygmentsSupportsJsonOutput() ) {
|
||||
$lexers = self::parseLexersFromJson( $output );
|
||||
} else {
|
||||
$lexers = self::parseLexersFromText( $output );
|
||||
}
|
||||
|
||||
$lexers = array_unique( $lexers );
|
||||
sort( $lexers );
|
||||
$data = [];
|
||||
foreach ( $lexers as $lexer ) {
|
||||
$data[$lexer] = true;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json output of the pygments lexers list and return as php array
|
||||
*
|
||||
* @param string $output JSON formatted output of pygments lexers list
|
||||
* @return array
|
||||
*/
|
||||
private static function parseLexersFromJson( $output ): array {
|
||||
$data = json_decode( $output, true );
|
||||
if ( $data === null ) {
|
||||
throw new PygmentsException(
|
||||
'Got invalid JSON from Pygments: ' . $output );
|
||||
}
|
||||
$lexers = [];
|
||||
foreach ( array_values( $data['lexers'] ) as $lexer ) {
|
||||
$lexers = array_merge( $lexers, $lexer['aliases'] );
|
||||
}
|
||||
return $lexers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse original stdout of the pygments lexers list
|
||||
* This was the only format available before pygments 2.11.0
|
||||
* NOTE: Should be removed when pygments 2.11 is the minimum version expected to be installed
|
||||
*
|
||||
* @param string $output Textual list of pygments lexers
|
||||
* @return array
|
||||
*/
|
||||
private static function parseLexersFromText( $output ): array {
|
||||
$lexers = [];
|
||||
foreach ( explode( "\n", $output ) as $line ) {
|
||||
if ( substr( $line, 0, 1 ) === '*' ) {
|
||||
|
@ -219,14 +275,7 @@ class Pygmentize {
|
|||
}
|
||||
}
|
||||
}
|
||||
$lexers = array_unique( $lexers );
|
||||
sort( $lexers );
|
||||
$data = [];
|
||||
foreach ( $lexers as $lexer ) {
|
||||
$data[$lexer] = true;
|
||||
}
|
||||
|
||||
return $data;
|
||||
return $lexers;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue