1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#!/usr/bin/env python3
#
# assets.py
"""
Role to provide a link to open a file within the web browser, rather than downloading it.
.. versionadded:: 0.5.0
.. extensions:: sphinx_toolbox.assets
.. TODO:: Handle non-HTML builders
Perhaps just put a message to see the online documentation?
Usage
------
.. latex:vspace:: -10px
.. rst:role:: asset
Adds a link to a local file that can be viewed within the web browser.
The file will be copied from the directory set in :confval:`assets_dir` to ``/_assets`` in the HTML output.
This is similar to the :rst:role:`download` role, but that role will download the file to the user's computer instead.
This role may be useful for PDFs, which most web browsers can display.
If the file can't be found an error will be shown in the build output:
.. code-block:: text
<page where the error occurred>: Asset file '<missing asset file name>' not found.
**Asset**
.. rest-example::
:asset:`hello_world.txt`
:asset:`hello_world <hello_world.txt>`
**Download**
.. rest-example::
:download:`hello_world.txt <../assets/hello_world.txt>`
Configuration
---------------
.. confval:: assets_dir
:type: :class:`str`
:required: False
:default: ``'./assets'``
The directory in which to find assets for the :rst:role:`asset` role.
API Reference
---------------
"""
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# stdlib
import pathlib
import posixpath
import shutil
from typing import Dict, List, Sequence, Tuple
# 3rd party
from docutils import nodes
from docutils.nodes import system_message
from docutils.parsers.rst.states import Inliner
from domdf_python_tools.paths import PathPlus
from domdf_python_tools.utils import stderr_writer
from sphinx.application import Sphinx
from sphinx.util import split_explicit_title
from sphinx.writers.html5 import HTML5Translator
__all__ = (
"asset_role",
"AssetNode",
"visit_asset_node",
"depart_asset_node",
"setup",
)
# this package
from sphinx_toolbox.utils import SphinxExtMetadata, metadata_add_version
class AssetNode(nodes.reference):
"""
Node representing a link to an asset.
"""
def asset_role(
name: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: Dict = {},
content: List[str] = []
) -> Tuple[Sequence[AssetNode], List[system_message]]:
"""
Adds a link to an asset.
:param name: The local name of the interpreted role, the role name actually used in the document.
:param rawtext: A string containing the entire interpreted text input, including the role and markup.
:param text: The interpreted text content.
:param lineno: The line number where the interpreted text begins.
:param inliner: The :class:`docutils.parsers.rst.states.Inliner` object that called :func:`~.source_role`.
It contains the several attributes useful for error reporting and document tree access.
:param options: A dictionary of directive options for customization (from the ``role`` directive),
to be interpreted by the function.
Used for additional attributes for the generated elements and other functionality.
:param content: A list of strings, the directive content for customization (from the ``role`` directive).
To be interpreted by the function.
:return: A list containing the created node, and a list containing any messages generated during the function.
"""
has_t, title, target = split_explicit_title(text)
title = nodes.unescape(title)
target = nodes.unescape(target)
if not has_t:
if target.startswith('~'):
target = target[1:]
title = pathlib.PurePosixPath(text[1:]).name
app = inliner.document.settings.env.app
base = app.config.assets_dir
node = AssetNode(rawtext, title, refuri=target, source_file=PathPlus(base) / target, **options)
return [node], []
def visit_asset_node(translator: HTML5Translator, node: AssetNode) -> None:
"""
Visit an :class:`~.AssetNode`.
:param translator:
:param node: The node being visited.
"""
if not hasattr(translator, "_asset_node_seen_files"):
# Files that have already been seen
translator._asset_node_seen_files = [] # type: ignore[attr-defined]
assets_out_dir = PathPlus(translator.builder.outdir) / "_assets"
assets_out_dir.maybe_make(parents=True)
source_file = PathPlus(translator.builder.confdir) / node["source_file"]
source_file_exists = source_file.is_file()
source_file_seen = source_file in translator._asset_node_seen_files # type: ignore[attr-defined]
if not source_file_seen and source_file_exists:
# Avoid unnecessary copies of potentially large files.
translator._asset_node_seen_files.append(source_file) # type: ignore[attr-defined]
shutil.copy2(source_file, assets_out_dir)
elif not source_file_exists:
stderr_writer(
f"\x1b[31m{translator.builder.current_docname}: "
f"Asset file '{source_file}' not found.\x1b[39m"
)
translator.context.append('')
return
# Create the HTML
current_uri = (pathlib.PurePosixPath('/') / translator.builder.current_docname).parent
refuri = posixpath.relpath(f"/_assets/{node['refuri']}", str(current_uri))
translator.body.append(f'<a class="reference external" href="{refuri}")/">')
translator.context.append("</a>")
def depart_asset_node(translator: HTML5Translator, node: AssetNode) -> None:
"""
Depart an :class:`~.AssetNode`.
:param translator:
:param node: The node being visited.
"""
translator.body.append(translator.context.pop())
@metadata_add_version
def setup(app: Sphinx) -> SphinxExtMetadata:
"""
Setup :mod:`sphinx_toolbox.assets`.
.. versionadded:: 1.0.0
:param app: The Sphinx application.
"""
app.add_role("asset", asset_role)
app.add_config_value("assets_dir", "./assets", "env", [str])
app.add_node(AssetNode, html=(visit_asset_node, depart_asset_node))
return {"parallel_read_safe": True}
|