1
1
from pygments .lexers .python import NumPyLexer
2
- from inspect import getmembers , getmodule , isfunction , ismethod , ismodule , isclass
2
+ from inspect import getmembers , isfunction , ismethod , ismodule , isclass
3
3
from sphinx .application import Sphinx
4
+ from typing import Type
4
5
import types
5
- import sphinx_github_style
6
+
6
7
7
8
def get_pkg_funcs (pkg : types .ModuleType ):
8
- funcs_meths = get_funcs (pkg ) # Get funcs/meths defined in pkg.__init__
9
- modules = getmembers (pkg , ismodule ) # Modules of package
9
+ # Get funcs/meths defined in pkg.__init__
10
+ funcs_meths = get_funcs (pkg )
11
+ modules = getmembers (pkg , ismodule )
12
+ # Get funcs/meths of each module in pkg
10
13
for name , module in modules :
11
- funcs_meths += get_funcs (module ) # Get standalone funcs defined in module
12
- classes = getmembers (module , isclass ) # Get classes in module
14
+ funcs_meths += get_funcs (module )
15
+ classes = getmembers (module , isclass )
13
16
for class_name , _class in classes :
14
- if getmodule (_class ).__name__ .startswith (
15
- pkg .__name__ ): # If class is defined in the module, get its funcs/meths
16
- funcs_meths += get_funcs (_class )
17
+ funcs_meths += get_funcs (_class )
18
+ # Set of all funcs/meths contained in modules used by package
17
19
return set (funcs_meths )
18
20
19
21
@@ -22,20 +24,24 @@ def get_funcs(of):
22
24
return list (dict (members ))
23
25
24
26
25
- funcs = get_pkg_funcs (sphinx_github_style )
26
-
27
-
28
27
class TDKMethLexer (NumPyLexer ):
29
-
30
- """Adds syntax highlighting for a python Package's methods
28
+ """Adds syntax highlighting for methods and functions within a python Package
31
29
32
30
"""
33
31
name = 'TDK'
34
32
url = 'https://github.com/TDKorn'
35
33
aliases = ['tdk' ]
36
34
37
- EXTRA_KEYWORDS = NumPyLexer .EXTRA_KEYWORDS .union (funcs )
35
+ EXTRA_KEYWORDS = NumPyLexer .EXTRA_KEYWORDS
36
+
37
+ @classmethod
38
+ def get_pkg_lexer (cls , pkg_name : str ) -> Type ["TDKMethLexer" ]:
39
+ pkg = __import__ (pkg_name )
40
+ funcs = get_pkg_funcs (pkg )
41
+ cls .EXTRA_KEYWORDS .update (funcs )
42
+ return cls
38
43
39
44
40
45
def setup (app : Sphinx ):
41
- app .add_lexer ('python' , TDKMethLexer )
46
+ pkg_name = app .config ._raw_config ['pkg_name' ]
47
+ app .add_lexer ('python' , TDKMethLexer .get_pkg_lexer (pkg_name ))
0 commit comments