Skip to content

Commit 5319872

Browse files
committed
ENH(sphinxext): +CSS to plot border * width %100
to help navigate zoom. This reverts commit 4501d5e: "DROP(sphinxext): CSS+importlib_resources are not really needed"
1 parent 44655d6 commit 5319872

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

graphtik/sphinxext/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
from collections import defaultdict
99
from pathlib import Path
10+
from shutil import copyfileobj
1011
from typing import Dict, List, Set, Union, cast
1112

1213
import sphinx
@@ -28,6 +29,12 @@
2829

2930
from .. import __version__
3031

32+
try:
33+
import importlib.resources as pkg_resources
34+
except ImportError:
35+
# Use backported to PY<3.7 `importlib_resources` lib.
36+
import importlib_resources as pkg_resources
37+
3138
obj_name = "graphtik diagram"
3239
role_name = "graphtik"
3340
log = logging.getLogger(__name__)
@@ -444,6 +451,26 @@ def _purge_old_document_images(app: Sphinx, env: BuildEnvironment, docname: str)
444451
env.graphtik_image_purgatory = DocFilesPurgatory()
445452

446453

454+
def _stage_my_pkg_resource(inp_fname, out_fpath):
455+
with pkg_resources.open_binary(__package__, inp_fname) as inp, open(
456+
out_fpath, "wb"
457+
) as out:
458+
copyfileobj(inp, out)
459+
460+
461+
_css_fname = "graphtik.css"
462+
463+
464+
def _copy_graphtik_static_assets(app: Sphinx, exc: Exception) -> None:
465+
"""Callback of `build-finished`` event. """
466+
if not exc and _should_work(app):
467+
dst = Path(app.outdir, "_static", _css_fname)
468+
## Builder `latex` does not have _static folder.
469+
#
470+
if not dst.exists() and dst.parent.exists():
471+
_stage_my_pkg_resource(_css_fname, dst)
472+
473+
447474
def _validate_and_apply_configs(app: Sphinx, config: Config):
448475
"""Callback of `config-inited`` event. """
449476
config.graphtik_default_graph_format is None or _valid_format_option(
@@ -507,6 +534,9 @@ def setup(app: Sphinx):
507534
app.connect("config-inited", _validate_and_apply_configs)
508535
app.connect("doctree-read", _run_doctests_on_graphtik_document)
509536
app.connect("env-purge-doc", _purge_old_document_images)
537+
app.connect("build-finished", _copy_graphtik_static_assets)
538+
539+
app.add_css_file(_css_fname)
510540

511541
# Permanently set this, or else, e.g. +SKIP will not work!
512542
app.config.trim_doctest_flags = False

graphtik/sphinxext/graphtik.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2020-2020, Kostis Anagnostopoulos;
3+
* Licensed under the terms of the Apache License, Version 2.0. See the LICENSE file associated with the project for terms.
4+
*/
5+
6+
/** Applied on IMG or OBJECT */
7+
.graphtik-zoomable-svg {
8+
border-style: inset;
9+
max-width: 100%;
10+
}

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
with io.open("graphtik/__init__.py", "rt", encoding="utf8") as f:
1616
version = re.search(r'__version__ = "(.*?)"', f.read()).group(1)
1717

18-
plot_deps = ["pydot", "jinja2"]
18+
plot_deps = ["pydot", "jinja2", "importlib_resources; python_version<'3.7'"]
1919
matplot_deps = plot_deps + ["matplotlib"]
2020
sphinx_deps = plot_deps + ["sphinx >=2"]
2121
test_deps = list(
@@ -52,6 +52,7 @@
5252
"Bug Tracker": "https://github.com/pygraphkit/graphtik/issues",
5353
},
5454
packages=find_packages(exclude=["test"]),
55+
package_data={"graphtik.sphinxext": ["*.css"]},
5556
python_requires=">=3.6",
5657
install_requires=[
5758
"contextvars; python_version < '3.7'",

0 commit comments

Comments
 (0)