3 abstract class PhabricatorTextDocumentEngine
4 extends PhabricatorDocumentEngine
{
6 private $encodingMessage = null;
8 protected function canRenderDocumentType(PhabricatorDocumentRef
$ref) {
9 return $ref->isProbablyText();
12 public function canConfigureEncoding(PhabricatorDocumentRef
$ref) {
16 protected function newTextDocumentContent(
17 PhabricatorDocumentRef
$ref,
19 array $options = array()) {
21 PhutilTypeSpec
::checkMap(
24 'blame' => 'optional wild',
25 'coverage' => 'optional list<wild>',
28 if (is_array($content)) {
31 $lines = phutil_split_lines($content);
34 $view = id(new PhabricatorSourceCodeView())
35 ->setHighlights($this->getHighlightedLines())
37 ->setSymbolMetadata($ref->getSymbolMetadata());
39 $blame = idx($options, 'blame');
40 if ($blame !== null) {
41 $view->setBlameMap($blame);
44 $coverage = idx($options, 'coverage');
45 if ($coverage !== null) {
46 $view->setCoverage($coverage);
50 if ($this->encodingMessage
!== null) {
51 $message = $this->newMessage($this->encodingMessage
);
54 $container = phutil_tag(
57 'class' => 'document-engine-text',
67 protected function loadTextData(PhabricatorDocumentRef
$ref) {
68 $content = $ref->loadData();
70 $encoding = $this->getEncodingConfiguration();
71 if ($encoding !== null) {
72 if (function_exists('mb_convert_encoding')) {
73 $content = mb_convert_encoding($content, 'UTF-8', $encoding);
74 $this->encodingMessage
= pht(
75 'This document was converted from %s to UTF8 for display.',
78 $this->encodingMessage
= pht(
79 'Unable to perform text encoding conversion: mbstring extension '.
83 if (!phutil_is_utf8($content)) {
84 if (function_exists('mb_detect_encoding')) {
85 $try_encodings = array(
87 'EUC-JP' => pht('EUC-JP'),
88 'SJIS' => pht('Shift JIS'),
89 'ISO-8859-1' => pht('ISO-8859-1 (Latin 1)'),
92 $guess = mb_detect_encoding($content, array_keys($try_encodings));
94 $content = mb_convert_encoding($content, 'UTF-8', $guess);
95 $this->encodingMessage
= pht(
96 'This document is not UTF8. It was detected as %s and '.
97 'converted to UTF8 for display.',
98 idx($try_encodings, $guess, $guess));
104 if (!phutil_is_utf8($content)) {
105 $content = phutil_utf8ize($content);
106 $this->encodingMessage
= pht(
107 'This document is not UTF8 and its text encoding could not be '.
108 'detected automatically. Use "Change Text Encoding..." to choose '.