Skip to content

Commit 68d36af

Browse files
committed
update
1 parent 8ed2bd4 commit 68d36af

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

src/sphinxnotes/snippet/integration/binding.nvim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function! g:SphinxNotesSnippetListAndView()
1111
function! s:CallView(selection)
1212
call g:SphinxNotesSnippetView(s:SplitID(a:selection))
1313
endfunction
14-
call g:SphinxNotesSnippetList(function('s:CallView'), 'ds')
14+
call g:SphinxNotesSnippetList(function('s:CallView'), '*')
1515
endfunction
1616

1717
" https://github.com/anhmv/vim-float-window/blob/master/plugin/float-window.vim
@@ -40,7 +40,7 @@ function! g:SphinxNotesSnippetView(id)
4040
" Press enter to return
4141
nmap <buffer> <CR> :call nvim_win_close(g:sphinx_notes_snippet_win, v:true)<CR>
4242
43-
let cmd = [s:snippet, 'get', '--text', a:id]
43+
let cmd = [s:snippet, 'get', '--src', a:id]
4444
call append(line('$'), ['.. hint:: Press <ENTER> to return'])
4545
execute '$read !' . '..'
4646
execute '$read !' . join(cmd, ' ')

src/sphinxnotes/snippet/integration/binding.sh

Lines changed: 2 additions & 2 deletions
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 c)
9+
selection=$(snippet_list)
1010
[ -z "$selection" ] && return
1111

1212
# Make sure we have $PAGER
@@ -18,7 +18,7 @@ function snippet_view() {
1818
fi
1919
fi
2020

21-
echo "$SNIPPET get --text $selection | $PAGER"
21+
echo "$SNIPPET get --src $selection | $PAGER"
2222
}
2323

2424
function snippet_edit() {

src/sphinxnotes/snippet/snippets.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import itertools
1414
from os import path
1515
import sys
16+
from pygments.lexers.shell import BashSessionLexer
1617

1718
from docutils import nodes
1819

@@ -112,51 +113,54 @@ def __init__(self, *nodes: nodes.Element) -> None:
112113

113114

114115
class Code(Snippet):
115-
ALLOWED_LANGS = ['console']
116116
#: Language of code block
117117
lang: str
118118
#: Description of code block, usually the text of preceding paragraph
119119
desc: str
120120

121121
def __init__(self, node: nodes.literal_block) -> None:
122122
assert isinstance(node, nodes.literal_block)
123-
super().__init__(node)
124123

125124
self.lang = node['language']
126-
if self.lang not in self.ALLOWED_LANGS:
125+
if self.lang not in BashSessionLexer.aliases: # TODO: support more language
127126
raise ValueError(
128-
f'Language of node {node} {self.lang} not in allowed language list {self.ALLOWED_LANGS}',
127+
f'Language {self.lang} is not supported',
129128
)
130129

131130
self.desc = ''
131+
# Use the preceding paragraph as descritpion. We usually write some
132+
# descritpions before a code block. For example, The ``::`` syntax is
133+
# a common way to create code block::
134+
#
135+
# | Foo:: | <paragraph>
136+
# | | Foo:
137+
# | Bar | <literal_block xml:space="preserve">
138+
# | | Bar
139+
#
140+
# In this case, the paragraph "Foo:" is the descritpion of the code block.
141+
# This convention also applies to the code, code-block, sourcecode directive.
132142
if isinstance(para := node.previous_sibling(), nodes.paragraph):
133-
# Use the preceding paragraph as descritpion.
134-
#
135-
# We usually write some descritpions before a code block, for example,
136-
# The ``::`` syntax is a common way to create code block::
137-
#
138-
# | Foo:: | <paragraph>
139-
# | | Foo:
140-
# | Bar | <literal_block xml:space="preserve">
141-
# | | Bar
142-
#
143-
# In this case, the preceding paragraph "Foo:" is the descritpion
144-
# of the code block. This convention also applies to the code,
145-
# code-block, sourcecode directive.
146-
147143
# For better display, the trailing colon is removed.
148144
# TODO: https://en.wikipedia.org/wiki/Colon_(punctuation)#Computing
149145
self.desc += para.astext().replace('\n', ' ').rstrip(':::︁︓﹕')
150146
if caption := node.get('caption'):
151147
# Use caption as descritpion.
152-
# In sphinx, all of code-block, sourcecode and code have caption option.
148+
# All of code-block, sourcecode and code directives have caption option.
153149
# https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block
154150
self.desc += caption
155151
if not self.desc:
156152
raise ValueError(
157153
f'Node f{node} lacks description: a preceding paragraph or a caption'
158154
)
159155

156+
if isinstance(para, nodes.paragraph):
157+
# If we have a paragraph preceding code block, include it.
158+
super().__init__(para, node)
159+
# Fixup text field, it should be pure code.
160+
self.text = node.astext().split('\n')
161+
else:
162+
super().__init__(node)
163+
160164

161165
class WithTitle(object):
162166
title: str

0 commit comments

Comments
 (0)