Skip to content

Commit

Permalink
CodeStringValueFormatter to support pretty JSON (#2173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Jan 15, 2017
1 parent bfc7b3b commit a85dc6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
40 changes: 34 additions & 6 deletions src/DataValues/ValueFormatters/CodeStringValueFormatter.php
Expand Up @@ -36,17 +36,45 @@ protected function doFormatFinalOutputFor( $type, $linker ) {

Outputs::requireResource( 'ext.smw.style' );

// This disables all active wiki and HTML markup:
$result = str_replace(
array( '<', '>', ' ', '[', '{', '=', "'", ':', "\n" ),
array( '&lt;', '&gt;', '&#160;', '&#x005B;', '&#x007B;', '&#x003D;', '&#x0027;', '&#58;', "<br />" ),
$text );
if ( $this->isJson( $text ) ) {
$result = self::formatAsPrettyJson( $text );
} else {
// This disables all active wiki and HTML markup:
$result = str_replace(
array( '<', '>', ' ', '[', '{', '=', "'", ':', "\n" ),
array( '&lt;', '&gt;', '&#160;', '&#x005B;', '&#x007B;', '&#x003D;', '&#x0027;', '&#58;', "<br />" ),
$text
);
}

if ( $abbreviate ) {
$result = "<div style=\"height:5em; overflow:auto;\">$result</div>";
$result = "<div style=\"min-height:5em; overflow:auto;\">$result</div>";
}

return "<div class=\"smwpre\">$result</div>";
}

/**
* @since 2.5
*
* @param string $string
*
* @return string
*/
public static function formatAsPrettyJson( $string ) {
return defined( 'JSON_PRETTY_PRINT' ) ? json_encode( json_decode( $string ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : $string;
}

private function isJson( $string ) {

// Don't bother
if ( substr( $string, 0, 1 ) !== '{' ) {
return false;
}

json_decode( $string );

return ( json_last_error() == JSON_ERROR_NONE );
}

}
Expand Up @@ -67,22 +67,22 @@ public function stringValueProvider() {
'foo',
CodeStringValueFormatter::WIKI_LONG,
null,
'<div class="smwpre"><div style="height:5em; overflow:auto;">foo</div></div>'
'<div class="smwpre"><div style="min-height:5em; overflow:auto;">foo</div></div>'
);

$provider[] = array(
'foo',
CodeStringValueFormatter::HTML_LONG,
null,
'<div class="smwpre"><div style="height:5em; overflow:auto;">foo</div></div>'
'<div class="smwpre"><div style="min-height:5em; overflow:auto;">foo</div></div>'
);

// > 255
$text = 'Lorem ipsum dolor sit amet consectetuer justo Nam quis lobortis vel. Sapien nulla enim Lorem enim pede ' .
'lorem nulla justo diam wisi. Libero Nam turpis neque leo scelerisque nec habitasse a lacus mattis. Accumsan ' .
'tincidunt Sed adipiscing nec facilisis tortor Nunc Sed ipsum tellus';

$expected = '<div class="smwpre"><div style="height:5em; overflow:auto;">Lorem&#160;ipsum&#160;dolor&#160;sit&#160;amet&#160;' .
$expected = '<div class="smwpre"><div style="min-height:5em; overflow:auto;">Lorem&#160;ipsum&#160;dolor&#160;sit&#160;amet&#160;' .
'consectetuer&#160;justo&#160;Nam&#160;quis&#160;lobortis&#160;vel.&#160;Sapien&#160;nulla&#160;enim&#160;Lorem&#160;enim&#160;' .
'pede&#160;lorem&#160;nulla&#160;justo&#160;diam&#160;wisi.&#160;Libero&#160;Nam&#160;turpis&#160;neque&#160;leo&#160;' .
'scelerisque&#160;nec&#160;habitasse&#160;a&#160;lacus&#160;mattis.&#160;Accumsan&#160;tincidunt&#160;Sed&#160;adipiscing&#160;' .
Expand All @@ -107,7 +107,7 @@ public function stringValueProvider() {
'<foo>',
CodeStringValueFormatter::HTML_LONG,
null,
'<div class="smwpre"><div style="height:5em; overflow:auto;">&lt;foo&gt;</div></div>'
'<div class="smwpre"><div style="min-height:5em; overflow:auto;">&lt;foo&gt;</div></div>'
);

$provider[] = array(
Expand All @@ -121,7 +121,16 @@ public function stringValueProvider() {
'*Foo',
CodeStringValueFormatter::WIKI_LONG,
null,
'<div class="smwpre"><div style="height:5em; overflow:auto;">*Foo</div></div>'
'<div class="smwpre"><div style="min-height:5em; overflow:auto;">*Foo</div></div>'
);

// JSON
$jsonString = '{"limit": 50,"offset": 0,"sort": [],"order": [],"mode": 1}';
$provider[] = array(
$jsonString,
CodeStringValueFormatter::WIKI_LONG,
null,
'<div class="smwpre"><div style="min-height:5em; overflow:auto;">' . CodeStringValueFormatter::formatAsPrettyJson( $jsonString ) . '</div></div>'
);

return $provider;
Expand Down

0 comments on commit a85dc6e

Please sign in to comment.