1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 08:12:40 +01:00

Remarkup code blocks: guess language from "name="

Summary:
The file name is a sufficient source of information, if available.

Closes T15729

Test Plan: The new unit test is green. Old unit tests are green.

Reviewers: O1 Blessed Committers, avivey

Reviewed By: O1 Blessed Committers, avivey

Subscribers: avivey, tobiaswiese, Matthew, Cigaryno

Maniphest Tasks: T15729

Differential Revision: https://we.phorge.it/D25560
This commit is contained in:
Valerio Bozzolan 2024-03-26 11:19:53 +01:00
parent 27f4b8321a
commit b1ae1b1a67
2 changed files with 26 additions and 0 deletions

View file

@ -153,6 +153,11 @@ final class PhutilRemarkupCodeBlockRule extends PhutilRemarkupBlockRule {
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 @@ final class PhutilRemarkupCodeBlockRule extends PhutilRemarkupBlockRule {
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;
}
}

View file

@ -0,0 +1,7 @@
name=/etc/phpmyadmin/config.txt
$lol = 1;
~~~~~~~~~~
<div class="remarkup-code-block" data-code-lang="txt" data-sigil="remarkup-code-block"><div class="remarkup-code-header">/etc/phpmyadmin/config.txt</div><pre class="remarkup-code">$lol = 1;</pre></div>
~~~~~~~~~~
name=/etc/phpmyadmin/config.txt
$lol = 1;