Skip to content

Commit b1ae1b1

Browse files
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
1 parent 27f4b83 commit b1ae1b1

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php

+19
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public function markupText($text, $children) {
153153
return implode("\n", $out);
154154
}
155155

156+
// The name is usually a sufficient source of information for file ext.
157+
if (empty($options['lang']) && isset($options['name'])) {
158+
$options['lang'] = $this->guessFilenameExtension($options['name']);
159+
}
160+
156161
if (empty($options['lang'])) {
157162
// If the user hasn't specified "lang=..." explicitly, try to guess the
158163
// language. If we fail, fall back to configured defaults.
@@ -343,4 +348,18 @@ private static function knownLanguageCodes() {
343348
return $map;
344349
}
345350

351+
/**
352+
* Get the extension from a filename.
353+
* @param string "/path/to/something.name"
354+
* @return null|string ".name"
355+
*/
356+
private function guessFilenameExtension($name) {
357+
$name = basename($name);
358+
$pos = strrpos($name, '.');
359+
if ($pos !== false) {
360+
return substr($name, $pos + 1);
361+
}
362+
return null;
363+
}
364+
346365
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name=/etc/phpmyadmin/config.txt
2+
$lol = 1;
3+
~~~~~~~~~~
4+
<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>
5+
~~~~~~~~~~
6+
name=/etc/phpmyadmin/config.txt
7+
$lol = 1;

0 commit comments

Comments
 (0)