Skip to content

Commit e0f0074

Browse files
committed
Only add syntax highlighting for funcs/methods in the package
1 parent 284eb5e commit e0f0074

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

sphinx_github_style/meth_lexer.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@
55
from inspect import getmembers, isfunction, ismethod, ismodule, isclass
66

77

8+
def is_module_in_package(object):
9+
"""Predicate to determine if an object is a module within the project's top level package"""
10+
return ismodule(object) and object.__name__.startswith(TDKMethLexer.TOP_LEVEL)
11+
12+
13+
def is_class_in_package(object):
14+
"""Predicate to determine if an object is a class within the project's top level package"""
15+
return isclass(object) and object.__module__.startswith(TDKMethLexer.TOP_LEVEL)
16+
17+
818
def get_pkg_funcs(pkg_module: types.ModuleType, funcs_meths=set(), processed_modules=set()):
919
funcs_meths.update(get_funcs(pkg_module))
1020
processed_modules.add(pkg_module)
1121

12-
for class_name, _class in getmembers(pkg_module, isclass):
22+
for class_name, _class in getmembers(pkg_module, is_class_in_package):
1323
funcs_meths.update(get_funcs(_class))
1424

15-
for mod_name, mod in getmembers(pkg_module, ismodule):
25+
for mod_name, mod in getmembers(pkg_module, is_module_in_package):
1626
if mod in processed_modules:
1727
continue
1828

1929
try:
20-
is_pkg = Path(mod.__file__).name == '__init__.py'
21-
except AttributeError: # It's a built-in module
22-
is_pkg = False
30+
mod_path = Path(mod.__file__)
31+
except AttributeError:
32+
continue # It's a built-in module
2333

24-
if not is_pkg: # If it's not a subpackage, get all funcs/meths defined in it
34+
if mod_path.name == '__init__.py': # If it's a subpackage, call recursively to process all submodules
35+
get_pkg_funcs(mod, funcs_meths, processed_modules)
36+
37+
else: # If it's not a subpackage, get all funcs/meths defined in it
2538
funcs_meths.update(get_funcs(mod))
2639
processed_modules.add(mod)
2740

28-
else: # If it's a subpackage, call recursively to process all submodules
29-
get_pkg_funcs(mod, funcs_meths, processed_modules)
30-
3141
return funcs_meths
3242

3343

0 commit comments

Comments
 (0)