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 @@ -44,11 +44,18 @@ } public function markupText($text, $children) { - if (preg_match('/^\s*```/', $text)) { + $header_line = null; + if (preg_match('/^\s*```(.*)/', $text, $matches)) { // If this is a ```-style block, trim off the backticks and any leading // blank line. $text = preg_replace('/^\s*```(\s*\n)?/', '', $text); $text = preg_replace('/```\s*$/', '', $text); + + // Just after the opening backticks there can be something valuable. + // Note that the general options are not necessarily here. + if (isset($matches[1])) { + $header_line = $matches[1]; + } } $lines = explode("\n", $text); @@ -79,6 +86,21 @@ } } + // Language expressed as Flavored Markdown + // If language was not specified using lang=something, we parse + // the thing after the first backticks. + // We skip one-line blocks, to do not eat potential one-line content. + // If found, we remove that line since it is not part of the content. + // In this early version we are somehow strict and we do not support + // c# or c++ or enttalk/2 etc - but you can 'csharp', 'cpp', etc. + // The goal is to avoid false positives taking away content. + if (empty($options['lang']) && $header_line && count($lines) > 1) { + if (preg_match('/^[a-z][a-z0-9\-]*$/', $header_line)) { + $options['lang'] = $header_line; + array_shift($lines); + } + } + // Normalize the text back to a 0-level indent. $min_indent = 80; foreach ($lines as $line) { diff --git a/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php b/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php --- a/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php +++ b/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php @@ -2,6 +2,8 @@ /** * Test cases for @{class:PhutilRemarkupEngine}. + * @TODO: This unit is not always triggered when you need it. + * https://we.phorge.it/T15500 */ final class PhutilRemarkupEngineTestCase extends PhutilTestCase { diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-flavored.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-flavored.txt new file mode 100644 --- /dev/null +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-flavored.txt @@ -0,0 +1,6 @@ +```cpp +code``` +~~~~~~~~~~ +
code
+~~~~~~~~~~ + code diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-comment.txt copy from src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt copy to src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-comment.txt --- a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-comment.txt @@ -1,18 +1,18 @@ -```code - -more code +```#comment +code +#more comment more code``` ~~~~~~~~~~ -
code
-
-more code
+
#comment
+code
 
+#more comment
 more code
~~~~~~~~~~ + #comment code - more code - + #more comment more code diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-empty.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-empty.txt new file mode 100644 --- /dev/null +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-empty.txt @@ -0,0 +1,9 @@ +``` +cpp +second line``` +~~~~~~~~~~ +
cpp
+second line
+~~~~~~~~~~ + cpp + second line diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored.txt new file mode 100644 --- /dev/null +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored.txt @@ -0,0 +1,19 @@ +```cpp +code + +more code + +more code``` + +~~~~~~~~~~ +
code
+
+more code
+
+more code
+~~~~~~~~~~ + code + + more code + + more code diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt --- a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi.txt @@ -1,17 +1,17 @@ -```code +```code code more code more code``` ~~~~~~~~~~ -
code
+
code code
 
 more code
 
 more code
~~~~~~~~~~ - code + code code more code