Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Oct 21, 2024
1 parent 8ed2bd4 commit 68d36af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/sphinxnotes/snippet/integration/binding.nvim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function! g:SphinxNotesSnippetListAndView()
function! s:CallView(selection)
call g:SphinxNotesSnippetView(s:SplitID(a:selection))
endfunction
call g:SphinxNotesSnippetList(function('s:CallView'), 'ds')
call g:SphinxNotesSnippetList(function('s:CallView'), '*')
endfunction

" https://github.com/anhmv/vim-float-window/blob/master/plugin/float-window.vim
Expand Down Expand Up @@ -40,7 +40,7 @@ function! g:SphinxNotesSnippetView(id)
" Press enter to return
nmap <buffer> <CR> :call nvim_win_close(g:sphinx_notes_snippet_win, v:true)<CR>
let cmd = [s:snippet, 'get', '--text', a:id]
let cmd = [s:snippet, 'get', '--src', a:id]
call append(line('$'), ['.. hint:: Press <ENTER> to return'])
execute '$read !' . '..'
execute '$read !' . join(cmd, ' ')
Expand Down
4 changes: 2 additions & 2 deletions src/sphinxnotes/snippet/integration/binding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# :Version: 20240828

function snippet_view() {
selection=$(snippet_list --tags c)
selection=$(snippet_list)
[ -z "$selection" ] && return

# Make sure we have $PAGER
Expand All @@ -18,7 +18,7 @@ function snippet_view() {
fi
fi

echo "$SNIPPET get --text $selection | $PAGER"
echo "$SNIPPET get --src $selection | $PAGER"
}

function snippet_edit() {
Expand Down
42 changes: 23 additions & 19 deletions src/sphinxnotes/snippet/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import itertools
from os import path
import sys
from pygments.lexers.shell import BashSessionLexer

from docutils import nodes

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


class Code(Snippet):
ALLOWED_LANGS = ['console']
#: Language of code block
lang: str
#: Description of code block, usually the text of preceding paragraph
desc: str

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

self.lang = node['language']
if self.lang not in self.ALLOWED_LANGS:
if self.lang not in BashSessionLexer.aliases: # TODO: support more language
raise ValueError(
f'Language of node {node} {self.lang} not in allowed language list {self.ALLOWED_LANGS}',
f'Language {self.lang} is not supported',
)

self.desc = ''
# Use the preceding paragraph as descritpion. We usually write some
# descritpions before a code block. For example, The ``::`` syntax is
# a common way to create code block::
#
# | Foo:: | <paragraph>
# | | Foo:
# | Bar | <literal_block xml:space="preserve">
# | | Bar
#
# In this case, the paragraph "Foo:" is the descritpion of the code block.
# This convention also applies to the code, code-block, sourcecode directive.
if isinstance(para := node.previous_sibling(), nodes.paragraph):
# Use the preceding paragraph as descritpion.
#
# We usually write some descritpions before a code block, for example,
# The ``::`` syntax is a common way to create code block::
#
# | Foo:: | <paragraph>
# | | Foo:
# | Bar | <literal_block xml:space="preserve">
# | | Bar
#
# In this case, the preceding paragraph "Foo:" is the descritpion
# of the code block. This convention also applies to the code,
# code-block, sourcecode directive.

# For better display, the trailing colon is removed.
# TODO: https://en.wikipedia.org/wiki/Colon_(punctuation)#Computing
self.desc += para.astext().replace('\n', ' ').rstrip(':::︁︓﹕')
if caption := node.get('caption'):
# Use caption as descritpion.
# In sphinx, all of code-block, sourcecode and code have caption option.
# All of code-block, sourcecode and code directives have caption option.
# https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block
self.desc += caption
if not self.desc:
raise ValueError(
f'Node f{node} lacks description: a preceding paragraph or a caption'
)

if isinstance(para, nodes.paragraph):
# If we have a paragraph preceding code block, include it.
super().__init__(para, node)
# Fixup text field, it should be pure code.
self.text = node.astext().split('\n')
else:
super().__init__(node)


class WithTitle(object):
title: str
Expand Down

0 comments on commit 68d36af

Please sign in to comment.