Skip to content

Commit 2e80b2f

Browse files
committed
Remove get_top_level() and adjust TDKMethLexer to work without it
The `top_level` confval is now only used to add syntax highlighting. If it's not provided, it will be parsed on the first call to `linkcode_resolve()`
1 parent 03ae2ef commit 2e80b2f

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

sphinx_github_style/__init__.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import sphinx
44
import inspect
55
import subprocess
6-
import pkg_resources
76
from pathlib import Path
87
from sphinx.application import Sphinx
98
from sphinx.errors import ExtensionError
@@ -20,7 +19,7 @@
2019
def setup(app: Sphinx) -> Dict[str, Any]:
2120
app.connect("builder-inited", add_static_path)
2221
app.add_config_value('linkcode_blob', 'head', True)
23-
app.add_config_value('top_level', '', True)
22+
app.add_config_value('top_level', None, True)
2423

2524
linkcode_blob = get_conf_val(app, "linkcode_blob")
2625
linkcode_url = get_linkcode_url(
@@ -31,9 +30,8 @@ def setup(app: Sphinx) -> Dict[str, Any]:
3130
linkcode_func = get_conf_val(app, "linkcode_resolve")
3231
repo_dir = get_repo_dir()
3332

34-
if not (top_level := get_conf_val(app, 'top_level')):
35-
top_level = get_top_level(repo_dir)
36-
set_conf_val(app, 'top_level', top_level)
33+
top_level = get_conf_val(app, 'top_level')
34+
TDKMethLexer.TOP_LEVEL = top_level
3735

3836
if not callable(linkcode_func):
3937
print(
@@ -45,9 +43,11 @@ def setup(app: Sphinx) -> Dict[str, Any]:
4543

4644
app.setup_extension('sphinx_github_style.add_linkcode_class')
4745
app.setup_extension('sphinx_github_style.github_style')
48-
app.setup_extension('sphinx_github_style.meth_lexer')
4946
app.setup_extension('sphinx.ext.linkcode')
5047

48+
# Add lexer after linkcode sets the top level
49+
app.connect('env-updated', add_lexer)
50+
5151
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
5252

5353

@@ -58,6 +58,11 @@ def add_static_path(app) -> None:
5858
)
5959

6060

61+
def add_lexer(app: Sphinx, env) -> None:
62+
"""Registers the :class:`~.TDKMethLexer` to add GitHub dark syntax highlighting"""
63+
app.add_lexer('python', TDKMethLexer.get_pkg_lexer())
64+
65+
6166
def get_linkcode_revision(blob: str) -> str:
6267
"""Get the blob to link to on GitHub
6368
@@ -157,9 +162,6 @@ def get_linkcode_resolve(linkcode_url: str, repo_dir: Optional[Path] = None, top
157162
if repo_dir is None:
158163
repo_dir = get_repo_dir()
159164

160-
if top_level is None:
161-
top_level = get_top_level(repo_dir)
162-
163165
def linkcode_resolve(domain, info):
164166
"""Returns a link to the source code on GitHub, with appropriate lines highlighted
165167
@@ -174,6 +176,10 @@ def linkcode_resolve(domain, info):
174176
modname = info['module']
175177
fullname = info['fullname']
176178

179+
if TDKMethLexer.TOP_LEVEL is None:
180+
pkg_name = modname.split('.')[0]
181+
TDKMethLexer.TOP_LEVEL = pkg_name
182+
177183
submod = sys.modules.get(modname)
178184
if submod is None:
179185
return None
@@ -212,19 +218,6 @@ def linkcode_resolve(domain, info):
212218
return linkcode_resolve
213219

214220

215-
def get_top_level(repo_dir: Optional[Path] = None) -> str:
216-
"""Retrieve the top-level module name of a package from its metadata.
217-
218-
:param repo_dir: The root directory of the Git repository.
219-
:return: The top-level module name of the package.
220-
"""
221-
if repo_dir is None:
222-
repo_dir = get_repo_dir()
223-
224-
pkg = pkg_resources.require(Path(repo_dir).stem)[0]
225-
return pkg.get_metadata('top_level.txt').strip()
226-
227-
228221
def get_repo_dir() -> Path:
229222
"""Returns the root directory of the repository
230223

sphinx_github_style/meth_lexer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import types
2-
from typing import Type
3-
from sphinx.application import Sphinx
2+
from typing import Type, Optional
43
from pygments.lexers.python import NumPyLexer
54
from inspect import getmembers, isfunction, ismethod, ismodule, isclass
65

@@ -31,17 +30,19 @@ class TDKMethLexer(NumPyLexer):
3130
name = 'TDK'
3231
url = 'https://github.com/TDKorn'
3332
aliases = ['tdk']
34-
35-
EXTRA_KEYWORDS = {}
33+
TOP_LEVEL = None
3634

3735
@classmethod
38-
def get_pkg_lexer(cls, pkg_name: str) -> Type["TDKMethLexer"]:
39-
pkg = __import__(pkg_name)
36+
def get_pkg_lexer(cls, pkg_name: Optional[str] = None) -> Type["TDKMethLexer"]:
37+
if pkg_name:
38+
cls.TOP_LEVEL = pkg_name
39+
40+
if not cls.TOP_LEVEL:
41+
raise ValueError('Must provide a package name')
42+
43+
pkg = __import__(cls.TOP_LEVEL)
4044
funcs = get_pkg_funcs(pkg)
4145
cls.EXTRA_KEYWORDS = funcs
4246
return cls
4347

4448

45-
def setup(app: Sphinx):
46-
top_level = app.config._raw_config.get('top_level', getattr(app.config, 'top_level'))
47-
app.add_lexer('python', TDKMethLexer.get_pkg_lexer(top_level))

0 commit comments

Comments
 (0)