Created
May 28, 2020 22:27
-
-
Save zengxs/b5fb79e85b59f1c27472923feb8cc3ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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': | |
return 'remarkup'; | |
case 'rainbow': | |
return 'rainbow'; | |
case 'md': | |
return 'markdown'; | |
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; | |
case 'markdown': | |
list($stdout, $stderr) = id(new ExecFuture('exec pandoc --from=gfm --to=html5 -F mdfilter')) | |
->setTimeout(15) | |
->write($content) | |
->resolvex(); | |
$readme_content = phutil_safe_html($stdout); | |
$class = 'phabricator-remarkup ml'; | |
break; | |
case 'text': | |
default: | |
$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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pandocfilters import toJSONFilters | |
if __name__ == '__main__': | |
return toJSONFilters([]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment