From 49063647515cabaf6bf9a0c4249c5ce357871240 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 23 Mar 2018 08:00:35 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 2 + .../files/document/PhabricatorDocumentRef.php | 4 ++ .../PhabricatorJSONDocumentEngine.php | 59 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/applications/files/document/PhabricatorJSONDocumentEngine.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 0e2ff3f0fa..4c6efe4be2 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/applications/files/document/PhabricatorDocumentRef.php b/src/applications/files/document/PhabricatorDocumentRef.php index e27d164cd7..cca0c102e2 100644 --- a/src/applications/files/document/PhabricatorDocumentRef.php +++ b/src/applications/files/document/PhabricatorDocumentRef.php @@ -116,6 +116,10 @@ final class PhabricatorDocumentRef } $snippet = $this->getSnippet(); + if (!preg_match('/^\s*[{[]/', $snippet)) { + return false; + } + return phutil_is_utf8($snippet); } diff --git a/src/applications/files/document/PhabricatorJSONDocumentEngine.php b/src/applications/files/document/PhabricatorJSONDocumentEngine.php new file mode 100644 index 0000000000..331a7e6820 --- /dev/null +++ b/src/applications/files/document/PhabricatorJSONDocumentEngine.php @@ -0,0 +1,59 @@ +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), + ); + } + +}