2 # Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in
13 # the documentation and/or other materials provided with the
15 # * Neither the name of Intel Corporation nor the names of its
16 # contributors may be used to endorse or promote products derived
17 # from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 from docutils import nodes
33 from distutils.version import LooseVersion
34 from sphinx import __version__ as sphinx_version
35 from sphinx.highlighting import PygmentsBridge
36 from pygments.formatters.latex import LatexFormatter
40 html_add_permalinks = ""
41 html_show_copyright = False
42 highlight_language = 'none'
44 version = subprocess.check_output(['make', '-sRrC', '../../', 'showversion']).decode('utf-8')
49 # Figures, tables and code-blocks automatically numbered if they have caption
60 # Latex directives to be included directly in the latex/pdf docs.
62 \usepackage[utf8]{inputenc}
63 \usepackage{DejaVuSansMono}
64 \usepackage[T1]{fontenc}
66 \renewcommand{\familydefault}{\sfdefault}
67 \RecustomVerbatimEnvironment{Verbatim}{Verbatim}{xleftmargin=5mm}
70 # Configuration for the latex/pdf docs.
72 'papersize': 'a4paper',
75 'classoptions': ',openany,oneside',
76 'babel': '\\usepackage[english]{babel}',
77 # customize Latex formatting
78 'preamble': latex_preamble
81 # Override the default Latex formatter in order to modify the
82 # code/verbatim blocks.
83 class CustomLatexFormatter(LatexFormatter):
84 def __init__(self, **options):
85 super(CustomLatexFormatter, self).__init__(**options)
86 # Use the second smallest font size for code/verbatim blocks.
87 self.verboptions = r'formatcom=\footnotesize'
89 # Replace the default latex formatter.
90 PygmentsBridge.latex_formatter = CustomLatexFormatter
92 ######## :numref: fallback ########
93 # The following hook functions add some simple handling for the :numref:
94 # directive for Sphinx versions prior to 1.3.1. The functions replace the
95 # :numref: reference with a link to the target (for all Sphinx doc types).
96 # It doesn't try to label figures/tables.
98 def numref_role(reftype, rawtext, text, lineno, inliner):
100 Add a Sphinx role to handle numref references. Note, we can't convert
101 the link here because the doctree isn't build and the target information
104 # Add an identifier to distinguish numref from other references.
105 newnode = nodes.reference('',
107 refuri='_local_numref_#%s' % text,
111 def process_numref(app, doctree, from_docname):
113 Process the numref nodes once the doctree has been built and prior to
114 writing the files. The processing involves replacing the numref with a
115 link plus text to indicate if it is a Figure or Table link.
118 # Iterate over the reference nodes in the doctree.
119 for node in doctree.traverse(nodes.reference):
120 target = node.get('refuri', '')
122 # Look for numref nodes.
123 if target.startswith('_local_numref_#'):
124 target = target.replace('_local_numref_#', '')
126 # Get the target label and link information from the Sphinx env.
127 data = app.builder.env.domains['std'].data
128 docname, label, _ = data['labels'].get(target, ('', '', ''))
129 relative_url = app.builder.get_relative_uri(from_docname, docname)
131 # Add a text label to the link.
132 if target.startswith('figure'):
134 elif target.startswith('table'):
139 # New reference node with the updated link information.
140 newnode = nodes.reference('',
142 refuri='%s#%s' % (relative_url, label),
144 node.replace_self(newnode)
147 if LooseVersion(sphinx_version) < LooseVersion('1.3.1'):
148 print('Upgrade sphinx to version >= 1.3.1 for '
149 'improved Figure/Table number handling.')
150 # Add a role to handle :numref: references.
151 app.add_role('numref', numref_role)
152 # Process the numref references once the doctree has been created.
153 app.connect('doctree-resolved', process_numref)