Skip to content

Commit 558fd24

Browse files
committed
code_samples_usage.php: Update for include_code
1 parent 3fa8a8a commit 558fd24

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

tools/code_samples/code_samples_usage.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function displayBlocks(array $docFileBlocks, ?string $docFilePath = null, $lineO
3535
try {
3636
$blockContents = getBlockContents($block);
3737
foreach ($blockContents['contents'] as $contentLineNumber => $contentLine) {
38-
$prefixedBlockContentLines[] = str_pad($contentLineNumber, 3, 0, STR_PAD_LEFT) . (in_array($contentLineNumber, $blockContents['highlights']) ? '❇️' : '') . $contentLine;
38+
$prefixedBlockContentLines[] = str_pad($contentLineNumber, 3, '0', STR_PAD_LEFT) . (in_array($contentLineNumber, $blockContents['highlights']) ? '❇️' : '') . $contentLine;
3939
}
4040
echo implode(PHP_EOL, $prefixedBlockContentLines) . PHP_EOL . PHP_EOL;
4141
} catch (Exception $exception) {
@@ -55,9 +55,9 @@ function displayBlocks(array $docFileBlocks, ?string $docFilePath = null, $lineO
5555
*/
5656
function getIncludingFileList(?string $codeSampleFilePath = null): array
5757
{
58-
$pattern = null === $codeSampleFilePath ? '= include_file' : $codeSampleFilePath;
58+
$pattern = null === $codeSampleFilePath ? '= (include_file|include_code)' : $codeSampleFilePath;
5959
$pattern = escapeshellarg($pattern);
60-
$command = "grep $pattern -Rl docs | sort";
60+
$command = "grep -E $pattern -Rl docs.tmp | sort";
6161
exec($command, $rawIncludingFileList, $commandResultCode);
6262
if (0 === $commandResultCode) {
6363
return $rawIncludingFileList;
@@ -73,7 +73,7 @@ function getIncludingFileList(?string $codeSampleFilePath = null): array
7373
*/
7474
function getInclusionBlocks(string $docFilePath, ?string $codeSampleFilePath = null): array
7575
{
76-
$pattern = null === $codeSampleFilePath ? '= include_file' : $codeSampleFilePath;
76+
$pattern = null === $codeSampleFilePath ? '@= (include_file|include_code)@' : "@$codeSampleFilePath@";
7777

7878
$docFileLines = file($docFilePath, FILE_IGNORE_NEW_LINES);
7979
if (!$docFileLines) {
@@ -87,7 +87,7 @@ function getInclusionBlocks(string $docFilePath, ?string $codeSampleFilePath = n
8787
if ($includingFileLineIndex <= $blockEndingLineIndex + 1) {
8888
continue;
8989
}
90-
if (str_contains($includingFileLine, $pattern)) {
90+
if (preg_match($pattern, $includingFileLine)) {
9191
for ($blockStartingLineIndex = $includingFileLineIndex - 1; 0 <= $blockStartingLineIndex; $blockStartingLineIndex--) {
9292
$previousLine = $docFileLines[$blockStartingLineIndex];
9393
if (str_contains($previousLine, '```')) {
@@ -147,8 +147,8 @@ function getBlockContents(array $block): array
147147
$blockHighlightedLines[] = (int)$rawHighlightedLine;
148148
}
149149
}
150-
} elseif (str_contains($blockSourceLine, '[[= include_file')) {
151-
preg_match_all("@\[\[= include_file\('(?<file>[^']+)'(, (?<start>[0-9]+)(, (?<end>([0-9]+|None))(, '(?<glue>[^']+)')?)?)?\) =\]\]@", $blockSourceLine, $matches);
150+
} 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);
152152
$solvedLine = $blockSourceLine;
153153
if (empty($matches['file'])) {
154154
throw new RuntimeException("The following line doesn't include file correctly: $blockSourceLine");
@@ -167,9 +167,15 @@ function getBlockContents(array $block): array
167167
if ('' === $matches['start'][$matchIndex]) {
168168
$sample = $includedFilesLines[$includedFilePath];
169169
} else {
170+
if ('include_code' === $matches['function'][$matchIndex]) {
171+
$matches['start'][$matchIndex] = (int)$matches['start'][$matchIndex] -1;
172+
}
170173
$sample = array_slice($includedFilesLines[$includedFilePath], (int)$matches['start'][$matchIndex], (int)$matches['end'][$matchIndex] - (int)$matches['start'][$matchIndex]);
171174
}
172-
$solvedLine = str_replace($matchString, implode(PHP_EOL . $matches['glue'][$matchIndex], $sample) . PHP_EOL, $solvedLine);
175+
if ('include_code' === $matches['function'][$matchIndex] && !empty($matches['glue'][$matchIndex])) {
176+
$matches['glue'][$matchIndex] = str_repeat(' ', $matches['glue'][$matchIndex]);
177+
}
178+
$solvedLine = str_replace($matchString, implode(PHP_EOL . $matches['glue'][$matchIndex], $sample) . ('include_code' === $matches['function'][$matchIndex] ? '' : PHP_EOL), $solvedLine);
173179
}
174180
$rawBlockCodeLines = array_merge($rawBlockCodeLines, explode(PHP_EOL, $solvedLine));
175181
} elseif (str_contains($blockSourceLine, '--8<--')) {

0 commit comments

Comments
 (0)