当源文件更改时自动构建 Sphinx 文档
我正在使用 Sphinx 来记录我的一个项目,并且我喜欢在浏览器中预览我的更改。我希望能够将一些更改保存到 .rst
文件中,并能够立即刷新浏览器并查看更改。
本质上,我想每当 .rst
文件之一发生更改时自动执行 make html
。
I'm using Sphinx to document one of my projects, and I like to preview my changes in my browser. I want to be able to save some changes to an .rst
file, and be able to immediately refresh my browser and see the changes.
Essentially I want to automatically execute a make html
whenever one of the .rst
files is changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 sphinx-autobuild。它还会自动在浏览器中启动页面刷新。
它很容易使用,例如:
或者,如果您有一个单独的构建目录,
其中
是文档的源目录,
是您想要构建 html 文件的目录。You can use sphinx-autobuild. It will also automatically initiate a page refresh in the browser.
It is easy to use, for example:
or, if you have a separate build dir,
where
<doc-source-dir>
is the source directory of your documentation and<doc-build-dir>
is the directory where you want the built html files.Jacob Kaplan-Moss 有一个很好的解决方案:
注意,更改模式以匹配您的后缀。 Jacob 使用 *.txt,但我需要将其更改为 *.rst。
Jacob Kaplan-Moss has a good solution:
Note, change the pattern to match your suffix. Jacob uses *.txt, but I needed to change it to *.rst.
如果您使用的是 *nix 系统,则可以使用 inotify 来监视文件系统事件并触发操作。
例如,在 ubuntu 上,
然后运行这样的脚本来侦听给定目录中的事件
If you're on a *nix system, you can use inotify to monitor filesystem events and trigger actions.
For example, on ubuntu,
Then run a script like this to listen for events in a given dir
您可以在您最喜欢的编辑器中创建一个宏来保存文件并在浏览器中打开它,任何文本编辑器都可以(geany、gedit、emacs、vi、notepad++...)
You can create a macro in you favorite editor that saves the file and opens it in your browser, any text editor can do (geany, gedit, emacs, vi, notepad++...)
重建页面并在 VSCode 中刷新预览
如果使用 vscode,请安装 实时预览,带有 sphinx-build 和 restructedText 然后,在
zsh
终端中,从您的文档根目录,使用单个命令,您可以:make html
),build/html/doc.html
code -r source/doc.rst
make html
将构建文档树code -r
将在两种情况下重用当前窗口如果每个
doc.html
,实时预览 的doc.html
和doc.rst
均已打开,sphinx 将重建树,vscode 将打开刷新的预览,然后返回编辑状态doc.rst
。更进一步
,创建一个动态键绑定,它将使用任何打开的
.rst
文件的键盘快捷键来完成上述所有操作。将其添加到您的keybindings.json
中:"key": "cmd+shift+R"
是键盘快捷键。当您的rst
文件位于活动编辑器中时运行此命令"command": "workbench.action.terminal.sendSequence"
告诉 vscode 要执行的操作(将 args 作为命令运行在终端中)"args": { "text" :
中的命令VSCODEOPENFILE=${file}
将环境变量设置为当前打开的文件路径sphinx-build -M html ${VSCODEOPENFILE:h} ${${VSCODEOPENFILE:h}/source/build/} ${VSCODEOPENFILE} && code -r ${${VSCODEOPENFILE:h}/source/build/}${${VSCODEOPENFILE:t}/rst/html}
将仅在当前文件上运行 sphinx 重建-M html
使用 html 构建器${VSCODEOPENFILE:h}
是source
目录的路径,例如workspace/docs/source
${${VSCODEOPENFILE:h}/source/build/}
将build
替换为source
以获取构建目录的路径,例如,workspace/docs/build
code -r ${${VSCODEOPENFILE:h}/source/build/}${${VSCODEOPENFILE:t}/rst/html}
将替换build
对于source
和html
对于参数中的rst
(即source/doc.rst
将变为build/doc.html
,) 并在现有窗口中重新打开它,强制刷新实时预览code -r ${VSCODEOPENFILE}
将返回到原始窗口(例如,doc.rst
)进行编辑。when
设置将限制Command-Shift-R
命令仅在编辑器处于焦点状态且非只读时才起作用。注意此代码适用于 mac os x 和
zsh
,但可能会在其他地方进行一些调整,这解决了我通过 sphinx-autobuild 进行连续自动重建的问题 我认为是由于
conf.py
中的shutil.copyfile
造成的。我还尝试了一些 vscode sphinx 和 rst-preview 扩展,但它们都不起作用。Rebuild a page and refresh a preview in VSCode
If using vscode, install Live Preview, with sphinx-build and restructuredText then, in a
zsh
terminal, from your doc root dir, in a single command you can:make html
),build/html/doc.html
code -r source/doc.rst
make html
will build the doc treecode -r
will reuse the current window in both casesIf each of
doc.html
, the live preview ofdoc.html
, anddoc.rst
are all open already, sphinx will rebuild the tree and vscode will open the refreshed preview, and return you to editingdoc.rst
.Take it one step further
and create a dynamic keybinding, which will do all of the above with a keyboard shortcut for any open
.rst
file. Add this to yourkeybindings.json
:"key": "cmd+shift+R"
is the keyboard shortcut. Run this when yourrst
file is in the active editor"command": "workbench.action.terminal.sendSequence"
tells vscode what to do (run the args as a command in the terminal)"args": { "text" :
VSCODEOPENFILE=${file}
will set the env var the currently open file pathsphinx-build -M html ${VSCODEOPENFILE:h} ${${VSCODEOPENFILE:h}/source/build/} ${VSCODEOPENFILE} && code -r ${${VSCODEOPENFILE:h}/source/build/}${${VSCODEOPENFILE:t}/rst/html}
will run sphinx rebuild on just the current file-M html
use the html builder${VSCODEOPENFILE:h}
is the path to thesource
dir, e.g.,workspace/docs/source
${${VSCODEOPENFILE:h}/source/build/}
substitutesbuild
forsource
to get the path to the build dir, e.g.,workspace/docs/build
code -r ${${VSCODEOPENFILE:h}/source/build/}${${VSCODEOPENFILE:t}/rst/html}
will substitute bothbuild
forsource
andhtml
forrst
in the argument (i.e.,source/doc.rst
will becomebuild/doc.html
,) and reopen it in the existing window, forcing the live preview to refreshcode -r ${VSCODEOPENFILE}
will return you to the original window (e.g.,doc.rst
,) for editing.when
setting will restrict theCommand-Shift-R
command to work when only when the editor is focused and not read-only.NOTE this code is for mac os x and
zsh
, but will likely work elsewhere with a little tweakingThis resolved an issue for me with continuous autorebuilding by
sphinx-autobuild
due, I assume, to anshutil.copyfile
inconf.py
. Also I tried a few vscode sphinx and rst-preview extensions, and none of them worked, either.