@@ -30,11 +30,49 @@ class TypeBuilder
3030
3131 public $ file = '' ;
3232
33+ public $ confluence = false ;
34+
3335 public function __construct ()
3436 {
3537 $ this ->processed = new \SplObjectStorage ();
3638 }
3739
40+ public function anchorLink ($ destinationHeader , $ anchor = null )
41+ {
42+ if ($ this ->confluence ) {
43+ $ l = str_replace ('` ' , '' , $ destinationHeader );
44+ $ l = str_replace (' ' , '- ' , $ l );
45+ if (!is_string ($ l )) {
46+ return '# ' ;
47+ }
48+
49+ $ l = urlencode ($ l );
50+
51+ return '# ' . $ l ;
52+ }
53+
54+ if (!empty ($ anchor )) {
55+ $ l = strtolower ($ anchor );
56+ } else {
57+ $ l = strtolower ($ destinationHeader );
58+ }
59+
60+ return '# ' . $ l ;
61+ }
62+
63+ public function header ($ text , $ anchor = null )
64+ {
65+ if ($ this ->confluence ) {
66+ return $ text ;
67+ }
68+
69+ if (!empty ($ anchor )) {
70+ $ l = strtolower ($ anchor );
71+ } else {
72+ $ l = strtolower ($ text );
73+ }
74+ return '<a id=" ' . $ l . '"> ' . '</a> ' . $ text ;
75+ }
3876
3977 /**
4078 * @param Schema|boolean|null $schema
@@ -62,10 +100,17 @@ public function getTypeString($schema, $path = '')
62100
63101 if (!empty ($ schema ->enum )) {
64102 $ res = '' ;
65- foreach ($ schema ->enum as $ value ) {
66- $ res .= '<br>` ' . var_export ($ value , true ) . '`, ' ;
103+ if ($ this ->confluence ) {
104+ foreach ($ schema ->enum as $ value ) {
105+ $ res .= '` ' . var_export ($ value , true ) . '`, ' ;
106+ }
107+ return substr ($ res , 0 , -2 );
108+ } else {
109+ foreach ($ schema ->enum as $ value ) {
110+ $ res .= '<br>` ' . var_export ($ value , true ) . '`, ' ;
111+ }
112+ return substr ($ res , 4 , -2 );
67113 }
68- return substr ($ res , 4 , -2 );
69114 }
70115
71116 if (!empty ($ schema ->getFromRefs ())) {
@@ -246,6 +291,10 @@ public function getTypeString($schema, $path = '')
246291 $ res = '`*` ' ;
247292 }
248293
294+ if (empty ($ res )) {
295+ $ res = '' ;
296+ }
297+
249298 $ res = str_replace ('`` ' , '' , $ res );
250299
251300 return $ res ;
@@ -273,7 +322,11 @@ private function typeName(Schema $schema, $path, $raw = false)
273322 return $ name ;
274323 }
275324
276- return '[` ' . $ name . '`](# ' . strtolower ($ name ) . ') ' ;
325+ if ($ this ->confluence ) {
326+ return '[ ' . $ name . ']( ' . $ this ->anchorLink ($ name ) . ') ' ;
327+ }
328+
329+ return '[` ' . $ name . '`]( ' . $ this ->anchorLink ($ name ) . ') ' ;
277330 }
278331
279332 private static function constraints ()
@@ -350,12 +403,10 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
350403 }
351404 }
352405
353- $ tnl = strtolower ($ typeName );
354-
355406 $ res = <<<MD
356407
357408
358- ### <a id=" $ tnl "></a> $ typeName
409+ ### { $ this -> header ( $ typeName)}
359410$ head
360411
361412MD ;
@@ -376,14 +427,19 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
376427 ];
377428 }
378429 }
379- $ res . = TableRenderer::create (new \ArrayIterator ($ rows ))
430+ $ tr = TableRenderer::create (new \ArrayIterator ($ rows ))
380431 ->stripEmptyColumns ()
381432 ->setColDelimiter ('| ' )
382433 ->setHeadRowDelimiter ('- ' )
383434 ->setOutlineVertical (true )
384- ->multilineCellDelimiter ('<br> ' )
385435 ->setShowHeader ();
386436
437+ if (!$ this ->confluence ) {
438+ $ tr ->multilineCellDelimiter ('<br> ' );
439+ }
440+
441+ $ res .= $ tr ;
442+
387443 $ res .= "\n\n" ;
388444
389445 $ rows = [];
@@ -412,14 +468,18 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
412468 }
413469 }
414470
415- $ res . = TableRenderer::create (new \ArrayIterator ($ rows ))
471+ $ tr = TableRenderer::create (new \ArrayIterator ($ rows ))
416472 ->stripEmptyColumns ()
417473 ->setColDelimiter ('| ' )
418474 ->setHeadRowDelimiter ('- ' )
419475 ->setOutlineVertical (true )
420- ->multilineCellDelimiter ('<br> ' )
421476 ->setShowHeader ();
422477
478+ if (!$ this ->confluence ) {
479+ $ tr ->multilineCellDelimiter ('<br> ' );
480+ }
481+
482+ $ res .= $ tr ;
423483 }
424484
425485 $ res .= <<<MD
@@ -470,15 +530,28 @@ public function tableOfContents()
470530 return $ res ;
471531 }
472532
533+ private function trim ($ s )
534+ {
535+ if (empty ($ s )) {
536+ return '' ;
537+ }
538+
539+ return trim ($ s );
540+ }
541+
473542 private function description (Schema $ schema )
474543 {
475- $ res = str_replace ("\n" , " " , trim ($ schema ->title ));
476- if (trim ($ schema ->description )) {
544+ $ res = str_replace ("\n" , " " , $ this ->trim ($ schema ->title ));
545+ if (!is_string ($ res )) {
546+ return '' ;
547+ }
548+
549+ if ($ this ->trim ($ schema ->description )) {
477550 if ($ res ) {
478551 $ res .= ". " ;
479552 }
480553
481- $ res .= str_replace ("\n" , " " , trim ($ schema ->description ));
554+ $ res .= str_replace ("\n" , " " , $ this -> trim ($ schema ->description ));
482555 }
483556 if ($ res ) {
484557 return rtrim ($ res , '. ' ) . '. ' ;
0 commit comments