简体   繁体   English

将Sphinx PDF输出附加到Sphinx HTML输出

[英]Attach Sphinx PDF output to Sphinx HTML output

This is a really weird question: 这是一个非常奇怪的问题:

I have been able to generate html and pdf output using Sphinx; 我已经能够使用Sphinx生成html和pdf输出; and I had to bundle both with my distribution (to PyPI) so that both would be accessible to the user. 而且我不得不将两者都捆绑到我的发行版(到PyPI),这样两者都可以被用户访问。

Though I can upload HTML documentation directly to be hosted on PyPI, I am unable to upload the PDF LaTeX version of it as well. 虽然我可以直接上传HTML文档以托管在PyPI上,但我也无法上传它的PDF LaTeX版本。 I want to do this because the actual code is under 50K, but bundling the documentation with it bloats it up to about 300K. 我想这样做是因为实际代码不到50K,但是将文档与它捆绑在一起会使其增加到大约300K。

Ultimately, I would like the user to be able to have an off-line version of the documentation without having to download several pages of sphinx documentation. 最终,我希望用户能够获得文档的离线版本,而无需下载几页sphinx文档。

So my question is this: is it possible for me to automatically bundle the PDF with the HTML so that an end user can directly download the PDF for off-line use? 所以我的问题是:我是否可以自动将PDF与HTML捆绑在一起,以便最终用户可以直接下载PDF以供离线使用? (I realize that I can bundle just the PDF with my distribution, but this seems a cleaner approach) (我意识到我可以将PDF与我的发行版捆绑在一起,但这似乎更简洁)

I went with a modification of Reinout van Rees's solution: 我修改了Reinout van Rees的解决方案:

I created a downloadMe.rst (which has lorem ipsum text within it) which gets automatically built into HTML when make html is run and therefore makes downloadMe.html with the lorem ipsum text. 我创建了一个downloadMe.rst (其中包含lorem ipsum文本),当运行make html时,它会自动构建到HTML中,因此使用lorem ipsum文本生成downloadMe.html

I then edited the Makefile's html target as follows: 然后我按如下方式编辑了Makefile的html目标:

  1. make it build the LaTeX pdf and copy it over into _build/html/static . 让它构建LaTeX pdf并将其复制到_build/html/static
  2. use a sed script to replace the lorem ipsum text in downloadMe.html with an HTML hyperlink to the PDF in _build/html/_static . 使用sed脚本将downloadMe.html的lorem ipsum文本替换为_build/html/_static PDF的HTML超链接。

When all of that was done, this is what the html target in Makefile looks like: 完成所有这些后,这就是Makefile的html目标:

html:
        @echo "Making LaTeX"
        make latex
        ( cd _build/latex/; make ) # the LaTeX needs to be built separately. This can be done in a subshell
        @echo "Done making LaTeX"
        @echo "Copying PDF to Static"
        cp _build/latex/Genetic.pdf _build/html/_static/
        @echo "Copy PDF to Static... DONE"
        @echo "Adding PDF to HTML"
        sed -i '' 's/lorem\ ipsum/\<a href="_static\/Genetic.pdf"\>Download\ Me\<\/a\>/g' _build/html/downloadPDF.html
        @echo "Done adding PDF to HTML"
        @echo "Removing LaTeX dir"
        rm -rf _build/latex
        @echo "Done removing LaTeX dir"

        $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
        @echo
        @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

This makefile target, though a little heavier has the advantage that it's fire-and-forget; 这个makefile目标,虽然稍微重一点,但它的优点在于它的发射功能。 I just have to make html and all the documentation is done in one go and I don't have to edit any files when make is done. 我只需要make html并且所有文档都可以一次完成,而且在make完成后我不必编辑任何文件。 This leaves less room for me to forget to edit some file or other to make the documentation correctly before uploading to PyPI 这使我在上传到PyPI之前忘记编辑某个文件或其他文件以便正确编写文档的空间更小

An alternative (also to my other answer) is to let http://readthedocs.org build and host your documentation. 另一种选择(也是我的另一个答案)是让http://readthedocs.org构建并托管您的文档。 They can also build PDFs, so you can provide a link to the PDF (build and hosted by readthedocs) in your documentation and/or your README.rst . 他们还可以构建PDF,因此您可以在文档和/或README.rst提供指向PDF(由readthedocs构建和托管)的链接。

For an example, look at https://readthedocs.org/projects/zestreleaser/downloads/ . 例如,请查看https://readthedocs.org/projects/zestreleaser/downloads/ You see links there for a PDF, an epub and a zipped html download. 您可以在那里看到PDF,epub和压缩html下载的链接。 Could be exactly what you want. 可能正是你想要的。

You could modify the Makefile that Sphinx placed in your doc/build/ directory. 您可以修改Sphinx放在doc/build/目录中的Makefile At the end of the latexpdf target, add a line to copy the PDF to the html build dir. latexpdf目标的末尾,添加一行以将PDF复制到html构建目录。 Here's an example (I only added the last line): 这是一个例子(我只添加了最后一行):

latexpdf:
    $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
    @echo "Running LaTeX files through pdflatex..."
    $(MAKE) -C $(BUILDDIR)/latex all-pdf
    @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
    cp $(BUILDDIR)/latex/*pdf $(BUILDDIR)/html/

You can then add a link to the PDF in your README.rst . 然后,您可以在README.rst添加指向PDF的链接。

(If the README is included in your Sphinx, this probably gives you a Sphinx warning, though, for a missing target file. Unless you also copy the PDF to your source dir. But then you have the risk of an older version being copied.) (如果您的Sphinx中包含自述文件,这可能会为您提供Sphinx警告,但是对于丢失的目标文件。除非您还将PDF复制到源目录。但是您可能会有复制旧版本的风险。 )

My suggestion: add a "raw html" entry to your README.rst , it also has the advantage of not showing up in the PDF output :-) 我的建议:在README.rst添加一个“原始html”条目,它还具有不显示在PDF输出中的优点:-)

.. raw:: html

    <a href="pypi/link/to/pdf">PDF version</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM