1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-02 11:42:42 +01:00
phorge-phorge/src/applications/diffusion/view/DiffusionReadmeView.php
epriestley 5305ebddda To improve wrapping behavior of rendered README files, don't use "PHUIDocumentView" in Diffusion
Summary:
See PHI1268. We currently do some weird width handling when rendering Diffusion readmes in a document directory view.

I think this came from D12330, which used `PHUIDocumentViewPro` to change the font, but we later reverted the font and were left with the `DocumentView`. Other changes after that modified `DocumentView` to have fixed-width behavior, but it doesn't make much sense here since the content panel is clearly rendered full-width.

Today, the `DocumentView` is a more structural element with methods like `setCurtain()`. Just get rid of it to simplify things, at least as a first step.

Test Plan:
Before:

{F6463493}

After:

{F6463492}

Reviewers: amckinley

Reviewed By: amckinley

Differential Revision: https://secure.phabricator.com/D20536
2019-05-21 12:18:18 -07:00

115 lines
2.9 KiB
PHP

<?php
final class DiffusionReadmeView extends DiffusionView {
private $path;
private $content;
public function setPath($path) {
$this->path = $path;
return $this;
}
public function getPath() {
return $this->path;
}
public function setContent($content) {
$this->content = $content;
return $this;
}
public function getContent() {
return $this->content;
}
/**
* Get the markup language a README should be interpreted as.
*
* @param string Local README path, like "README.txt".
* @return string Best markup interpreter (like "remarkup") for this file.
*/
private function getReadmeLanguage($path) {
$path = phutil_utf8_strtolower($path);
if ($path == 'readme') {
return 'remarkup';
}
$ext = last(explode('.', $path));
switch ($ext) {
case 'remarkup':
case 'md':
return 'remarkup';
case 'rainbow':
return 'rainbow';
case 'txt':
default:
return 'text';
}
}
public function render() {
$readme_path = $this->getPath();
$readme_name = basename($readme_path);
$interpreter = $this->getReadmeLanguage($readme_name);
require_celerity_resource('diffusion-readme-css');
$content = $this->getContent();
$class = null;
switch ($interpreter) {
case 'remarkup':
// TODO: This is sketchy, but make sure we hit the markup cache.
$markup_object = id(new PhabricatorMarkupOneOff())
->setEngineRuleset('diffusion-readme')
->setContent($content);
$markup_field = 'default';
$content = id(new PhabricatorMarkupEngine())
->setViewer($this->getUser())
->addObject($markup_object, $markup_field)
->process()
->getOutput($markup_object, $markup_field);
$engine = $markup_object->newMarkupEngine($markup_field);
$readme_content = $content;
$class = 'ml';
break;
case 'rainbow':
$content = id(new PhutilRainbowSyntaxHighlighter())
->getHighlightFuture($content)
->resolve();
$readme_content = phutil_escape_html_newlines($content);
require_celerity_resource('syntax-highlighting-css');
$class = 'remarkup-code ml';
break;
default:
case 'text':
$readme_content = phutil_escape_html_newlines($content);
$class = 'ml';
break;
}
$readme_content = phutil_tag(
'div',
array(
'class' => $class,
),
$readme_content);
$header = id(new PHUIHeaderView())
->setHeader($readme_name)
->addClass('diffusion-panel-header-view');
return id(new PHUIObjectBoxView())
->setHeader($header)
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
->addClass('diffusion-mobile-view')
->appendChild($readme_content)
->addClass('diffusion-readme-view');
}
}