当源文件更改时自动构建 Sphinx 文档

发布于 2024-10-30 13:02:55 字数 212 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

橘虞初梦 2024-11-06 13:02:55

您可以使用 sphinx-autobuild。它还会自动在浏览器中启动页面刷新。

它很容易使用,例如:

sphinx-autobuild docs docs/_build/html

或者,如果您有一个单独的构建目录,

sphinx-autobuild <doc-source-dir> <doc-build-dir>

其中 是文档的源目录, 是您想要构建 html 文件的目录。

You can use sphinx-autobuild. It will also automatically initiate a page refresh in the browser.

It is easy to use, for example:

sphinx-autobuild docs docs/_build/html

or, if you have a separate build dir,

sphinx-autobuild <doc-source-dir> <doc-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.

看春风乍起 2024-11-06 13:02:55

Jacob Kaplan-Moss 有一个很好的解决方案

pip install watchdog
watchmedo shell-command \
          --patterns="*.rst" \
          --ignore-pattern='_build/*' \
          --recursive \
          --command='make html'

注意,更改模式以匹配您的后缀。 Jacob 使用 *.txt,但我需要将其更改为 *.rst。

Jacob Kaplan-Moss has a good solution:

pip install watchdog
watchmedo shell-command \
          --patterns="*.rst" \
          --ignore-pattern='_build/*' \
          --recursive \
          --command='make html'

Note, change the pattern to match your suffix. Jacob uses *.txt, but I needed to change it to *.rst.

背叛残局 2024-11-06 13:02:55

如果您使用的是 *nix 系统,则可以使用 inotify 来监视文件系统事件并触发操作。

例如,在 ubuntu 上,

apt-get install inotify-tools

然后运行这样的脚本来侦听给定目录中的事件

while true
  do
    inotifywait -r -e modify -e move -e create -e delete /tmp/docs | while read line
      do
        echo "file changed; time to run make html"
      done
  done

If you're on a *nix system, you can use inotify to monitor filesystem events and trigger actions.

For example, on ubuntu,

apt-get install inotify-tools

Then run a script like this to listen for events in a given dir

while true
  do
    inotifywait -r -e modify -e move -e create -e delete /tmp/docs | while read line
      do
        echo "file changed; time to run make html"
      done
  done
夕嗳→ 2024-11-06 13:02:55

您可以在您最喜欢的编辑器中创建一个宏来保存文件并在浏览器中打开它,任何文本编辑器都可以(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++...)

一百个冬季 2024-11-06 13:02:55

重建页面并在 VSCode 中刷新预览

如果使用 vscode,请安装 实时预览,带有 sphinx-buildrestructedText 然后,在 zsh 终端中,从您的文档根目录,使用单个命令,您可以:

  • 重建 (make html),
  • 打开预览 build/html/doc.html
  • 重新打开您正在编辑的文档 code -r source/doc.rst
make html && code -r build/html/doc.html && code -r source/doc.rst
  • make html 将构建文档树
  • code -r 将在两种情况下重用当前窗口

如果每个 doc.html实时预览doc.htmldoc.rst 均已打开,sphinx 将重建树,vscode 将打开刷新的预览,然后返回编辑状态doc.rst

更进一步

,创建一个动态键绑定,它将使用任何打开的 .rst 文件的键盘快捷键来完成上述所有操作。将其添加到您的 keybindings.json 中:

   {
      "key": "cmd+shift+R",
      "command": "workbench.action.terminal.sendSequence",
      "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} && code -r ${VSCODEOPENFILE}\u000D"
      },
      "when": "textInputFocus && !editorReadonly"
    }
  • "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 对于 sourcehtml 对于参数中的 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:

  • rebuild (make html),
  • open a preview build/html/doc.html
  • re-open the document you were editing code -r source/doc.rst
make html && code -r build/html/doc.html && code -r source/doc.rst
  • make html will build the doc tree
  • code -r will reuse the current window in both cases

If each of doc.html, the live preview of doc.html, and doc.rst are all open already, sphinx will rebuild the tree and vscode will open the refreshed preview, and return you to editing doc.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 your keybindings.json:

   {
      "key": "cmd+shift+R",
      "command": "workbench.action.terminal.sendSequence",
      "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} && code -r ${VSCODEOPENFILE}\u000D"
      },
      "when": "textInputFocus && !editorReadonly"
    }
  • "key": "cmd+shift+R" is the keyboard shortcut. Run this when your rst 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)
  • the command in "args": { "text" :
    • VSCODEOPENFILE=${file} will set the env var the currently open file path
    • sphinx-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 the source dir, e.g., workspace/docs/source
      • ${${VSCODEOPENFILE:h}/source/build/} substitutes build for source 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 both build for source and html for rst in the argument (i.e., source/doc.rst will become build/doc.html,) and reopen it in the existing window, forcing the live preview to refresh
      • code -r ${VSCODEOPENFILE} will return you to the original window (e.g., doc.rst,) for editing.
  • when setting will restrict the Command-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 tweaking

This resolved an issue for me with continuous autorebuilding by sphinx-autobuild due, I assume, to an shutil.copyfile in conf.py. Also I tried a few vscode sphinx and rst-preview extensions, and none of them worked, either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文