Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php
Show All 38 Lines | while (isset($lines[$cursor])) { | ||||
} | } | ||||
$cursor++; | $cursor++; | ||||
} | } | ||||
return $num_lines; | return $num_lines; | ||||
} | } | ||||
public function markupText($text, $children) { | public function markupText($text, $children) { | ||||
$default_lang = null; | |||||
if (preg_match('/^\s*```/', $text)) { | if (preg_match('/^\s*```/', $text)) { | ||||
// Look for GFM-style ```langname blocks and handle them | |||||
if (preg_match('/^\s*``` *([a-z]+)\n/', $text, $matches)) { | |||||
Lint: Use of Undeclared Variable: Declare variables prior to use (even if you are passing them as reference parameters). You may… | |||||
$default_lang = $matches[1]; | |||||
$text = preg_replace('/^\s*``` *([a-z]+)\n/', '', $text); | |||||
} | |||||
// If this is a ```-style block, trim off the backticks and any leading | // If this is a ```-style block, trim off the backticks and any leading | ||||
// blank line. | // blank line. | ||||
$text = preg_replace('/^\s*```(\s*\n)?/', '', $text); | $text = preg_replace('/^\s*```(\s*\n)?/', '', $text); | ||||
$text = preg_replace('/```\s*$/', '', $text); | $text = preg_replace('/```\s*$/', '', $text); | ||||
} | } | ||||
$lines = explode("\n", $text); | $lines = explode("\n", $text); | ||||
while ($lines && !strlen(last($lines))) { | while ($lines && !strlen(last($lines))) { | ||||
unset($lines[last_key($lines)]); | unset($lines[last_key($lines)]); | ||||
} | } | ||||
$options = array( | $options = array( | ||||
'counterexample' => false, | 'counterexample' => false, | ||||
'lang' => null, | 'lang' => $default_lang, | ||||
'name' => null, | 'name' => null, | ||||
'lines' => null, | 'lines' => null, | ||||
); | ); | ||||
$parser = new PhutilSimpleOptions(); | $parser = new PhutilSimpleOptions(); | ||||
$custom = $parser->parse(head($lines)); | $custom = $parser->parse(head($lines)); | ||||
if ($custom) { | if ($custom) { | ||||
$valid = true; | $valid = true; | ||||
foreach ($custom as $key => $value) { | foreach ($custom as $key => $value) { | ||||
if (!array_key_exists($key, $options)) { | if (!array_key_exists($key, $options)) { | ||||
$valid = false; | $valid = false; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
speckUnsubmitted Not Done Inline ActionsWould it make sense to put the language parsing here instead of above? Essentially if we see a key here which doesn't exist in options, whose value is empty/null, and the key matches a known syntax language then we could specify the language here. speck: Would it make sense to put the language parsing here instead of above? Essentially if we see a… | |||||
if ($valid) { | if ($valid) { | ||||
array_shift($lines); | array_shift($lines); | ||||
$options = $custom + $options; | $options = $custom + $options; | ||||
} | } | ||||
} | } | ||||
// Normalize the text back to a 0-level indent. | // Normalize the text back to a 0-level indent. | ||||
$min_indent = 80; | $min_indent = 80; | ||||
Show All 27 Lines | if ($this->getEngine()->isTextMode()) { | ||||
} | } | ||||
$text = preg_replace('/^/m', ' ', $text); | $text = preg_replace('/^/m', ' ', $text); | ||||
$out[] = $text; | $out[] = $text; | ||||
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); | ||||
speckUnsubmitted Not Done Inline ActionsIt may also make sense to update this area of the code. Possibly having the guesswork done here or at least updating the comment to clarify that this guessing should only occur if the user didn't specify lang or if we determined it was specified GFM style speck: It may also make sense to update this area of the code. Possibly having the guesswork done here… | |||||
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; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
Declare variables prior to use (even if you are passing them as reference parameters). You may have misspelled this variable name.