32
32
<a href="{path}"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square" /></a>
33
33
"""
34
34
35
+ _MDX_SOURCE_BADGE_TEMPLATE = """
36
+ <a href="{path}"><img align="right" style={{{{"float":"right"}}}} src="https://img.shields.io/badge/-source-cccccc?style=flat-square" /></a>
37
+ """
38
+
35
39
_SEPARATOR = """
36
40
---
37
41
"""
@@ -200,6 +204,7 @@ def to_md_file(
200
204
out_path : str = "." ,
201
205
watermark : bool = True ,
202
206
disable_markdownlint : bool = True ,
207
+ is_mdx : bool = False
203
208
) -> None :
204
209
"""Creates an API docs file from a provided text.
205
210
@@ -215,8 +220,13 @@ def to_md_file(
215
220
return
216
221
217
222
md_file = filename
218
- if not filename .endswith (".md" ):
219
- md_file = filename + ".md"
223
+
224
+ if is_mdx :
225
+ if not filename .endswith (".mdx" ):
226
+ md_file = filename + ".mdx"
227
+ else :
228
+ if not filename .endswith (".md" ):
229
+ md_file = filename + ".md"
220
230
221
231
if disable_markdownlint :
222
232
markdown_str = "<!-- markdownlint-disable -->\n " + markdown_str
@@ -521,7 +531,7 @@ def _get_src_path(self, obj: Any, append_base: bool = True) -> str:
521
531
522
532
return relative_path
523
533
524
- def func2md (self , func : Callable , clsname : str = "" , depth : int = 3 ) -> str :
534
+ def func2md (self , func : Callable , clsname : str = "" , depth : int = 3 , is_mdx : bool = False ) -> str :
525
535
"""Takes a function (or method) and generates markdown docs.
526
536
527
537
Args:
@@ -602,11 +612,14 @@ def func2md(self, func: Callable, clsname: str = "", depth: int = 3) -> str:
602
612
)
603
613
604
614
if path :
605
- markdown = _SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
615
+ if is_mdx :
616
+ markdown = _MDX_SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
617
+ else :
618
+ markdown = _SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
606
619
607
620
return markdown
608
621
609
- def class2md (self , cls : Any , depth : int = 2 ) -> str :
622
+ def class2md (self , cls : Any , depth : int = 2 , is_mdx : bool = False ) -> str :
610
623
"""Takes a class and creates markdown text to document its methods and variables.
611
624
612
625
Args:
@@ -646,7 +659,7 @@ def class2md(self, cls: Any, depth: int = 2) -> str:
646
659
hasattr (cls .__init__ , "__module__" )
647
660
and cls .__init__ .__module__ == modname
648
661
):
649
- init = self .func2md (cls .__init__ , clsname = clsname )
662
+ init = self .func2md (cls .__init__ , clsname = clsname , is_mdx = is_mdx )
650
663
else :
651
664
init = ""
652
665
except (ValueError , TypeError ):
@@ -698,7 +711,7 @@ def class2md(self, cls: Any, depth: int = 2) -> str:
698
711
# object module should be the same as the calling module
699
712
and obj .__module__ == modname
700
713
):
701
- function_md = self .func2md (obj , clsname = clsname , depth = depth + 1 )
714
+ function_md = self .func2md (obj , clsname = clsname , depth = depth + 1 , is_mdx = is_mdx )
702
715
if function_md :
703
716
methods .append (_SEPARATOR + function_md )
704
717
@@ -713,11 +726,14 @@ def class2md(self, cls: Any, depth: int = 2) -> str:
713
726
)
714
727
715
728
if path :
716
- markdown = _SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
729
+ if is_mdx :
730
+ markdown = _MDX_SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
731
+ else :
732
+ markdown = _SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
717
733
718
734
return markdown
719
735
720
- def module2md (self , module : types .ModuleType , depth : int = 1 ) -> str :
736
+ def module2md (self , module : types .ModuleType , depth : int = 1 , is_mdx : bool = False ) -> str :
721
737
"""Takes an imported module object and create a Markdown string containing functions and classes.
722
738
723
739
Args:
@@ -758,7 +774,7 @@ def module2md(self, module: types.ModuleType, depth: int = 1) -> str:
758
774
and hasattr (obj , "__module__" )
759
775
and obj .__module__ == modname
760
776
):
761
- class_markdown = self .class2md (obj , depth = depth + 1 )
777
+ class_markdown = self .class2md (obj , depth = depth + 1 , is_mdx = is_mdx )
762
778
if class_markdown :
763
779
classes .append (_SEPARATOR + class_markdown )
764
780
line_nos .append (_get_line_no (obj ) or 0 )
@@ -774,7 +790,7 @@ def module2md(self, module: types.ModuleType, depth: int = 1) -> str:
774
790
and hasattr (obj , "__module__" )
775
791
and obj .__module__ == modname
776
792
):
777
- function_md = self .func2md (obj , depth = depth + 1 )
793
+ function_md = self .func2md (obj , depth = depth + 1 , is_mdx = is_mdx )
778
794
if function_md :
779
795
functions .append (_SEPARATOR + function_md )
780
796
line_nos .append (_get_line_no (obj ) or 0 )
@@ -808,11 +824,14 @@ def module2md(self, module: types.ModuleType, depth: int = 1) -> str:
808
824
)
809
825
810
826
if path :
811
- markdown = _SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
827
+ if (is_mdx ):
828
+ markdown = _MDX_SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
829
+ else :
830
+ markdown = _SOURCE_BADGE_TEMPLATE .format (path = path ) + markdown
812
831
813
832
return markdown
814
833
815
- def import2md (self , obj : Any , depth : int = 1 ) -> str :
834
+ def import2md (self , obj : Any , depth : int = 1 , is_mdx : bool = False ) -> str :
816
835
"""Generates markdown documentation for a selected object/import.
817
836
818
837
Args:
@@ -823,16 +842,16 @@ def import2md(self, obj: Any, depth: int = 1) -> str:
823
842
str: Markdown documentation of selected object.
824
843
"""
825
844
if inspect .isclass (obj ):
826
- return self .class2md (obj , depth = depth )
845
+ return self .class2md (obj , depth = depth , is_mdx = is_mdx )
827
846
elif isinstance (obj , types .ModuleType ):
828
- return self .module2md (obj , depth = depth )
847
+ return self .module2md (obj , depth = depth , is_mdx = is_mdx )
829
848
elif callable (obj ):
830
- return self .func2md (obj , depth = depth )
849
+ return self .func2md (obj , depth = depth , is_mdx = is_mdx )
831
850
else :
832
851
print (f"Could not generate markdown for object type { str (type (obj ))} " )
833
852
return ""
834
853
835
- def overview2md (self ) -> str :
854
+ def overview2md (self , is_mdx : bool = False ) -> str :
836
855
"""Generates a documentation overview file based on the generated docs."""
837
856
838
857
entries_md = ""
@@ -841,7 +860,10 @@ def overview2md(self) -> str:
841
860
):
842
861
full_name = obj ["full_name" ]
843
862
if "module" in obj :
844
- link = "./" + obj ["module" ] + ".md#" + obj ["anchor_tag" ]
863
+ if is_mdx :
864
+ link = "./" + obj ["module" ] + ".mdx#" + obj ["anchor_tag" ]
865
+ else :
866
+ link = "./" + obj ["module" ] + ".md#" + obj ["anchor_tag" ]
845
867
else :
846
868
link = "#unknown"
847
869
@@ -857,7 +879,10 @@ def overview2md(self) -> str:
857
879
for obj in list (filter (lambda d : d ["type" ] == "class" , self .generated_objects )):
858
880
module_name = obj ["module" ].split ("." )[- 1 ]
859
881
name = module_name + "." + obj ["full_name" ]
860
- link = "./" + obj ["module" ] + ".md#" + obj ["anchor_tag" ]
882
+ if is_mdx :
883
+ link = "./" + obj ["module" ] + ".mdx#" + obj ["anchor_tag" ]
884
+ else :
885
+ link = "./" + obj ["module" ] + ".md#" + obj ["anchor_tag" ]
861
886
description = obj ["description" ]
862
887
entries_md += f"\n - [`{ name } `]({ link } )"
863
888
if description :
@@ -872,7 +897,10 @@ def overview2md(self) -> str:
872
897
):
873
898
module_name = obj ["module" ].split ("." )[- 1 ]
874
899
name = module_name + "." + obj ["full_name" ]
875
- link = "./" + obj ["module" ] + ".md#" + obj ["anchor_tag" ]
900
+ if is_mdx :
901
+ link = "./" + obj ["module" ] + ".mdx#" + obj ["anchor_tag" ]
902
+ else :
903
+ link = "./" + obj ["module" ] + ".md#" + obj ["anchor_tag" ]
876
904
description = obj ["description" ]
877
905
entries_md += f"\n - [`{ name } `]({ link } )"
878
906
if description :
@@ -893,6 +921,7 @@ def generate_docs(
893
921
src_base_url : Optional [str ] = None ,
894
922
remove_package_prefix : bool = False ,
895
923
ignored_modules : Optional [List [str ]] = None ,
924
+ output_format : Optional [str ] = None ,
896
925
overview_file : Optional [str ] = None ,
897
926
watermark : bool = True ,
898
927
validate : bool = False ,
@@ -919,6 +948,10 @@ def generate_docs(
919
948
if not ignored_modules :
920
949
ignored_modules = list ()
921
950
951
+ if output_format and output_format != 'md' and output_format != 'mdx' :
952
+ raise Exception (f"Unsupported output format: { output_format } . Choose either 'md' or 'mdx'." )
953
+ is_mdx = output_format == 'mdx'
954
+
922
955
if not src_root_path :
923
956
try :
924
957
# Set src root path to git root
@@ -967,7 +1000,7 @@ def generate_docs(
967
1000
try :
968
1001
mod_spec = loader .find_spec (module_name )
969
1002
mod = importlib .util .module_from_spec (mod_spec )
970
- module_md = generator .module2md (mod )
1003
+ module_md = generator .module2md (mod , is_mdx = is_mdx )
971
1004
if not module_md :
972
1005
# Module md is empty -> ignore module and all submodules
973
1006
# Add module to ignore list, so submodule will also be ignored
@@ -982,6 +1015,7 @@ def generate_docs(
982
1015
mod .__name__ ,
983
1016
out_path = output_path ,
984
1017
watermark = watermark ,
1018
+ is_mdx = is_mdx ,
985
1019
)
986
1020
except Exception as ex :
987
1021
print (
@@ -1005,7 +1039,7 @@ def generate_docs(
1005
1039
spec .loader .exec_module (mod ) # type: ignore
1006
1040
1007
1041
if mod :
1008
- module_md = generator .module2md (mod )
1042
+ module_md = generator .module2md (mod , is_mdx = is_mdx )
1009
1043
if stdout_mode :
1010
1044
print (module_md )
1011
1045
else :
@@ -1014,6 +1048,7 @@ def generate_docs(
1014
1048
module_name ,
1015
1049
out_path = output_path ,
1016
1050
watermark = watermark ,
1051
+ is_mdx = is_mdx ,
1017
1052
)
1018
1053
else :
1019
1054
raise Exception (f"Failed to generate markdown for { path } " )
@@ -1044,7 +1079,7 @@ def generate_docs(
1044
1079
try :
1045
1080
mod_spec = loader .find_spec (module_name )
1046
1081
mod = importlib .util .module_from_spec (mod_spec )
1047
- module_md = generator .module2md (mod )
1082
+ module_md = generator .module2md (mod , is_mdx = is_mdx )
1048
1083
1049
1084
if not module_md :
1050
1085
# Module MD is empty -> ignore module and all submodules
@@ -1060,32 +1095,38 @@ def generate_docs(
1060
1095
mod .__name__ ,
1061
1096
out_path = output_path ,
1062
1097
watermark = watermark ,
1098
+ is_mdx = is_mdx
1063
1099
)
1064
1100
except Exception as ex :
1065
1101
print (
1066
1102
f"Failed to generate docs for module { module_name } : "
1067
1103
+ repr (ex )
1068
1104
)
1069
1105
else :
1070
- import_md = generator .import2md (obj )
1106
+ import_md = generator .import2md (obj , is_mdx = is_mdx )
1071
1107
if stdout_mode :
1072
1108
print (import_md )
1073
1109
else :
1074
1110
to_md_file (
1075
- import_md , path , out_path = output_path , watermark = watermark
1111
+ import_md , path , out_path = output_path , watermark = watermark , is_mdx = is_mdx
1076
1112
)
1077
1113
else :
1078
1114
raise Exception (f"Failed to generate markdown for { path } ." )
1079
1115
1080
1116
if overview_file and not stdout_mode :
1081
- if not overview_file .endswith (".md" ):
1082
- overview_file = overview_file + ".md"
1117
+ if is_mdx :
1118
+ if not overview_file .endswith (".mdx" ):
1119
+ overview_file = overview_file + ".mdx"
1120
+ else :
1121
+ if not overview_file .endswith (".md" ):
1122
+ overview_file = overview_file + ".md"
1083
1123
1084
1124
to_md_file (
1085
- generator .overview2md (),
1125
+ generator .overview2md (is_mdx = is_mdx ),
1086
1126
overview_file ,
1087
1127
out_path = output_path ,
1088
1128
watermark = watermark ,
1129
+ is_mdx = is_mdx
1089
1130
)
1090
1131
1091
1132
# Write mkdocs pages file
0 commit comments