@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 115 lines 2.9 kB view raw
1<?php 2 3final class DiffusionReadmeView extends DiffusionView { 4 5 private $path; 6 private $content; 7 8 public function setPath($path) { 9 $this->path = $path; 10 return $this; 11 } 12 13 public function getPath() { 14 return $this->path; 15 } 16 17 public function setContent($content) { 18 $this->content = $content; 19 return $this; 20 } 21 22 public function getContent() { 23 return $this->content; 24 } 25 26 /** 27 * Get the markup language a README should be interpreted as. 28 * 29 * @param string $path Local README path, like "README.txt". 30 * @return string Best markup interpreter (like "remarkup") for this file. 31 */ 32 private function getReadmeLanguage($path) { 33 $path = phutil_utf8_strtolower($path); 34 if ($path == 'readme') { 35 return 'remarkup'; 36 } 37 38 $ext = last(explode('.', $path)); 39 switch ($ext) { 40 case 'remarkup': 41 case 'md': 42 return 'remarkup'; 43 case 'rainbow': 44 return 'rainbow'; 45 case 'txt': 46 default: 47 return 'text'; 48 } 49 } 50 51 52 public function render() { 53 $readme_path = $this->getPath(); 54 $readme_name = basename($readme_path); 55 $interpreter = $this->getReadmeLanguage($readme_name); 56 require_celerity_resource('diffusion-readme-css'); 57 58 $content = $this->getContent(); 59 60 $class = null; 61 switch ($interpreter) { 62 case 'remarkup': 63 // TODO: This is sketchy, but make sure we hit the markup cache. 64 $markup_object = id(new PhabricatorMarkupOneOff()) 65 ->setEngineRuleset('diffusion-readme') 66 ->setContent($content); 67 $markup_field = 'default'; 68 69 $content = id(new PhabricatorMarkupEngine()) 70 ->setViewer($this->getUser()) 71 ->addObject($markup_object, $markup_field) 72 ->process() 73 ->getOutput($markup_object, $markup_field); 74 75 $engine = $markup_object->newMarkupEngine($markup_field); 76 77 $readme_content = $content; 78 $class = 'ml'; 79 break; 80 case 'rainbow': 81 $content = id(new PhutilRainbowSyntaxHighlighter()) 82 ->getHighlightFuture($content) 83 ->resolve(); 84 $readme_content = phutil_escape_html_newlines($content); 85 86 require_celerity_resource('syntax-highlighting-css'); 87 $class = 'remarkup-code ml'; 88 break; 89 default: 90 case 'text': 91 $readme_content = phutil_escape_html_newlines($content); 92 $class = 'ml'; 93 break; 94 } 95 96 $readme_content = phutil_tag( 97 'div', 98 array( 99 'class' => $class, 100 ), 101 $readme_content); 102 103 $header = id(new PHUIHeaderView()) 104 ->setHeader($readme_name) 105 ->addClass('diffusion-panel-header-view'); 106 107 return id(new PHUIObjectBoxView()) 108 ->setHeader($header) 109 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 110 ->addClass('diffusion-mobile-view') 111 ->appendChild($readme_content) 112 ->addClass('diffusion-readme-view'); 113 } 114 115}