Skip to content

Commit 1c53109

Browse files
authored
feat: Add docname attr for snippet (#24)
1 parent b1e6d77 commit 1c53109

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/sphinxnotes/snippet/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
"""
88

99
from __future__ import annotations
10-
from typing import List, Tuple, Optional
10+
from typing import List, Tuple, Optional, TYPE_CHECKING
1111
import itertools
1212

1313
from docutils import nodes
1414

15+
if TYPE_CHECKING:
16+
from sphinx.environment import BuildEnvironment
17+
1518
__version__ = '1.1.1'
1619

1720

@@ -22,6 +25,10 @@ class Snippet(object):
2225
:param nodes: Document nodes that make up this snippet
2326
"""
2427

28+
#: docname where the snippet is located, can be referenced by
29+
# :rst:role:`doc`.
30+
docname: str
31+
2532
#: Source file path of snippet
2633
file: str
2734

@@ -41,7 +48,9 @@ class Snippet(object):
4148
def __init__(self, *nodes: nodes.Node) -> None:
4249
assert len(nodes) != 0
4350

51+
env: BuildEnvironment = nodes[0].document.settings.env
4452
self.file = nodes[0].source
53+
self.docname = env.path2doc(self.file)
4554

4655
lineno = [float('inf'), -float('inf')]
4756
for node in nodes:

src/sphinxnotes/snippet/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def main(argv: List[str] = sys.argv[1:]):
100100
formatter_class=HelpFormatter,
101101
help='get information of snippet by index ID',
102102
)
103+
getparser.add_argument(
104+
'--docname', '-d', action='store_true', help='get docname of snippet'
105+
)
103106
getparser.add_argument(
104107
'--file', '-f', action='store_true', help='get source file path of snippet'
105108
)
@@ -216,6 +219,8 @@ def _on_command_get(args: argparse.Namespace):
216219
sys.exit(1)
217220
if args.text:
218221
print('\n'.join(item.snippet.rst))
222+
if args.docname:
223+
print(item.snippet.docname)
219224
if args.file:
220225
print(item.snippet.file)
221226
if args.url:

0 commit comments

Comments
 (0)