Skip to content

Commit 8ed2bd4

Browse files
committed
update
1 parent 860fd1f commit 8ed2bd4

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

src/sphinxnotes/snippet/cli.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ def main(argv: list[str] = sys.argv[1:]):
140140
'--text',
141141
'-t',
142142
action='store_true',
143-
help='get source reStructuredText of snippet',
143+
help='get text representation of snippet',
144+
)
145+
getparser.add_argument(
146+
'--src',
147+
action='store_true',
148+
help='get source text of snippet',
144149
)
145150
getparser.add_argument(
146151
'--url',
@@ -273,7 +278,9 @@ def p(*args, **opts):
273278
p('no such index ID', file=sys.stderr)
274279
sys.exit(1)
275280
if args.text:
276-
p('\n'.join(item.snippet.rst))
281+
p('\n'.join(item.snippet.text))
282+
if args.src:
283+
p('\n'.join(item.snippet.source))
277284
if args.docname:
278285
p(item.snippet.docname)
279286
if args.file:

src/sphinxnotes/snippet/ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def extract_excerpt(s: Snippet) -> str:
5656
elif isinstance(s, Section) and s.title is not None:
5757
return '[' + s.title + ']'
5858
elif isinstance(s, Code):
59-
return s.lang + '`' + s.desc + '`'
59+
return '`' + (s.lang + ':').ljust(8, ' ') + ' ' + s.desc + '`'
6060
return ''
6161

6262

src/sphinxnotes/snippet/integration/binding.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# :Version: 20240828
77

88
function snippet_view() {
9-
selection=$(snippet_list --tags ds)
9+
selection=$(snippet_list --tags c)
1010
[ -z "$selection" ] && return
1111

1212
# Make sure we have $PAGER

src/sphinxnotes/snippet/snippets.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ class Snippet(object):
3838
# :rst:role:`doc`.
3939
docname: str
4040

41-
#: Absolute path of the source file.
41+
#: Absolute path to the source file.
4242
file: str
4343

44-
#: Line number range of snippet, in the source file which is left closed
45-
#: and right opened.
44+
#: Line number range of source file (:attr:`Snippet.file`),
45+
#: left closed and right opened.
4646
lineno: tuple[int, int]
4747

48-
#: The original reStructuredText of snippet
49-
rst: list[str]
48+
#: The source text read from source file (:attr:`Snippet.file`),
49+
# in Markdown or reStructuredText.
50+
source: list[str]
51+
52+
#: Text representation of the snippet, usually generated form
53+
# :meth:`nodes.Element.astext`.
54+
text: list[str]
5055

5156
#: The possible identifier key of snippet, which is picked from nodes'
5257
#: (or nodes' parent's) `ids attr`_.
@@ -57,7 +62,7 @@ class Snippet(object):
5762
def __init__(self, *nodes: nodes.Element) -> None:
5863
assert len(nodes) != 0
5964

60-
env: BuildEnvironment = nodes[0].document.settings.env
65+
env: BuildEnvironment = nodes[0].document.settings.env # type: ignore
6166

6267
file, docname = None, None
6368
for node in nodes:
@@ -66,7 +71,7 @@ def __init__(self, *nodes: nodes.Element) -> None:
6671
docname = env.path2doc(file)
6772
break
6873
if not file or not docname:
69-
raise ValueError('Missing source file or docname')
74+
raise ValueError(f'Nodes {nodes} lacks source file or docname')
7075
self.file = file
7176
self.docname = docname
7277

@@ -78,13 +83,18 @@ def __init__(self, *nodes: nodes.Element) -> None:
7883
lineno[1] = max(lineno[1], _line_of_end(node))
7984
self.lineno = (lineno[0], lineno[1])
8085

81-
lines = []
86+
source = []
8287
with open(self.file, 'r') as f:
8388
start = self.lineno[0] - 1
8489
stop = self.lineno[1] - 1
8590
for line in itertools.islice(f, start, stop):
86-
lines.append(line.strip('\n'))
87-
self.rst = lines
91+
source.append(line.strip('\n'))
92+
self.source = source
93+
94+
text = []
95+
for node in nodes:
96+
text.extend(node.astext().split('\n'))
97+
self.text = text
8898

8999
# Find exactly one ID attr in nodes
90100
self.refid = None
@@ -102,19 +112,21 @@ def __init__(self, *nodes: nodes.Element) -> None:
102112

103113

104114
class Code(Snippet):
115+
ALLOWED_LANGS = ['console']
105116
#: Language of code block
106117
lang: str
107118
#: Description of code block, usually the text of preceding paragraph
108119
desc: str
109-
#: The code itself.
110-
code: str
111120

112121
def __init__(self, node: nodes.literal_block) -> None:
113122
assert isinstance(node, nodes.literal_block)
114123
super().__init__(node)
115124

116125
self.lang = node['language']
117-
self.code = node.astext()
126+
if self.lang not in self.ALLOWED_LANGS:
127+
raise ValueError(
128+
f'Language of node {node} {self.lang} not in allowed language list {self.ALLOWED_LANGS}',
129+
)
118130

119131
self.desc = ''
120132
if isinstance(para := node.previous_sibling(), nodes.paragraph):
@@ -131,7 +143,10 @@ def __init__(self, node: nodes.literal_block) -> None:
131143
# In this case, the preceding paragraph "Foo:" is the descritpion
132144
# of the code block. This convention also applies to the code,
133145
# code-block, sourcecode directive.
134-
self.desc += para.astext().replace('\n', ' ')
146+
147+
# For better display, the trailing colon is removed.
148+
# TODO: https://en.wikipedia.org/wiki/Colon_(punctuation)#Computing
149+
self.desc += para.astext().replace('\n', ' ').rstrip(':::︁︓﹕')
135150
if caption := node.get('caption'):
136151
# Use caption as descritpion.
137152
# In sphinx, all of code-block, sourcecode and code have caption option.
@@ -147,10 +162,9 @@ class WithTitle(object):
147162
title: str
148163

149164
def __init__(self, node: nodes.Element) -> None:
150-
if title := node.next_node(nodes.title):
151-
self.title = title.astext()
152-
else:
165+
if not (title := node.next_node(nodes.title)):
153166
raise ValueError(f'Node f{node} lacks title')
167+
self.title = title.astext()
154168

155169

156170
class Section(Snippet, WithTitle):

0 commit comments

Comments
 (0)