2
2
import json
3
3
import sys
4
4
from collections import OrderedDict
5
+ from tabulate import tabulate
5
6
6
7
verbose = False
7
8
8
9
MARKDOWN_FORMAT = "markdown"
10
+ RST_FORMAT = "rst"
11
+
9
12
DICT_FORMAT = "dict"
10
13
11
14
@@ -369,14 +372,17 @@ def generate_documentation(self, format=MARKDOWN_FORMAT):
369
372
Work in progress...
370
373
"""
371
374
372
- if format == MARKDOWN_FORMAT :
375
+ if format == MARKDOWN_FORMAT or format == RST_FORMAT :
373
376
doc_string = ""
374
377
if format == DICT_FORMAT :
375
378
doc_dict = {}
376
379
377
380
print (" - %s (%s)" % (self .__class__ .__name__ , self ._definition ))
378
381
379
- def insert_links (text ):
382
+ rst_url_format = '`%s <%s>`_'
383
+
384
+ def insert_links (text , format = MARKDOWN_FORMAT ):
385
+
380
386
if not "_" in text :
381
387
return text
382
388
if '"' in text :
@@ -386,24 +392,39 @@ def insert_links(text):
386
392
for i in range (int (len (split ) / 2.0 )):
387
393
pre = split [i * 2 ]
388
394
type = split [i * 2 + 1 ]
389
- text2 += '%s<a href="#%s">%s</a>' % (pre , type .lower (), type )
395
+ if format == MARKDOWN_FORMAT :
396
+ text2 += '%s<a href="#%s">%s</a>' % (pre , type .lower (), type )
397
+ elif format == RST_FORMAT :
398
+ #text2 += ('%s'+rst_url_format) % (pre, type, '#'+type.lower # problem with handling links ending with s e.g. _Graph_s
399
+
400
+ text2 += ('%s%s' ) % (pre , type ) # temp hack... problem with handling links ending with s e.g. _Graph_s
390
401
if int (len (split ) / 2.0 ) != len (split ) / 2.0 :
391
402
text2 += split [- 1 ]
392
403
return text2
393
404
394
405
name = self .__class__ .__name__
406
+
395
407
if format == MARKDOWN_FORMAT :
396
- doc_string += "## %s\n " % name
408
+ doc_string += "## %s\n \n " % name
397
409
if self ._definition is not None :
398
- doc_string += "%s\n " % insert_links (self ._definition )
399
- if format == DICT_FORMAT :
410
+ doc_string += "%s\n \n " % insert_links (self ._definition )
411
+ elif format == RST_FORMAT :
412
+ doc_string += "%s\n %s\n %s\n " % ("=" * len (name ),name ,"=" * len (name ))
413
+ if self ._definition is not None :
414
+ doc_string += "%s\n \n " % insert_links (self ._definition , format = RST_FORMAT )
415
+
416
+ elif format == DICT_FORMAT :
400
417
doc_dict [name ] = {}
401
418
if self ._definition is not None :
402
419
doc_dict [name ]["definition" ] = self ._definition
403
420
404
421
if len (self .allowed_fields ) > 0 :
405
422
if format == MARKDOWN_FORMAT :
406
- doc_string += "#### Allowed parameters\n <table>"
423
+ doc_string += "### Allowed parameters\n <table>\n "
424
+ if format == RST_FORMAT :
425
+ ap = "**Allowed parameters**"
426
+ doc_string += "%s\n \n " % (ap )
427
+ table_info = []
407
428
if format == DICT_FORMAT :
408
429
doc_dict [name ]["allowed_parameters" ] = {}
409
430
@@ -424,15 +445,26 @@ def insert_links(text):
424
445
] = self .allowed_fields [f ][0 ]
425
446
426
447
if format == MARKDOWN_FORMAT :
427
- doc_string += "<tr><td><b>%s</b></td><td>%s</td>" % (
448
+ doc_string += " <tr>\n <td><b>%s</b></td>\n <td>%s</td>" % (
428
449
f ,
429
450
'<a href="#%s">%s</a>' % (type_ .lower (), type_ )
430
451
if referencable
431
452
else type_ ,
432
453
)
433
- doc_string += "<td><i>%s</i></td></tr>\n \n " % (
454
+ doc_string += "\n <td><i>%s</i></td>\n </tr>\n \n " % (
434
455
insert_links (self .allowed_fields [f ][0 ])
435
456
)
457
+ if format == RST_FORMAT :
458
+ n = "**%s**" % f
459
+ t = "%s" % (
460
+ rst_url_format % (type_ , '#' + type_ .lower ())
461
+ if referencable
462
+ else type_ ,
463
+ )
464
+ d = "*%s*" % (
465
+ insert_links (self .allowed_fields [f ][0 ], format = RST_FORMAT )
466
+ )
467
+ table_info .append ([n ,t ,d ])
436
468
437
469
if referencable :
438
470
inst = self .allowed_fields [f ][1 ]()
@@ -441,11 +473,17 @@ def insert_links(text):
441
473
442
474
if len (self .allowed_fields ) > 0 :
443
475
if format == MARKDOWN_FORMAT :
444
- doc_string += "\n </table>\n \n "
476
+ doc_string += "</table>\n \n "
477
+ if format == RST_FORMAT :
478
+ doc_string += "%s\n \n " % (tabulate (table_info , ['Allowed field' ,'Data Type' ,'Description' ], tablefmt = "rst" ))
445
479
446
480
if len (self .allowed_children ) > 0 :
447
481
if format == MARKDOWN_FORMAT :
448
- doc_string += "#### Allowed children\n <table>"
482
+ doc_string += "#### Allowed children\n \n <table>\n "
483
+ if format == RST_FORMAT :
484
+ ap = "**Allowed children**"
485
+ doc_string += "%s\n \n " % (ap )
486
+ table_info = []
449
487
if format == DICT_FORMAT :
450
488
doc_dict [name ]["allowed_children" ] = {}
451
489
@@ -466,32 +504,49 @@ def insert_links(text):
466
504
] = self .allowed_children [c ][0 ]
467
505
468
506
if format == MARKDOWN_FORMAT :
469
- doc_string += "<tr><td><b>%s</b></td><td>%s</td>" % (
507
+ doc_string += " <tr>\n <td><b>%s</b></td>\n <td>%s</td>" % (
470
508
c ,
471
509
'<a href="#%s">%s</a>' % (type_ .lower (), type_ )
472
510
if referencable
473
511
else type_ ,
474
512
)
475
- doc_string += "<td><i>%s</i></td></tr>\n \n " % (
513
+ doc_string += "\n <td><i>%s</i></td>\n </tr>\n \n " % (
476
514
insert_links (self .allowed_children [c ][0 ])
477
515
)
478
516
517
+ if format == RST_FORMAT :
518
+ n = "**%s**" % c
519
+ t = "%s" % (
520
+ rst_url_format % (type_ , '#' + type_ .lower ())
521
+ if referencable
522
+ else type_ ,
523
+ )
524
+ d = "*%s*" % (
525
+ insert_links (self .allowed_children [c ][0 ], format = RST_FORMAT )
526
+ )
527
+ table_info .append ([n ,t ,d ])
528
+
529
+
479
530
inst = self .allowed_children [c ][1 ]()
480
531
inst .id = ""
481
532
referenced .append (inst )
482
533
483
534
if len (self .allowed_children ) > 0 :
484
535
if format == MARKDOWN_FORMAT :
485
- doc_string += "\n </table>\n \n "
536
+ doc_string += "</table>\n \n "
537
+
538
+ if len (self .allowed_children ) > 0 :
539
+ if format == RST_FORMAT :
540
+ doc_string += "%s\n \n " % (tabulate (table_info , ['Allowed child' ,'Data Type' ,'Description' ], tablefmt = "rst" ))
486
541
487
542
for r in referenced :
488
- if format == MARKDOWN_FORMAT :
543
+ if format == MARKDOWN_FORMAT or format == RST_FORMAT :
489
544
doc_string += r .generate_documentation (format = format )
490
545
if format == DICT_FORMAT :
491
546
pass
492
547
doc_dict .update (r .generate_documentation (format = format ))
493
548
494
- if format == MARKDOWN_FORMAT :
549
+ if format == MARKDOWN_FORMAT or format == RST_FORMAT :
495
550
return doc_string
496
551
if format == DICT_FORMAT :
497
552
return doc_dict
0 commit comments