|
2 | 2 | sphinxnotes-snippet |
3 | 3 | =================== |
4 | 4 |
|
5 | | ------------------------------------------------------- |
6 | | -Non-intrusive snippet manager for Sphinx documentation |
7 | | ------------------------------------------------------- |
8 | | - |
9 | 5 | .. image:: https://img.shields.io/github/stars/sphinx-notes/snippet.svg?style=social&label=Star&maxAge=2592000 |
10 | 6 | :target: https://github.com/sphinx-notes/snippet |
11 | 7 |
|
12 | 8 | :version: |version| |
13 | 9 | :copyright: Copyright ©2021 by Shengyu Zhang. |
14 | 10 | :license: BSD, see LICENSE for details. |
15 | 11 |
|
16 | | -.. contents:: |
| 12 | +Inspecting snippets of Sphinx documentation. |
| 13 | + |
| 14 | +.. _Sphinx: https://www.sphinx-doc.org/ |
| 15 | + |
| 16 | +.. contents:: Table of Contents |
17 | 17 | :local: |
18 | 18 | :backlinks: none |
19 | 19 |
|
| 20 | +Introduction |
| 21 | +============ |
| 22 | + |
| 23 | +Documents in Sphinx can be parsed into doctree_ (In other words, Abstract Syntax Tree), all contents of documents are parsed into node of this tree. The :ref:`ext` collects snippets (meaningful doctree_ nodes) during the build phase of Sphinx, and provide a :ref:`cli` for your accessing. |
| 24 | + |
| 25 | +.. _doctree: https://docutils.sourceforge.io/docs/ref/doctree.html |
| 26 | + |
| 27 | +How It Works |
| 28 | +------------ |
| 29 | + |
| 30 | +In more detail, when collecting snippets, the :ref:`ext` does these things: |
| 31 | + |
| 32 | +- Create unique ID |
| 33 | +- Collect meta info of snippet |
| 34 | +- Extract keywords from snippet content (In NLP way) |
| 35 | + |
| 36 | + - For Chinese content, keywords are converted to PinYin |
| 37 | + |
| 38 | +- Dump the aboved information to disk |
| 39 | + |
| 40 | +The :ref:`cli` can read these information from disk, list snippets or get snippet by ID. |
| 41 | + |
| 42 | +What Can This Do |
| 43 | +---------------- |
| 44 | + |
| 45 | +It depends on your imagination. |
| 46 | + |
| 47 | +The most common usage is, feed snippet list to a fuzzy finder like Fzf_, we can search the snippet by fuzzy keywords. Once we get the snippet ID, we can access all its meta information, view its source code, edit its corresponding file, open its corresponding HTML documentation... |
| 48 | + |
| 49 | +Which means, you can integrate your most handy tools with Sphinx by writing scripts. We also provide some examples about this, please refer to :ref:`integration`. |
| 50 | + |
| 51 | +.. _Fzf: https://github.com/junegunn/fzf |
| 52 | + |
20 | 53 | Installation |
21 | 54 | ============ |
22 | 55 |
|
23 | | -Download it from official Python Package Index: |
| 56 | +1. Download it from official Python Package Index: |
| 57 | + |
| 58 | + .. code-block:: console |
| 59 | +
|
| 60 | + # pip install sphinxnotes-snippet |
| 61 | +
|
| 62 | +2. Make sure you have ``snippet`` command in your ``$PATH`` |
| 63 | +3. Add extension to :file:`conf.py` in your Sphinx project: |
| 64 | + |
| 65 | + .. code-block:: python |
| 66 | +
|
| 67 | + extensions = [ |
| 68 | + # … |
| 69 | + 'sphinxnotes.snippet.ext', |
| 70 | + # … |
| 71 | + ] |
| 72 | +
|
| 73 | +4. Rebuild documentation, then invoke ``snippet stat``, the project name is expected to be seen in output. |
| 74 | + |
| 75 | +.. _integration: |
| 76 | + |
| 77 | +Integration |
| 78 | +=========== |
| 79 | + |
| 80 | +Currently we provide integration for Bash, Zsh, and Vim, you can use the following fucntion after you activated the corresponding configuration (see subsections). |
| 81 | +Beside, Fzf_ is always required. |
| 82 | + |
| 83 | +Fast Edit |
| 84 | + :Shortcut: :kbd:`Ctrl+k,e` |
| 85 | + |
| 86 | + Fuzzy find snippet with Fzf_ and edit corresponding file with vim |
| 87 | + |
| 88 | + .. note:: :kbd:`Ctrl+k,e` means: Press :kbd:`Ctrl+k` first, then press :kbd:`e` immediately, same below |
| 89 | + |
| 90 | +Fast View HTML |
| 91 | + :Shortcut: :kbd:`Ctrl+k,u` |
| 92 | + |
| 93 | + Fuzzy find snippet with Fzf_ and open its corresponding HTML URL with xdg-open |
| 94 | + |
| 95 | + .. note:: Before use this function, you should configurate ``base_url`` in CLI tool :ref:`cli-conf` |
24 | 96 |
|
25 | | -.. code-block:: console |
| 97 | +Bash |
| 98 | +---- |
26 | 99 |
|
27 | | - $ pip install sphinxnotes-snippet |
| 100 | +Add the following code to your :file:`~/.bashrc`: |
28 | 101 |
|
29 | | -Add extension to :file:`conf.py` in your sphinx project: |
| 102 | +.. code-block:: bash |
30 | 103 |
|
31 | | -TODO |
| 104 | + eval "$(snippet integration --sh --sh-binding)" |
32 | 105 |
|
33 | | -.. code-block:: python |
| 106 | +Zsh |
| 107 | +--- |
34 | 108 |
|
35 | | - extensions = [ |
36 | | - # … |
37 | | - 'sphinxnotes.snippet.ext', |
38 | | - # … |
39 | | - ] |
| 109 | +Add the following code to your :file:`~/.zshrc`: |
40 | 110 |
|
| 111 | +.. code-block:: zsh |
41 | 112 |
|
42 | | -.. _Configuration: |
| 113 | + eval "$(snippet integration --zsh --zsh-binding)" |
| 114 | +
|
| 115 | +Fast edit demo: |
| 116 | + |
| 117 | +.. asciinema:: /_assets/zsh.cast |
| 118 | + |
| 119 | +Vim |
| 120 | +--- |
| 121 | + |
| 122 | +Add the following code to your :file:`~/.vimrc`: |
| 123 | + |
| 124 | +.. code-block:: vim |
| 125 | +
|
| 126 | + let snippet_vim = tempname() |
| 127 | + call system('snippet integration --vim --vim-binding>' . snippet_vim) |
| 128 | + execute 'source ' . snippet_vim |
| 129 | + call delete(snippet_vim) |
| 130 | +
|
| 131 | +Fast edit demo: |
| 132 | + |
| 133 | +.. asciinema:: /_assets/vim.cast |
| 134 | + |
| 135 | +Usage |
| 136 | +===== |
| 137 | + |
| 138 | +.. _ext: |
43 | 139 |
|
44 | 140 | Extension |
45 | | -========= |
| 141 | +--------- |
| 142 | + |
| 143 | +Append ``sphinxnotes.snippet.ext`` to Sphinx extensions. |
46 | 144 |
|
47 | 145 | Configuration |
48 | | -------------- |
| 146 | +~~~~~~~~~~~~~ |
49 | 147 |
|
50 | 148 | The extension provides the following configuration: |
51 | 149 |
|
52 | | -:snippet_config: (Type: ``Dict[str,Any]``, Default: ``{}``) |
53 | | - Configuration of snippet cli command. |
| 150 | +:snippet_config: |
| 151 | + :Type: ``Dict[str,Any]`` |
| 152 | + |
| 153 | + Custom CLI tool :ref:`cli-conf`. |
| 154 | + |
| 155 | + .. attention:: Maybe deprecated in future |
| 156 | + |
| 157 | +:snippet_patterns: |
| 158 | + :Type: ``Dict[str,List[str]]`` |
| 159 | + :Default: ``{'*': ['.*']}``) |
| 160 | + |
| 161 | + A "snippet tags" → "regular expression list" mapping. |
| 162 | + |
| 163 | + If a snippet's tags are not included in the dict, or the snippet's docname_ does not matched by the any of regular expression of corresponding list, it wil be filtered. |
| 164 | + |
| 165 | + The default vaule ``{'*': ['.*']}`` matchs any snippet. |
54 | 166 |
|
55 | | -:snippet_patterns: (Type: ``Dict[str,List[str]]``, Default: ``{}``) |
| 167 | + .. note:: See `snippet --help` for available snippet tags |
56 | 168 |
|
| 169 | +.. _docname: https://www.sphinx-doc.org/en/master/glossary.html#term-document-name |
| 170 | + |
| 171 | +.. _cli: |
57 | 172 |
|
58 | 173 | Command Line Tool |
59 | | -================= |
| 174 | +----------------- |
| 175 | + |
| 176 | +See ``snippet --help`` for usage. |
| 177 | + |
| 178 | +.. _cli-conf: |
| 179 | + |
| 180 | +Configuration |
| 181 | +~~~~~~~~~~~~~ |
| 182 | + |
| 183 | +The configuration of CLI tools is a python script, located at :file:`$XDG_CONFIG_HOME/sphinxnotes/snippet/conf.py`, Usually :file:`~/.config/sphinxnotes/snippet/conf.py`. |
| 184 | + |
| 185 | +:cache_dir: |
| 186 | + :Type: ``str`` |
| 187 | + :Default: ``"$XDG_CACHE_HOME/sphinxnotes/snippet"`` |
| 188 | + |
| 189 | + Path to snippet cache directory. |
60 | 190 |
|
61 | | -A python script named :file:`snippet` should be installed in your path. |
62 | | -(Usually :file:`/usr/local/bin` or :file:`~/.local/bin`). |
| 191 | +:base_url: |
| 192 | + :Type: ``Dict[str,str]`` |
| 193 | + :Default: ``{}`` |
| 194 | + |
| 195 | + A "project name" → "base URL" mapping. It is used as prefix of snippet URL when you invoke ``snippet get --url <SNIPPET_ID>`` |
| 196 | + |
| 197 | + Base URL can point to your Sphinx site or local HTML file. For local file, URL should use "file://" schema (required by ``xdg-open``), such as: "file:///home/la/documents/bullet/_build/html/". |
| 198 | + |
| 199 | + .. note:: Project name is the `project confval`_ of your Sphinx project. |
| 200 | + |
| 201 | + .. _project confval: https://www.sphinx-doc.org/en/master/usage/configuration.html?highlight=project#confval-project |
63 | 202 |
|
64 | | -Invoke ``snippet --help`` for usage. |
65 | 203 |
|
66 | 204 | Change Log |
67 | 205 | ========== |
68 | 206 |
|
| 207 | +2021-08-?? 1.0 |
| 208 | +-------------- |
| 209 | + |
| 210 | +.. sectionauthor:: Shengyu Zhang |
| 211 | + |
| 212 | +The first stable version is out, enjoy~ |
| 213 | + |
69 | 214 | 2021-03-20 1.0b2 |
70 | 215 | ---------------- |
71 | 216 |
|
|
0 commit comments