File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change 77"""
88
99from __future__ import annotations
10- from typing import List , Tuple , Optional
10+ from typing import List , Tuple , Optional , TYPE_CHECKING
1111import itertools
1212
1313from 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 :
Original file line number Diff line number Diff 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 :
You can’t perform that action at this time.
0 commit comments