diff --git a/src/sphinxnotes/snippet/__init__.py b/src/sphinxnotes/snippet/__init__.py index 9736afd..042d354 100644 --- a/src/sphinxnotes/snippet/__init__.py +++ b/src/sphinxnotes/snippet/__init__.py @@ -7,11 +7,14 @@ """ from __future__ import annotations -from typing import List, Tuple, Optional +from typing import List, Tuple, Optional, TYPE_CHECKING import itertools from docutils import nodes +if TYPE_CHECKING: + from sphinx.environment import BuildEnvironment + __version__ = '1.1.1' @@ -22,6 +25,10 @@ class Snippet(object): :param nodes: Document nodes that make up this snippet """ + #: docname where the snippet is located, can be referenced by + # :rst:role:`doc`. + docname: str + #: Source file path of snippet file: str @@ -41,7 +48,9 @@ class Snippet(object): def __init__(self, *nodes: nodes.Node) -> None: assert len(nodes) != 0 + env: BuildEnvironment = nodes[0].document.settings.env self.file = nodes[0].source + self.docname = env.path2doc(self.file) lineno = [float('inf'), -float('inf')] for node in nodes: diff --git a/src/sphinxnotes/snippet/cli.py b/src/sphinxnotes/snippet/cli.py index 277d410..dcc732f 100644 --- a/src/sphinxnotes/snippet/cli.py +++ b/src/sphinxnotes/snippet/cli.py @@ -100,6 +100,9 @@ def main(argv: List[str] = sys.argv[1:]): formatter_class=HelpFormatter, help='get information of snippet by index ID', ) + getparser.add_argument( + '--docname', '-d', action='store_true', help='get docname of snippet' + ) getparser.add_argument( '--file', '-f', action='store_true', help='get source file path of snippet' ) @@ -216,6 +219,8 @@ def _on_command_get(args: argparse.Namespace): sys.exit(1) if args.text: print('\n'.join(item.snippet.rst)) + if args.docname: + print(item.snippet.docname) if args.file: print(item.snippet.file) if args.url: