Skip to content

Commit 9788759

Browse files
committed
Update TDKMethLexer
Added a class method for creating package-specific Lexer
1 parent f137896 commit 9788759

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

sphinx_github_style/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sphinx.application import Sphinx
1010

1111

12-
__version__ = "0.0.1b4"
12+
__version__ = "0.0.1b5"
1313
__author__ = 'Adam Korn <hello@dailykitten.net>'
1414

1515

@@ -30,13 +30,14 @@ def setup(app: Sphinx) -> Dict[str, Any]:
3030
app.connect('doctree-resolved', add_linkcode_node_class)
3131
# app.connect('build-finished', save_generated_rst_files)
3232

33+
app.add_config_value('pkg_name', pkg_name, 'html')
3334
app.add_config_value('linkcode_link_text', '[source]', 'html')
3435
app.add_config_value('linkcode_default_blob', 'master', 'html')
3536
app.config.pygments_style = 'sphinx_github_style.TDKStyle'
3637
app.config.html_context['github_version'] = get_linkcode_revision(app)
3738

3839
app.add_css_file('github_linkcode.css')
39-
app.add_lexer('python', TDKMethLexer)
40+
app.add_lexer('python', TDKMethLexer.get_pkg_lexer(pkg_name))
4041

4142
linkcode_url = get_linkcode_url(app)
4243

sphinx_github_style/meth_lexer.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
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
33
from sphinx.application import Sphinx
4+
from typing import Type
45
import types
5-
import sphinx_github_style
6+
67

78
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
1013
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)
1316
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
1719
return set(funcs_meths)
1820

1921

@@ -22,20 +24,24 @@ def get_funcs(of):
2224
return list(dict(members))
2325

2426

25-
funcs = get_pkg_funcs(sphinx_github_style)
26-
27-
2827
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
3129
3230
"""
3331
name = 'TDK'
3432
url = 'https://github.com/TDKorn'
3533
aliases = ['tdk']
3634

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
3843

3944

4045
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

Comments
 (0)