Skip to content

Commit bbc90e8

Browse files
Improve API documentation page
1 parent d01a27b commit bbc90e8

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

build/src/ApiGenerator/Application/ApiMarkdownGenerator.php

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace PhelWeb\ApiGenerator\Application;
66

77
use Phel\Api\Transfer\PhelFunction;
8-
use Phel\Lang\Symbol;
98
use Phel\Shared\Facade\ApiFacadeInterface;
109

1110
final readonly class ApiMarkdownGenerator
1211
{
1312
public function __construct(
14-
private ApiFacadeInterface $apiFacade
13+
private ApiFacadeInterface $apiFacade,
1514
) {
1615
}
1716

@@ -24,11 +23,12 @@ public function generate(): array
2423
$phelFns = $this->apiFacade->getPhelFunctions();
2524
$groupedByNamespace = $this->groupFunctionsByNamespace($phelFns);
2625

26+
$namespaces = [];
2727
foreach ($groupedByNamespace as $namespace => $functions) {
28-
$result = array_merge($result, $this->buildNamespaceSection($namespace, $functions));
28+
$namespaces[] = $this->buildNamespaceSection($namespace, $functions);
2929
}
3030

31-
return $result;
31+
return array_merge($result, ...$namespaces);
3232
}
3333

3434
/**
@@ -50,18 +50,12 @@ private function groupFunctionsByNamespace(array $phelFns): array
5050
*/
5151
private function buildNamespaceSection(string $namespace, array $functions): array
5252
{
53-
$lines = [
54-
'',
55-
'---',
56-
'',
57-
"## `{$namespace}`",
58-
];
59-
53+
$elements = [];
6054
foreach ($functions as $fn) {
61-
$lines = array_merge($lines, $this->buildFunctionSection($fn));
55+
$elements[] = $this->buildFunctionSection($fn);
6256
}
6357

64-
return $lines;
58+
return array_merge(['', '---', '', "## `{$namespace}`", ''], ...$elements);
6559
}
6660

6761
/**
@@ -75,7 +69,16 @@ private function buildFunctionSection(PhelFunction $fn): array
7569
$lines[] = $deprecation;
7670
}
7771

78-
$lines[] = $fn->doc;
72+
// Handle exceptional documentation blocks
73+
if ($fn->name === 'with-mock-wrapper' || $fn->name === 'with-mocks') {
74+
$input = preg_replace('/```phel/', '```clojure', $fn->doc, 1);
75+
$input = preg_replace('/^[ \t]+/m', '', $input);
76+
$input = preg_replace('/(?<!\n)\n(```phel)/', "\n\n$1", $input);
77+
} else {
78+
$input = $fn->doc;
79+
}
80+
81+
$lines[] = $input;
7982

8083
if ($example = $this->buildExampleSection($fn)) {
8184
$lines = array_merge($lines, $example);
@@ -88,6 +91,7 @@ private function buildFunctionSection(PhelFunction $fn): array
8891
if ($sourceLink = $this->buildSourceLink($fn)) {
8992
$lines[] = $sourceLink;
9093
}
94+
$lines[] = '';
9195

9296
return $lines;
9397
}
@@ -100,7 +104,7 @@ private function buildDeprecationNotice(PhelFunction $fn): ?string
100104

101105
$message = sprintf(
102106
'<small><span style="color: red; font-weight: bold;">Deprecated</span>: %s',
103-
$fn->meta['deprecated']
107+
$fn->meta['deprecated'],
104108
);
105109

106110
if (isset($fn->meta['superseded-by'])) {
@@ -109,7 +113,7 @@ private function buildDeprecationNotice(PhelFunction $fn): ?string
109113
$message .= sprintf(
110114
' &mdash; Use [`%s`](#%s) instead',
111115
$supersededBy,
112-
$anchor
116+
$anchor,
113117
);
114118
}
115119

@@ -129,7 +133,7 @@ private function buildExampleSection(PhelFunction $fn): ?array
129133
'',
130134
'**Example:**',
131135
'',
132-
'```phel',
136+
'```clojure',
133137
$fn->meta['example'],
134138
'```',
135139
];
@@ -158,10 +162,7 @@ private function buildSeeAlsoSection(PhelFunction $fn): ?array
158162
*/
159163
private function extractFunctionNames(mixed $seeAlso): array
160164
{
161-
return array_map(
162-
fn(Symbol $symbol) => $symbol->getName(),
163-
iterator_to_array($seeAlso)
164-
);
165+
return iterator_to_array($seeAlso);
165166
}
166167

167168
/**
@@ -171,12 +172,8 @@ private function extractFunctionNames(mixed $seeAlso): array
171172
private function buildFunctionLinks(array $functionNames): array
172173
{
173174
return array_map(
174-
fn(string $func) => sprintf(
175-
'[`%s`](#%s)',
176-
$func,
177-
$this->sanitizeAnchor($func)
178-
),
179-
$functionNames
175+
fn(string $func) => sprintf('[`%s`](#%s)', $func, $this->sanitizeAnchor($func)),
176+
$functionNames,
180177
);
181178
}
182179

@@ -219,7 +216,6 @@ private function buildZolaHeaders(): array
219216
'template = "page-api.html"',
220217
'aliases = [ "/api" ]',
221218
'+++',
222-
'',
223219
];
224220
}
225221
}

build/tests/php/ApiGenerator/Domain/ApiMarkdownGeneratorTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public function test_generate_page_without_phel_functions(): void
2424
'template = "page-api.html"',
2525
'aliases = [ "/api" ]',
2626
'+++',
27-
'',
2827
];
2928

3029
self::assertEquals($expected, $generator->generate());
@@ -53,12 +52,13 @@ public function test_generate_page_with_one_phel_function(): void
5352
'aliases = [ "/api" ]',
5453
'+++',
5554
'',
56-
'',
5755
'---',
5856
'',
5957
'## `ns-1`',
58+
'',
6059
'### `ns-1/function-1`',
6160
'The doc from function 1',
61+
'',
6262
];
6363

6464
self::assertEquals($expected, $generator->generate());
@@ -91,14 +91,16 @@ public function test_generate_page_with_multiple_phel_functions_in_same_group():
9191
'aliases = [ "/api" ]',
9292
'+++',
9393
'',
94-
'',
9594
'---',
9695
'',
9796
'## `core`',
97+
'',
9898
'### `function-1`',
9999
'The doc from function 1',
100+
'',
100101
'### `function-2`',
101102
'The doc from function 2',
103+
'',
102104
];
103105

104106
self::assertEquals($expected, $generator->generate());
@@ -131,18 +133,21 @@ public function test_generate_page_with_multiple_phel_functions_in_different_gro
131133
'aliases = [ "/api" ]',
132134
'+++',
133135
'',
134-
'',
135136
'---',
136137
'',
137138
'## `ns-1`',
139+
'',
138140
'### `ns-1/function-1`',
139141
'The doc from function 1',
140142
'',
143+
'',
141144
'---',
142145
'',
143146
'## `ns-2`',
147+
'',
144148
'### `ns-2/function-2`',
145149
'The doc from function 2',
150+
'',
146151
];
147152

148153
self::assertEquals($expected, $generator->generate());

composer.lock

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)