Skip to content

Commit ff97459

Browse files
committed
code_samples_usage.php: handle named parameters
1 parent 934ddc4 commit ff97459

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

tools/code_samples/code_samples_usage.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function getBlockContents(array $block): array
148148
}
149149
}
150150
} elseif (str_contains($blockSourceLine, '[[= include_file') || str_contains($blockSourceLine, '[[= include_code')) {
151-
preg_match_all("@\[\[= (?<function>include_file|include_code)\('(?<file>[^']+)'(, (?<start>[0-9]+)(, (?<end>([0-9]+|None))(, '(?<glue>[^']+)')?)?)?\) =\]\]@", $blockSourceLine, $matches);
151+
preg_match_all("@\[\[= (?<function>include_file|include_code)\('(?<file>[^']+)'(, (?<start>[^,\)]+)(, (?<end>([^,\)]+|None))(, (?<indent_level>[^,\)]+)(, (?<remove_indent>[^,\)]+))?)?)?)?\) =\]\]@", $blockSourceLine, $matches);
152152
$solvedLine = $blockSourceLine;
153153
if (empty($matches['file'])) {
154154
throw new RuntimeException("The following line doesn't include file correctly: $blockSourceLine");
@@ -161,6 +161,23 @@ function getBlockContents(array $block): array
161161
throw new RuntimeException("The following included file can't be opened: $includedFilePath");
162162
}
163163
}
164+
165+
// Named argument mapping
166+
foreach(['start', 'end', 'indent_level', 'remove_indent'] as $key) {
167+
if (str_starts_with($matches[$key][$matchIndex], 'glue=')) {
168+
$matches[$key][$matchIndex] = '';
169+
$matches['indent_level'][$matchIndex] = str_replace('glue=', '', $matches['start'][$matchIndex]);
170+
}
171+
else if (str_starts_with($matches[$key][$matchIndex], 'indent_level=')) {
172+
$matches[$key][$matchIndex] = '';
173+
$matches['indent_level'][$matchIndex] = str_replace('indent_level=', '', $matches['start'][$matchIndex]);
174+
}
175+
else if (str_starts_with($matches[$key][$matchIndex], 'remove_indent=')) {
176+
$matches[$key][$matchIndex] = '';
177+
$matches['remove_indent'][$matchIndex] = str_replace('remove_indent=', '', $matches['start'][$matchIndex]);
178+
}
179+
}
180+
164181
if ('None' === $matches['end'][$matchIndex]) {
165182
$matches['end'][$matchIndex] = count($includedFilesLines[$includedFilePath]);
166183
}
@@ -172,10 +189,10 @@ function getBlockContents(array $block): array
172189
}
173190
$sample = array_slice($includedFilesLines[$includedFilePath], (int)$matches['start'][$matchIndex], (int)$matches['end'][$matchIndex] - (int)$matches['start'][$matchIndex]);
174191
}
175-
if ('include_code' === $matches['function'][$matchIndex] && !empty($matches['glue'][$matchIndex])) {
176-
$matches['glue'][$matchIndex] = str_repeat(' ', $matches['glue'][$matchIndex]);
192+
if ('include_code' === $matches['function'][$matchIndex] && !empty($matches['indent_level'][$matchIndex])) {
193+
$matches['indent_level'][$matchIndex] = str_repeat(' ', $matches['indent_level'][$matchIndex]);
177194
}
178-
$solvedLine = str_replace($matchString, implode(PHP_EOL . $matches['glue'][$matchIndex], $sample) . ('include_code' === $matches['function'][$matchIndex] ? '' : PHP_EOL), $solvedLine);
195+
$solvedLine = str_replace($matchString, implode(PHP_EOL . $matches['indent_level'][$matchIndex], $sample) . ('include_code' === $matches['function'][$matchIndex] ? '' : PHP_EOL), $solvedLine);
179196
}
180197
$rawBlockCodeLines = array_merge($rawBlockCodeLines, explode(PHP_EOL, $solvedLine));
181198
} elseif (str_contains($blockSourceLine, '--8<--')) {

0 commit comments

Comments
 (0)