diff --git a/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php b/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php --- a/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php +++ b/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php @@ -153,6 +153,11 @@ return implode("\n", $out); } + // The name is usually a sufficient source of information for file ext. + if (empty($options['lang']) && isset($options['name'])) { + $options['lang'] = $this->guessFilenameExtension($options['name']); + } + if (empty($options['lang'])) { // If the user hasn't specified "lang=..." explicitly, try to guess the // language. If we fail, fall back to configured defaults. @@ -343,4 +348,18 @@ return $map; } + /** + * Get the extension from a filename. + * @param string "/path/to/something.name" + * @return null|string ".name" + */ + private function guessFilenameExtension($name) { + $name = basename($name); + $pos = strrpos($name, '.'); + if ($pos !== false) { + return substr($name, $pos + 1); + } + return null; + } + } diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/code-block-guess-from-name.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/code-block-guess-from-name.txt new file mode 100644 --- /dev/null +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/code-block-guess-from-name.txt @@ -0,0 +1,7 @@ + name=/etc/phpmyadmin/config.txt + $lol = 1; +~~~~~~~~~~ +
$lol = 1;