mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-10 14:51:06 +01:00
Add a JSON document rendering engine
Summary: Depends on D19254. This engine just formats JSON files in a nicer, more readable way. Test Plan: Looked at some JSON files, saw them become formatted nicely. Reviewers: mydeveloperday Reviewed By: mydeveloperday Differential Revision: https://secure.phabricator.com/D19255
This commit is contained in:
parent
d2727d24da
commit
4906364751
3 changed files with 65 additions and 0 deletions
|
@ -3187,6 +3187,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorIteratorFileUploadSource' => 'applications/files/uploadsource/PhabricatorIteratorFileUploadSource.php',
|
||||
'PhabricatorJIRAAuthProvider' => 'applications/auth/provider/PhabricatorJIRAAuthProvider.php',
|
||||
'PhabricatorJSONConfigType' => 'applications/config/type/PhabricatorJSONConfigType.php',
|
||||
'PhabricatorJSONDocumentEngine' => 'applications/files/document/PhabricatorJSONDocumentEngine.php',
|
||||
'PhabricatorJSONExportFormat' => 'infrastructure/export/format/PhabricatorJSONExportFormat.php',
|
||||
'PhabricatorJavelinLinter' => 'infrastructure/lint/linter/PhabricatorJavelinLinter.php',
|
||||
'PhabricatorJiraIssueHasObjectEdgeType' => 'applications/doorkeeper/edge/PhabricatorJiraIssueHasObjectEdgeType.php',
|
||||
|
@ -8800,6 +8801,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorIteratorFileUploadSource' => 'PhabricatorFileUploadSource',
|
||||
'PhabricatorJIRAAuthProvider' => 'PhabricatorOAuth1AuthProvider',
|
||||
'PhabricatorJSONConfigType' => 'PhabricatorTextConfigType',
|
||||
'PhabricatorJSONDocumentEngine' => 'PhabricatorTextDocumentEngine',
|
||||
'PhabricatorJSONExportFormat' => 'PhabricatorExportFormat',
|
||||
'PhabricatorJavelinLinter' => 'ArcanistLinter',
|
||||
'PhabricatorJiraIssueHasObjectEdgeType' => 'PhabricatorEdgeType',
|
||||
|
|
|
@ -116,6 +116,10 @@ final class PhabricatorDocumentRef
|
|||
}
|
||||
|
||||
$snippet = $this->getSnippet();
|
||||
if (!preg_match('/^\s*[{[]/', $snippet)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return phutil_is_utf8($snippet);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorJSONDocumentEngine
|
||||
extends PhabricatorTextDocumentEngine {
|
||||
|
||||
const ENGINEKEY = 'json';
|
||||
|
||||
public function getViewAsLabel(PhabricatorDocumentRef $ref) {
|
||||
return pht('View as JSON');
|
||||
}
|
||||
|
||||
protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
|
||||
return 'fa-database';
|
||||
}
|
||||
|
||||
protected function getContentScore(PhabricatorDocumentRef $ref) {
|
||||
if (preg_match('/\.json\z/', $ref->getName())) {
|
||||
return 2000;
|
||||
}
|
||||
|
||||
if ($ref->isProbablyJSON()) {
|
||||
return 1750;
|
||||
}
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
||||
protected function newDocumentContent(PhabricatorDocumentRef $ref) {
|
||||
$raw_data = $this->loadTextData($ref);
|
||||
|
||||
try {
|
||||
$data = phutil_json_decode($raw_data);
|
||||
|
||||
if (preg_match('/^\s*\[/', $raw_data)) {
|
||||
$content = id(new PhutilJSON())->encodeAsList($data);
|
||||
} else {
|
||||
$content = id(new PhutilJSON())->encodeFormatted($data);
|
||||
}
|
||||
|
||||
$message = null;
|
||||
$content = PhabricatorSyntaxHighlighter::highlightWithLanguage(
|
||||
'json',
|
||||
$content);
|
||||
} catch (PhutilJSONParserException $ex) {
|
||||
$message = $this->newMessage(
|
||||
pht(
|
||||
'This document is not valid JSON: %s',
|
||||
$ex->getMessage()));
|
||||
|
||||
$content = $raw_data;
|
||||
}
|
||||
|
||||
return array(
|
||||
$message,
|
||||
$this->newTextDocumentContent($content),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue