Skip to content

Commit

Permalink
refactor: Move extension entrypoint from ext.py to __init__.py
Browse files Browse the repository at this point in the history
Then it can be consistency with other sphinxnotes projects.
  • Loading branch information
SilverRainZ committed Oct 13, 2024
1 parent c2ba165 commit b042146
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../src/sphinxnotes'))
extensions.append('snippet.ext')
extensions.append('snippet')

# DOG FOOD CONFIGURATION START

Expand Down
36 changes: 36 additions & 0 deletions src/sphinxnotes/snippet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
sphinxnotes.snippet
~~~~~~~~~~~~~~~~~~~
Sphinx extension entrypoint.
:copyright: Copyright 2024 Shengyu Zhang
:license: BSD, see LICENSE for details.
"""


def setup(app):
# **WARNING**: We don't import these packages globally, because the current
# package (sphinxnotes.snippet) is always resloved when importing
# sphinxnotes.snippet.*. If we import packages here, eventually we will
# load a lot of packages from the Sphinx. It will seriously **SLOW DOWN**
# the startup time of our CLI tool (sphinxnotes.snippet.cli).
#
# .. seealso:: https://github.com/sphinx-notes/snippet/pull/31
from .ext import (
SnippetBuilder,
on_config_inited,
on_env_get_outdated,
on_doctree_resolved,
on_builder_finished,
)

app.add_builder(SnippetBuilder)

app.add_config_value('snippet_config', {}, '')
app.add_config_value('snippet_patterns', {'*': ['.*']}, '')

app.connect('config-inited', on_config_inited)
app.connect('env-get-outdated', on_env_get_outdated)
app.connect('doctree-resolved', on_doctree_resolved)
app.connect('build-finished', on_builder_finished)
18 changes: 3 additions & 15 deletions src/sphinxnotes/snippet/ext.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
sphinxnotes.ext.snippet
sphinxnotes.snippet.ext
~~~~~~~~~~~~~~~~~~~~~~~
Sphinx extension for sphinxnotes.snippet.
Sphinx extension implementation, but the entrypoint is located at __init__.py.
:copyright: Copyright 2021 Shengyu Zhang
:copyright: Copyright 2024 Shengyu Zhang
:license: BSD, see LICENSE for details.
"""

Expand Down Expand Up @@ -206,15 +206,3 @@ def _format_modified_time(timestamp: float) -> str:
"""Return an RFC 3339 formatted string representing the given timestamp."""
seconds, fraction = divmod(timestamp, 1)
return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(seconds)) + f'.{fraction:.3f}'


def setup(app: Sphinx):
app.add_builder(SnippetBuilder)

app.add_config_value('snippet_config', {}, '')
app.add_config_value('snippet_patterns', {'*': ['.*']}, '')

app.connect('config-inited', on_config_inited)
app.connect('env-get-outdated', on_env_get_outdated)
app.connect('doctree-resolved', on_doctree_resolved)
app.connect('build-finished', on_builder_finished)

0 comments on commit b042146

Please sign in to comment.