Skip to content

Commit e16019a

Browse files
committed
code_samples_usage.php: handle remove_indent
1 parent 980e3a6 commit e16019a

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tools/code_samples/code_samples_usage.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ function getBlockContents(array $block): array
192192
if ('include_code' === $matches['function'][$matchIndex] && !empty($matches['indent_level'][$matchIndex])) {
193193
$matches['indent_level'][$matchIndex] = str_repeat(' ', $matches['indent_level'][$matchIndex]);
194194
}
195+
if ('True' === $matches['remove_indent'][$matchIndex]) {
196+
$removable = null;
197+
foreach ($sample as $line) {
198+
if ('' !== $line) {
199+
$removable = min($removable ?? strlen($line), strlen($line) - strlen(ltrim($line)));
200+
}
201+
}
202+
if ($removable) {
203+
foreach ($sample as $n => $line) {
204+
$sample[$n] = substr($line, $removable);
205+
}
206+
}
207+
}
195208
$solvedLine = str_replace($matchString, implode(PHP_EOL . $matches['indent_level'][$matchIndex], $sample) . ('include_code' === $matches['function'][$matchIndex] ? '' : PHP_EOL), $solvedLine);
196209
}
197210
$rawBlockCodeLines = array_merge($rawBlockCodeLines, explode(PHP_EOL, $solvedLine));

0 commit comments

Comments
 (0)