Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix imports #11

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions src/moldoc/_internal/moldoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from docutils import nodes
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.util.docutils import SphinxDirective
from sphinx.writers.html5 import HTML5Translator

Expand Down Expand Up @@ -42,6 +43,11 @@ def run(self) -> list[MolDocNode]:
moldoc_name=f'moldoc_{self.env.new_serialno("moldoc")}',
molecule=globals_["moldoc_display_molecule"],
)

if not hasattr(self.env, "moldoc_documents"):
self.env.moldoc_documents = set() # type: ignore[attr-defined]

self.env.moldoc_documents.add(self.env.docname) # type: ignore[attr-defined]
return [node]


Expand All @@ -57,23 +63,15 @@ def html_moldoc(self: HTML5Translator, node: MolDocNode) -> None:
moldoc_node_id = node.get_moldoc_name()

if not getattr(self, "moldoc_scripts_added", False):
self.body.append(
'<script src="./_static/three.min.js"></script>'
'<script src="./_static/molDraw.js"></script>'
'<script src="../_static/three.min.js"></script>'
'<script src="../_static/molDraw.js"></script>'
"<script>const md=molDraw;"
"let atoms=[];"
"let bonds=[];"
"let maybeMolecule=undefined;"
"</script>"
)
self.body.append("<script>let moldoc_molecules=[];</script>")
self.moldoc_scripts_added = True

content = (
f"atoms={get_atom_array(molecule.get_atoms())};"
f"bonds={get_bond_array(molecule.get_bonds())};"
"maybeMolecule=md.maybeMolecule(atoms)(bonds);"
"moldoc_molecules.push(() => {"
"const md = molDraw;"
f"let atoms={get_atom_array(molecule.get_atoms())};"
f"let bonds={get_bond_array(molecule.get_bonds())};"
"let maybeMolecule=md.maybeMolecule(atoms)(bonds);"
"if (md.isLeft(maybeMolecule))"
"{"
'console.log("There was an issue with your molecule.");'
Expand All @@ -90,6 +88,7 @@ def html_moldoc(self: HTML5Translator, node: MolDocNode) -> None:
"})(molecule);"
"md.drawMol(scene(meshes));"
"}"
"});"
)

self.body.append(
Expand All @@ -116,8 +115,48 @@ def copy_asset_files(app: Sphinx, exc: Exception | None) -> None:
raise RuntimeError(msg)


def add_moldoc_scripts(
app: Sphinx,
pagename: str,
templatename: str, # noqa: ARG001
context: dict[str, typing.Any], # noqa: ARG001
doctree: nodes.document, # noqa: ARG001
) -> None:
if (
hasattr(app.env, "moldoc_documents")
and pagename in app.env.moldoc_documents
):
app.add_js_file("three.min.js")
app.add_js_file("molDraw.js")
app.add_js_file(None, body="moldoc_molecules.forEach(f=>f());")


def purge_moldoc_documents(
app: Sphinx, # noqa: ARG001
env: BuildEnvironment,
docnames: list[str],
) -> None:
if hasattr(env, "moldoc_documents"):
env.moldoc_documents.difference_update(docnames)


def merge_moldoc_documents(
app: Sphinx, # noqa: ARG001
env: BuildEnvironment,
docnames: list[str], # noqa: ARG001
other: BuildEnvironment,
) -> None:
if not hasattr(env, "moldoc_documents"):
env.moldoc_documents = set() # type: ignore[attr-defined]
if hasattr(other, "moldoc_documents"):
env.moldoc_documents.update(other.moldoc_documents) # type: ignore[attr-defined]


def setup(app: Sphinx) -> dict:
app.connect("build-finished", copy_asset_files)
app.connect("html-page-context", add_moldoc_scripts)
app.connect("env-purge-doc", purge_moldoc_documents)
app.connect("env-merge-info", merge_moldoc_documents)
app.add_directive("moldoc", MolDoc)
app.add_node(
node=MolDocNode,
Expand Down
11 changes: 9 additions & 2 deletions tests/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ Welcome to moldoc-test's documentation!
=======================================

.. toctree::
:maxdepth: 2
:caption: Contents:
:maxdepth: 2
:caption: Contents:

nest/index.rst
nest/foo.rst
nest/nest/index.rst
nest/nest/nest/index.rst
nest/nest/nest/nest/index.rst
nest/nest/nest/nest/nest/index.rst


.. moldoc::
Expand Down
22 changes: 22 additions & 0 deletions tests/source/nest/foo.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.. moldoc-test documentation master file, created by
sphinx-quickstart on Fri Jul 30 17:53:00 2021.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to moldoc-test's documentation!
=======================================

.. toctree::
:maxdepth: 2
:caption: Contents:


Some content but no moldoc directive


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
Loading
Loading