Debian Codegraph @

doc/faq.rst

raw
1 .. _faq:
2
3 Sphinx FAQ
4 ==========
5
6 This is a list of Frequently Asked Questions about Sphinx. Feel free to
7 suggest new entries!
8
9 How do I...
10 -----------
11
12 ... create PDF files without LaTeX?
13 `rinohtype`_ provides a PDF builder that can be used as a drop-in
14 replacement for the LaTeX builder.
15
16 .. _rinohtype: https://github.com/brechtm/rinohtype
17
18 ... get section numbers?
19 They are automatic in LaTeX output; for HTML, give a ``:numbered:`` option to
20 the :rst:dir:`toctree` directive where you want to start numbering.
21
22 ... customize the look of the built HTML files?
23 Use themes, see :doc:`/usage/theming`.
24
25 ... add global substitutions or includes?
26 Add them in the :confval:`rst_prolog` or :confval:`rst_epilog` config value.
27
28 ... display the whole TOC tree in the sidebar?
29 Use the :data:`toctree` callable in a custom layout template, probably in the
30 ``sidebartoc`` block.
31
32 ... write my own extension?
33 See the :ref:`extension-tutorials-index`.
34
35 ... convert from my existing docs using MoinMoin markup?
36 The easiest way is to convert to xhtml, then convert `xhtml to reStructuredText`_.
37 You'll still need to mark up classes and such, but the headings and code
38 examples come through cleanly.
39
40 For many more extensions and other contributed stuff, see the sphinx-contrib_
41 repository.
42
43 .. _sphinx-contrib: https://github.com/sphinx-contrib/
44
45 .. _usingwith:
46
47 Using Sphinx with...
48 --------------------
49
50 Read the Docs
51 `Read the Docs <https://readthedocs.org>`_ is a documentation hosting
52 service based around Sphinx. They will host sphinx documentation, along
53 with supporting a number of other features including version support, PDF
54 generation, and more. The `Getting Started`_ guide is a good place to start.
55
56 Epydoc
57 There's a third-party extension providing an `api role`_ which refers to
58 Epydoc's API docs for a given identifier.
59
60 Doxygen
61 Michael Jones has developed a reStructuredText/Sphinx bridge to doxygen
62 called `breathe <https://github.com/breathe-doc/breathe/tree/master>`_.
63
64 SCons
65 Glenn Hutchings has written a SCons build script to build Sphinx
66 documentation; it is hosted here:
67 https://bitbucket-archive.softwareheritage.org/projects/zo/zondo/sphinx-scons.html
68
69 PyPI
70 Jannis Leidel wrote a `setuptools command
71 <https://pypi.org/project/Sphinx-PyPI-upload/>`_ that automatically
72 uploads Sphinx documentation to the PyPI package documentation area at
73 https://pythonhosted.org/.
74
75 GitHub Pages
76 Please add :py:mod:`sphinx.ext.githubpages` to your project. It allows you
77 to publish your document in GitHub Pages. It generates helper files for
78 GitHub Pages on building HTML document automatically.
79
80 MediaWiki
81 See `sphinx-wiki`_, a project by Kevin Dunn.
82
83 .. _sphinx-wiki: https://bitbucket-archive.softwareheritage.org/projects/ke/kevindunn/sphinx-wiki.html
84
85 Google Analytics
86 You can use a custom ``layout.html`` template, like this:
87
88 .. code-block:: html+jinja
89
90 {% extends "!layout.html" %}
91
92 {%- block extrahead %}
93 {{ super() }}
94 <script>
95 var _gaq = _gaq || [];
96 _gaq.push(['_setAccount', 'XXX account number XXX']);
97 _gaq.push(['_trackPageview']);
98 </script>
99 {% endblock %}
100
101 {% block footer %}
102 {{ super() }}
103 <div class="footer">This page uses <a href="https://analytics.google.com/">
104 Google Analytics</a> to collect statistics. You can disable it by blocking
105 the JavaScript coming from www.google-analytics.com.
106 <script>
107 (function() {
108 var ga = document.createElement('script');
109 ga.src = ('https:' == document.location.protocol ?
110 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
111 ga.setAttribute('async', 'true');
112 document.documentElement.firstChild.appendChild(ga);
113 })();
114 </script>
115 </div>
116 {% endblock %}
117
118
119 Google Search
120 To replace Sphinx's built-in search function with Google Search, proceed as
121 follows:
122
123 1. Go to https://cse.google.com/cse/all to create the Google Search code
124 snippet.
125
126 2. Copy the code snippet and paste it into ``_templates/searchbox.html`` in
127 your Sphinx project:
128
129 .. code-block:: html+jinja
130
131 <div>
132 <h3>{{ _('Quick search') }}</h3>
133 <script>
134 (function() {
135 var cx = '......';
136 var gcse = document.createElement('script');
137 gcse.async = true;
138 gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
139 var s = document.getElementsByTagName('script')[0];
140 s.parentNode.insertBefore(gcse, s);
141 })();
142 </script>
143 <gcse:search></gcse:search>
144 </div>
145
146 3. Add ``searchbox.html`` to the :confval:`html_sidebars` configuration value.
147
148 .. _Getting Started: https://docs.readthedocs.io/en/stable/intro/getting-started-with-sphinx.html
149 .. _api role: https://git.savannah.gnu.org/cgit/kenozooid.git/tree/doc/extapi.py
150 .. _xhtml to reStructuredText: https://docutils.sourceforge.io/sandbox/xhtml2rest/xhtml2rest.py
151
152
153 Sphinx vs. Docutils
154 -------------------
155
156 tl;dr: *docutils* converts reStructuredText to multiple output formats. Sphinx
157 builds upon docutils to allow construction of cross-referenced and indexed
158 bodies of documentation.
159
160 `docutils`__ is a text processing system for converting plain text
161 documentation into other, richer formats. As noted in the `docutils
162 documentation`__, docutils uses *readers* to read a document, *parsers* for
163 parsing plain text formats into an internal tree representation made up of
164 different types of *nodes*, and *writers* to output this tree in various
165 document formats. docutils provides parsers for one plain text format -
166 `reStructuredText`__ - though other, *out-of-tree* parsers have been
167 implemented including Sphinx's :doc:`Markdown parser </usage/markdown>`. On the
168 other hand, it provides writers for many different formats including HTML,
169 LaTeX, man pages, Open Document Format and XML.
170
171 docutils exposes all of its functionality through a variety of `front-end
172 tools`__, such as ``rst2html``, ``rst2odt`` and ``rst2xml``. Crucially though,
173 all of these tools, and docutils itself, are concerned with individual
174 documents. They don't support concepts such as cross-referencing, indexing of
175 documents, or the construction of a document hierarchy (typically manifesting
176 in a table of contents).
177
178 Sphinx builds upon docutils by harnessing docutils' readers and parsers and
179 providing its own :doc:`/usage/builders/index`. As a result, Sphinx wraps some
180 of the *writers* provided by docutils. This allows Sphinx to provide many
181 features that would simply not be possible with docutils, such as those
182 outlined above.
183
184 __ https://docutils.sourceforge.io/
185 __ https://docutils.sourceforge.io/docs/dev/hacking.html
186 __ https://docutils.sourceforge.io/rst.html
187 __ https://docutils.sourceforge.io/docs/user/tools.html
188
189
190 .. _epub-faq:
191
192 Epub info
193 ---------
194
195 The following list gives some hints for the creation of epub files:
196
197 * Split the text into several files. The longer the individual HTML files are,
198 the longer it takes the ebook reader to render them. In extreme cases, the
199 rendering can take up to one minute.
200
201 * Try to minimize the markup. This also pays in rendering time.
202
203 * For some readers you can use embedded or external fonts using the CSS
204 ``@font-face`` directive. This is *extremely* useful for code listings which
205 are often cut at the right margin. The default Courier font (or variant) is
206 quite wide and you can only display up to 60 characters on a line. If you
207 replace it with a narrower font, you can get more characters on a line. You
208 may even use `FontForge <https://fontforge.github.io/>`_ and create
209 narrow variants of some free font. In my case I get up to 70 characters on a
210 line.
211
212 You may have to experiment a little until you get reasonable results.
213
214 * Test the created epubs. You can use several alternatives. The ones I am aware
215 of are Epubcheck_, Calibre_, FBreader_ (although it does not render the CSS),
216 and Bookworm_. For Bookworm, you can download the source from
217 https://code.google.com/archive/p/threepress and run your own local server.
218
219 * Large floating divs are not displayed properly.
220 If they cover more than one page, the div is only shown on the first page.
221 In that case you can copy the :file:`epub.css` from the
222 ``sphinx/themes/epub/static/`` directory to your local ``_static/``
223 directory and remove the float settings.
224
225 * Files that are inserted outside of the ``toctree`` directive must be manually
226 included. This sometimes applies to appendixes, e.g. the glossary or
227 the indices. You can add them with the :confval:`epub_post_files` option.
228
229 * The handling of the epub cover page differs from the reStructuredText
230 procedure which automatically resolves image paths and puts the images
231 into the ``_images`` directory. For the epub cover page put the image in the
232 :confval:`html_static_path` directory and reference it with its full path in
233 the :confval:`epub_cover` config option.
234
235 * kindlegen_ command can convert from epub3 resulting file to ``.mobi`` file
236 for Kindle. You can get ``yourdoc.mobi`` under ``_build/epub`` after the
237 following command:
238
239 .. code-block:: console
240
241 $ make epub
242 $ kindlegen _build/epub/yourdoc.epub
243
244 The kindlegen command doesn't accept documents that have section
245 titles surrounding ``toctree`` directive:
246
247 .. code-block:: rst
248
249 Section Title
250 =============
251
252 .. toctree::
253
254 subdocument
255
256 Section After Toc Tree
257 ======================
258
259 kindlegen assumes all documents order in line, but the resulting document
260 has complicated order for kindlegen::
261
262 ``parent.xhtml`` -> ``child.xhtml`` -> ``parent.xhtml``
263
264 If you get the following error, fix your document structure:
265
266 .. code-block:: none
267
268 Error(prcgen):E24011: TOC section scope is not included in the parent chapter:(title)
269 Error(prcgen):E24001: The table of content could not be built.
270
271 .. _Epubcheck: https://github.com/IDPF/epubcheck
272 .. _Calibre: https://calibre-ebook.com/
273 .. _FBreader: https://fbreader.org/
274 .. _Bookworm: https://www.oreilly.com/bookworm/index.html
275 .. _kindlegen: https://www.amazon.com/gp/feature.html?docId=1000765211
276
277 .. _texinfo-faq:
278
279 Texinfo info
280 ------------
281
282 There are two main programs for reading Info files, ``info`` and GNU Emacs. The
283 ``info`` program has less features but is available in most Unix environments
284 and can be quickly accessed from the terminal. Emacs provides better font and
285 color display and supports extensive customization (of course).
286
287 .. _texinfo-links:
288
289 Displaying Links
290 ~~~~~~~~~~~~~~~~
291
292 One noticeable problem you may encounter with the generated Info files is how
293 references are displayed. If you read the source of an Info file, a reference
294 to this section would look like::
295
296 * note Displaying Links: target-id
297
298 In the stand-alone reader, ``info``, references are displayed just as they
299 appear in the source. Emacs, on the other-hand, will by default replace
300 ``*note:`` with ``see`` and hide the ``target-id``. For example:
301
302 :ref:`texinfo-links`
303
304 One can disable generation of the inline references in a document
305 with :confval:`texinfo_cross_references`. That makes
306 an info file more readable with stand-alone reader (``info``).
307
308 The exact behavior of how Emacs displays references is dependent on the variable
309 ``Info-hide-note-references``. If set to the value of ``hide``, Emacs will hide
310 both the ``*note:`` part and the ``target-id``. This is generally the best way
311 to view Sphinx-based documents since they often make frequent use of links and
312 do not take this limitation into account. However, changing this variable
313 affects how all Info documents are displayed and most do take this behavior
314 into account.
315
316 If you want Emacs to display Info files produced by Sphinx using the value
317 ``hide`` for ``Info-hide-note-references`` and the default value for all other
318 Info files, try adding the following Emacs Lisp code to your start-up file,
319 ``~/.emacs.d/init.el``.
320
321 .. code-block:: elisp
322
323 (defadvice info-insert-file-contents (after
324 sphinx-info-insert-file-contents
325 activate)
326 "Hack to make `Info-hide-note-references' buffer-local and
327 automatically set to `hide' iff it can be determined that this file
328 was created from a Texinfo file generated by Docutils or Sphinx."
329 (set (make-local-variable 'Info-hide-note-references)
330 (default-value 'Info-hide-note-references))
331 (save-excursion
332 (save-restriction
333 (widen) (goto-char (point-min))
334 (when (re-search-forward
335 "^Generated by \\(Sphinx\\|Docutils\\)"
336 (save-excursion (search-forward "\x1f" nil t)) t)
337 (set (make-local-variable 'Info-hide-note-references)
338 'hide)))))
339
340
341 Notes
342 ~~~~~
343
344 The following notes may be helpful if you want to create Texinfo files:
345
346 - Each section corresponds to a different ``node`` in the Info file.
347
348 - Colons (``:``) cannot be properly escaped in menu entries and xrefs.
349 They will be replaced with semicolons (``;``).
350
351 - Links to external Info files can be created using the somewhat official URI
352 scheme ``info``. For example::
353
354 info:Texinfo#makeinfo_options