55namespace PhelWeb \ApiGenerator \Application ;
66
77use Phel \Api \Transfer \PhelFunction ;
8- use Phel \Lang \Symbol ;
98use Phel \Shared \Facade \ApiFacadeInterface ;
109
1110final 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 ' — 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}
0 commit comments