[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class DiffusionReadmeQueryConduitAPIMethod 4 extends DiffusionQueryConduitAPIMethod { 5 6 public function getAPIMethodName() { 7 return 'diffusion.readmequery'; 8 } 9 10 public function getMethodDescription() { 11 return 12 pht('Retrieve any "readme" that can be found for a set of paths in '. 13 'repository.'); 14 } 15 16 public function defineReturnType() { 17 return 'string'; 18 } 19 20 protected function defineCustomParamTypes() { 21 return array( 22 'paths' => 'required array <string>', 23 'commit' => 'optional string', 24 ); 25 } 26 27 protected function getResult(ConduitAPIRequest $request) { 28 $drequest = $this->getDiffusionRequest(); 29 $path_dicts = $request->getValue('paths', array()); 30 $paths = array(); 31 foreach ($path_dicts as $dict) { 32 $paths[] = DiffusionRepositoryPath::newFromDictionary($dict); 33 } 34 35 $best = -1; 36 $readme = ''; 37 $best_render_type = 'plain'; 38 foreach ($paths as $result_path) { 39 $file_type = $result_path->getFileType(); 40 if (($file_type != ArcanistDiffChangeType::FILE_NORMAL) && 41 ($file_type != ArcanistDiffChangeType::FILE_TEXT)) { 42 // Skip directories, etc. 43 continue; 44 } 45 46 $path = strtolower($result_path->getPath()); 47 48 if ($path === 'readme') { 49 $path .= '.remarkup'; 50 } 51 52 if (strncmp($path, 'readme.', 7) !== 0) { 53 continue; 54 } 55 56 $priority = 0; 57 switch (substr($path, 7)) { 58 case 'remarkup': 59 $priority = 100; 60 $render_type = 'remarkup'; 61 break; 62 case 'rainbow': 63 $priority = 90; 64 $render_type = 'rainbow'; 65 break; 66 case 'md': 67 $priority = 50; 68 $render_type = 'remarkup'; 69 break; 70 case 'txt': 71 $priority = 10; 72 $render_type = 'plain'; 73 break; 74 default: 75 $priority = 0; 76 $render_type = 'plain'; 77 break; 78 } 79 80 if ($priority > $best) { 81 $best = $priority; 82 $readme = $result_path; 83 $best_render_type = $render_type; 84 } 85 } 86 87 if (!$readme) { 88 return ''; 89 } 90 91 $readme_request = DiffusionRequest::newFromDictionary( 92 array( 93 'user' => $request->getUser(), 94 'repository' => $drequest->getRepository(), 95 'commit' => $drequest->getStableCommit(), 96 'path' => $readme->getFullPath(), 97 )); 98 99 $file_content = DiffusionFileContent::newFromConduit( 100 DiffusionQuery::callConduitWithDiffusionRequest( 101 $request->getUser(), 102 $readme_request, 103 'diffusion.filecontentquery', 104 array( 105 'commit' => $drequest->getStableCommit(), 106 'path' => $readme->getFullPath(), 107 'needsBlame' => false, 108 ))); 109 $readme_content = $file_content->getCorpus(); 110 111 switch ($best_render_type) { 112 case 'plain': 113 $readme_content = phutil_escape_html_newlines($readme_content); 114 $class = null; 115 break; 116 case 'rainbow': 117 $highlighter = new PhutilRainbowSyntaxHighlighter(); 118 $readme_content = $highlighter 119 ->getHighlightFuture($readme_content) 120 ->resolve(); 121 $readme_content = phutil_escape_html_newlines($readme_content); 122 123 require_celerity_resource('syntax-highlighting-css'); 124 $class = 'remarkup-code'; 125 break; 126 case 'remarkup': 127 // TODO: This is sketchy, but make sure we hit the markup cache. 128 $markup_object = id(new PhabricatorMarkupOneOff()) 129 ->setEngineRuleset('diffusion-readme') 130 ->setContent($readme_content); 131 $markup_field = 'default'; 132 133 $readme_content = id(new PhabricatorMarkupEngine()) 134 ->setViewer($request->getUser()) 135 ->addObject($markup_object, $markup_field) 136 ->process() 137 ->getOutput($markup_object, $markup_field); 138 139 $engine = $markup_object->newMarkupEngine($markup_field); 140 $toc = PhutilRemarkupHeaderBlockRule::renderTableOfContents($engine); 141 if ($toc) { 142 $toc = phutil_tag_div( 143 'phabricator-remarkup-toc', 144 array( 145 phutil_tag_div( 146 'phabricator-remarkup-toc-header', 147 pht('Table of Contents')), 148 $toc, 149 )); 150 $readme_content = array($toc, $readme_content); 151 } 152 153 $class = 'phabricator-remarkup'; 154 break; 155 } 156 157 $readme_content = phutil_tag( 158 'div', 159 array( 160 'class' => $class, 161 ), 162 $readme_content); 163 164 return $readme_content; 165 } 166 167 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |