Skip to content

Commit

Permalink
feat: Add docname attr for snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Aug 25, 2024
1 parent b1e6d77 commit 5e03ca5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/sphinxnotes/snippet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand All @@ -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

Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/sphinxnotes/snippet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 5e03ca5

Please sign in to comment.