1. 简介
在使用 sphinx 写文档的过程中, 尤其是 Linux 教程, 经常需要将控制台中的命令以及执行结果复制到文档中, 并修改格式, 非常的麻烦.
sphinx-console 是一个可以自动执行指定命令, 并对命令和输出进行自动渲染的 sphinx 扩展, 你可以非常方便的使用 sphinx-console 在你的 sphinx 文档中插入终端命令以及输出结果. 比如:
显示系统版本.
$ python3 -c "from platform import platform; print(platform())" Linux-5.19.0-1028-aws-x86_64-with-glibc2.35
执行
ls -al
命令.$ ls -al total 36 drwxr-xr-x 5 docs docs 4096 Oct 11 06:08 . drwxr-xr-x 8 docs docs 4096 Oct 11 06:08 .. drwxr-xr-x 3 docs docs 4096 Oct 11 06:08 _build drwxr-xr-x 3 docs docs 4096 Oct 11 06:07 chapters -rw-r--r-- 1 docs docs 6181 Oct 11 06:08 conf.py -rw-r--r-- 1 docs docs 283 Oct 11 06:07 index.rst -rw-r--r-- 1 docs docs 34 Oct 11 06:07 requirements.txt drwxr-xr-x 2 docs docs 4096 Oct 11 06:07 statics
启动 Python, 并执行
import this
命令, 然后执行exit()
退出 Python.$ python3 Python 3.9.17 (main, Jul 27 2023, 08:42:23) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>> exit()
执行
ping
命令.$ ping localhost -c 4 PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.021 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.022 ms 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.022 ms 64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.021 ms --- localhost ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3077ms rtt min/avg/max/mdev = 0.021/0.021/0.022/0.000 ms
当然, 你可以只显示命令本身.
$ ping localhost -c 4
值得注意的是, sphinx-console 也支持持颜色显示1.
$ bash color_test.sh 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 52 53 54 55 56 57 88 89 90 91 92 93 22 23 24 25 26 27 58 59 60 61 62 63 94 95 96 97 98 99 28 29 30 31 32 33 64 65 66 67 68 69 100 101 102 103 104 105 34 35 36 37 38 39 70 71 72 73 74 75 106 107 108 109 110 111 40 41 42 43 44 45 76 77 78 79 80 81 112 113 114 115 116 117 46 47 48 49 50 51 82 83 84 85 86 87 118 119 120 121 122 123 124 125 126 127 128 129 160 161 162 163 164 165 196 197 198 199 200 201 130 131 132 133 134 135 166 167 168 169 170 171 202 203 204 205 206 207 136 137 138 139 140 141 172 173 174 175 176 177 208 209 210 211 212 213 142 143 144 145 146 147 178 179 180 181 182 183 214 215 216 217 218 219 148 149 150 151 152 153 184 185 186 187 188 189 220 221 222 223 224 225 154 155 156 157 158 159 190 191 192 193 194 195 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
footnotes