🚧 This instance is under construction; expect occasional downtime. Runners available in /repos. Questions? Ask in #wikimedia-gitlab on libera.chat, or under GitLab on Phabricator.

Unverified Commit ff29250e authored by Samwilson's avatar Samwilson Committed by GitHub
Browse files

Merge pull request #1 from samwilson/other-types

Add Mscgen and PlantUML tags
parents b85567d8 380483b9
......@@ -5,7 +5,6 @@ namespace MediaWiki\Extension\Diagrams;
use Html;
use MediaWiki\MediaWikiServices;
use Parser;
use PPFrame;
class Hooks {
......@@ -14,35 +13,53 @@ class Hooks {
* @param Parser $parser
*/
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setHook( 'graphviz', [ static::class, 'renderGraphviz' ] );
foreach ( [ 'graphviz', 'mscgen', 'uml' ] as $tag ) {
$parser->setHook( $tag, function ( string $input ) use ( $tag ) {
if ( $tag === 'graphviz' ) {
// GraphViz.
return static::render( $tag, $input, 'cmapx' );
} elseif ( $tag === 'mscgen' ) {
// Mscgen.
return static::render( $tag, $input, 'ismap' );
} else {
// PlantUML.
return static::render( 'plantuml', $input );
}
} );
}
}
/**
* Graphziz rendering method.
* The main rendering method, handling all types.
* @param string $generator
* @param string $input
* @param array $args
* @param Parser $parser
* @param PPFrame $frame
* @param string|null $type
* @return string
*/
public static function renderGraphviz(
string $input, array $args, Parser $parser, PPFrame $frame
) {
protected static function render( $generator, $input, $type = null ) {
$requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
$baseUrl = MediaWikiServices::getInstance()->getMainConfig()->get( 'DiagramsServiceUrl' );
$url = trim( $baseUrl, '/' ) . '/render';
$params = [
'postData' => [
'generator' => 'graphviz',
'types' => [ 'png', 'cmapx' ],
'generator' => $generator,
'types' => array_filter( [ 'png', $type ] ),
'source' => $input,
],
];
$result = $requestFactory->request( 'POST', $url, $params, __METHOD__ );
$response = \GuzzleHttp\json_decode( $result );
if ( $response->status === 'ok' ) {
if ( isset( $response->error ) ) {
$error = wfMessage( 'diagrams-error-' . $response->error );
if ( isset( $response->message ) ) {
$error .= Html::element( 'br' ) . $response->message;
}
return Html::rawElement( 'span', [ 'class' => 'error' ], $error );
}
$imgAttrs = [ 'src' => $response->diagrams->png->url ];
if ( isset( $response->diagrams->cmapx->contents ) ) {
// Image maps in cmapx format.
$imageMap = new ImageMap( $response->diagrams->cmapx->contents );
$imgAttrs = [ 'src' => $response->diagrams->png->url ];
if ( $imageMap->hasAreas() ) {
$imgAttrs['usemap'] = '#' . $imageMap->getName();
}
......@@ -50,9 +67,18 @@ class Hooks {
if ( $imageMap->hasAreas() ) {
$out .= $imageMap->getMap();
}
return $out;
} elseif ( isset( $response->diagrams->ismap->contents ) ) {
// Image maps in imap format.
$imgAttrs['ismap'] = true;
$out = Html::rawElement(
'a',
[ 'href' => $response->diagrams->ismap->url ],
Html::element( 'img', $imgAttrs )
);
} else {
// No image map.
$out = Html::element( 'img', $imgAttrs );
}
$error = wfMessage( 'diagrams-error-' . $response->error );
return Html::element( 'span', [ 'class' => 'error' ], $error );
return Html::rawElement( 'div', [ 'class' => 'ext-diagrams' ], $out );
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment