Changeset View
Changeset View
Standalone View
Standalone View
src/markup/engine/remarkup/blockrule/PhutilRemarkupCodeBlockRule.php
Show First 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | if ($this->getEngine()->isTextMode()) { | ||||
return implode("\n", $out); | return implode("\n", $out); | ||||
} | } | ||||
if (empty($options['lang'])) { | if (empty($options['lang'])) { | ||||
// If the user hasn't specified "lang=..." explicitly, try to guess the | // If the user hasn't specified "lang=..." explicitly, try to guess the | ||||
// language. If we fail, fall back to configured defaults. | // language. If we fail, fall back to configured defaults. | ||||
$lang = PhutilLanguageGuesser::guessLanguage($text); | $lang = PhutilLanguageGuesser::guessLanguage($text); | ||||
if (!$lang && $options['name']) { | |||||
$lang = $this->getHighlighterEngine() | |||||
->getLanguageFromFilename($options['name']); | |||||
} | |||||
if (!$lang) { | if (!$lang) { | ||||
$lang = nonempty( | $lang = nonempty( | ||||
$this->getEngine()->getConfig('phutil.codeblock.language-default'), | $this->getEngine()->getConfig('phutil.codeblock.language-default'), | ||||
'text'); | 'text'); | ||||
} | } | ||||
$options['lang'] = $lang; | $options['lang'] = $lang; | ||||
} | } | ||||
$code_body = $this->highlightSource($text, $options); | $code_body = $this->highlightSource($text, $options); | ||||
$name_header = null; | $name_header = null; | ||||
$block_style = null; | $block_style = null; | ||||
if ($this->getEngine()->isHTMLMailMode()) { | if ($this->getEngine()->isHTMLMailMode()) { | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | if ($options['lines']) { | ||||
$height = max(6, (int)$options['lines']); | $height = max(6, (int)$options['lines']); | ||||
$aux_style = $aux_style | $aux_style = $aux_style | ||||
.' ' | .' ' | ||||
.'max-height: ' | .'max-height: ' | ||||
.(2 * $height) | .(2 * $height) | ||||
.'em; overflow: auto;'; | .'em; overflow: auto;'; | ||||
} | } | ||||
$engine = $this->getEngine()->getConfig('syntax-highlighter.engine'); | |||||
if (!$engine) { | |||||
$engine = 'PhutilDefaultSyntaxHighlighterEngine'; | |||||
} | |||||
$engine = newv($engine, array()); | |||||
$engine->setConfig( | |||||
'pygments.enabled', | |||||
$this->getEngine()->getConfig('pygments.enabled')); | |||||
return phutil_tag( | return phutil_tag( | ||||
'pre', | 'pre', | ||||
array( | array( | ||||
'class' => 'remarkup-code'.$aux_class, | 'class' => 'remarkup-code'.$aux_class, | ||||
'style' => $aux_style, | 'style' => $aux_style, | ||||
), | ), | ||||
PhutilSafeHTML::applyFunction( | PhutilSafeHTML::applyFunction( | ||||
'rtrim', | 'rtrim', | ||||
$engine->highlightSource($options['lang'], $text))); | $this->getHighlighterEngine() | ||||
->highlightSource($options['lang'], $text))); | |||||
} | |||||
private function getHighlighterEngine() { | |||||
$engine = $this->getEngine()->getConfig('syntax-highlighter.engine'); | |||||
if (!$engine) { | |||||
$engine = 'PhutilDefaultSyntaxHighlighterEngine'; | |||||
} | |||||
$engine = newv($engine, array()); | |||||
$engine->setConfig( | |||||
'pygments.enabled', | |||||
$this->getEngine()->getConfig('pygments.enabled')); | |||||
$engine->setConfig( | |||||
'filename.map', | |||||
$this->getEngine()->getConfig('filename.map')); | |||||
return $engine; | |||||
} | } | ||||
} | } |