1
1
const fs = require ( 'fs' ) ;
2
2
const path = require ( 'path' ) ;
3
+ const { getAllEntries } = require ( "./convert" ) ;
3
4
4
5
// Read docs.json
5
6
const data = JSON . parse ( fs . readFileSync ( path . join ( __dirname , '../docs/data.json' ) ) ) ;
6
7
7
- // Flatten and organize data structure
8
- function getEntries ( entry ) {
9
- if ( ! entry ) return [ ] ;
10
- if ( ! entry . members ) return [ entry ] ;
11
-
12
- return [
13
- entry ,
14
- ...getAllEntries ( entry . members . global || [ ] ) ,
15
- ...getAllEntries ( entry . members . inner || [ ] ) ,
16
- ...getAllEntries ( entry . members . instance || [ ] ) ,
17
- ...getAllEntries ( entry . members . events || [ ] ) ,
18
- ...getAllEntries ( entry . members . static || [ ] )
19
- ] ;
20
- }
21
-
22
- function getAllEntries ( arr ) {
23
- return arr . flatMap ( getEntries ) ;
24
- }
25
8
const organized = {
26
9
modules : { } ,
27
10
classes : { } ,
28
11
classitems : [ ] ,
29
12
consts : { }
30
13
} ;
31
14
15
+ // Add this helper function at the top with other helpers
16
+ function normalizeClassName ( className ) {
17
+ if ( ! className || className === 'p5' ) return 'p5' ;
18
+ return className . startsWith ( 'p5.' ) ? className : `p5.${ className } ` ;
19
+ }
20
+
32
21
// Organize data into structured format
33
22
function organizeData ( data ) {
34
23
const allData = getAllEntries ( data ) ;
@@ -54,6 +43,8 @@ function organizeData(data) {
54
43
if ( entry . kind === 'class' ) {
55
44
const { module, submodule } = getModuleInfo ( entry ) ;
56
45
const className = entry . name ;
46
+ const extendsTag = entry . tags ?. find ( tag => tag . title === 'extends' ) ;
47
+
57
48
organized . classes [ className ] = {
58
49
name : className ,
59
50
description : extractDescription ( entry . description ) ,
@@ -63,7 +54,8 @@ function organizeData(data) {
63
54
optional : param . type ?. type === 'OptionalType'
64
55
} ) ) ,
65
56
module,
66
- submodule
57
+ submodule,
58
+ extends : extendsTag ?. name || null
67
59
} ;
68
60
}
69
61
} ) ;
@@ -72,11 +64,20 @@ function organizeData(data) {
72
64
allData . forEach ( entry => {
73
65
if ( entry . kind === 'function' || entry . kind === 'property' ) {
74
66
const { module, submodule, forEntry } = getModuleInfo ( entry ) ;
67
+
68
+ // Normalize memberof and forEntry
69
+ let memberof = entry . memberof ;
70
+ if ( memberof && memberof !== 'p5' && ! memberof . startsWith ( 'p5.' ) ) {
71
+ memberof = 'p5.' + memberof ;
72
+ }
73
+
74
+ let normalizedForEntry = forEntry ;
75
+ if ( forEntry && forEntry !== 'p5' && ! forEntry . startsWith ( 'p5.' ) ) {
76
+ normalizedForEntry = 'p5.' + forEntry ;
77
+ }
78
+
75
79
// Use memberof if available, fallback to forEntry, then default to 'p5'
76
- const className = entry . memberof || forEntry || 'p5' ;
77
-
78
- // Create the class entry if it doesn't exist
79
- // if (!organized.classes[className]) {console.log(`returning for ${className}`); return};
80
+ const className = normalizeClassName ( memberof || normalizedForEntry || 'p5' ) ;
80
81
81
82
const isStatic = entry . path ?. [ 0 ] ?. scope === 'static' ;
82
83
// Handle overloads
@@ -356,18 +357,28 @@ function generateClassDeclaration(classDoc, organizedData) {
356
357
}
357
358
if ( classDoc . tags ) {
358
359
if ( description ) {
359
- output += ' *\n' ; // Add separator between description and tags
360
+ output += ' *\n' ;
360
361
}
361
362
classDoc . tags . forEach ( tag => {
362
363
if ( tag . description ) {
363
364
const tagDesc = extractDescription ( tag . description ) ;
365
+
364
366
output += formatJSDocComment ( `@${ tag . title } ${ tagDesc } ` , 0 ) + '\n' ;
365
367
}
366
368
} ) ;
367
369
}
368
370
output += ' */\n' ;
369
371
}
370
372
373
+ // Get the parent class if it exists
374
+ const parentClass = classDoc . extends ;
375
+ const extendsClause = parentClass ? ` extends ${ parentClass } ` : '' ;
376
+
377
+ // Start class declaration with inheritance if applicable
378
+ const fullClassName = normalizeClassName ( classDoc . name ) ;
379
+ const classDocName = fullClassName . replace ( 'p5.' , '' ) ;
380
+ output += `class ${ classDocName } ${ extendsClause } {\n` ;
381
+
371
382
// Add constructor if there are parameters
372
383
if ( classDoc . params ?. length > 0 ) {
373
384
output += ' constructor(' ;
@@ -378,9 +389,10 @@ function generateClassDeclaration(classDoc, organizedData) {
378
389
}
379
390
380
391
// Get all class items for this class
381
- const classDocName = classDoc . name . startsWith ( 'p5.' ) ? classDoc . name . substring ( 3 ) : classDoc . name ;
382
-
383
- const classItems = organizedData . classitems . filter ( item => item . class === classDocName ) ;
392
+ const classItems = organizedData . classitems . filter ( item =>
393
+ item . class === fullClassName ||
394
+ item . class === fullClassName . replace ( 'p5.' , '' )
395
+ ) ;
384
396
385
397
// Separate static and instance members
386
398
const staticItems = classItems . filter ( item => item . isStatic ) ;
@@ -445,10 +457,16 @@ function generateDeclarationFile(items, filePath, organizedData) {
445
457
// Find the class documentation if it exists
446
458
const classDoc = items . find ( item => item . kind === 'class' ) ;
447
459
if ( classDoc ) {
448
- const classDocName = classDoc . name . startsWith ( 'p5.' ) ? classDoc . name . substring ( 3 ) : classDoc . name ;
460
+ const fullClassName = normalizeClassName ( classDoc . name ) ;
461
+ const classDocName = fullClassName . replace ( 'p5.' , '' ) ;
462
+ let parentClass = classDoc . tags ?. find ( tag => tag . title === 'extends' ) ?. name ;
463
+ if ( parentClass ) {
464
+ parentClass = parentClass . replace ( 'p5.' , '' ) ;
465
+ }
466
+ const extendsClause = parentClass ? ` extends ${ parentClass } ` : '' ;
449
467
450
468
// Start class declaration
451
- output += ` class ${ classDocName } {\n` ;
469
+ output += ` class ${ classDocName } ${ extendsClause } {\n` ;
452
470
453
471
// Add constructor if there are parameters
454
472
if ( classDoc . params ?. length > 0 ) {
@@ -458,27 +476,27 @@ function generateDeclarationFile(items, filePath, organizedData) {
458
476
. join ( ', ' ) ;
459
477
output += ');\n\n' ;
460
478
}
461
-
462
- // Get all members that belong to this class
479
+
480
+ // Get all class items for this class
463
481
const classItems = organizedData . classitems . filter ( item =>
464
- item . memberof === classDoc . name || item . class === classDocName
482
+ item . class === fullClassName ||
483
+ item . class === fullClassName . replace ( 'p5.' , '' )
465
484
) ;
466
485
467
486
// Separate static and instance members
468
487
const staticItems = classItems . filter ( item => item . isStatic ) ;
469
488
const instanceItems = classItems . filter ( item => ! item . isStatic ) ;
470
-
489
+
471
490
// Generate static members
472
491
staticItems . forEach ( item => {
473
492
output += generateMethodDeclarations ( item , true ) ;
474
493
} ) ;
475
-
494
+
476
495
// Generate instance members
477
496
instanceItems . forEach ( item => {
478
497
output += generateMethodDeclarations ( item , false ) ;
479
498
} ) ;
480
-
481
- // Close class declaration
499
+
482
500
output += ' }\n\n' ;
483
501
}
484
502
0 commit comments