Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / scripts / make_docs.py
1 #!/usr/bin/python
2 # encoding: utf-8
3 #
4 # Ceres Solver - A fast non-linear least squares minimizer
5 # Copyright 2015 Google Inc. All rights reserved.
6 # http://ceres-solver.org/
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # * Redistributions of source code must retain the above copyright notice,
12 #   this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 #   this list of conditions and the following disclaimer in the documentation
15 #   and/or other materials provided with the distribution.
16 # * Neither the name of Google Inc. nor the names of its contributors may be
17 #   used to endorse or promote products derived from this software without
18 #   specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32 # Author: sameeragarwal@google.com (Sameer Agarwal)
33 #
34 # Note: You will need Sphinx and Pygments installed for this to work.
35
36 from __future__ import print_function
37 import glob
38 import io
39 import os
40 import sys
41
42 # Number of arguments
43 N = len(sys.argv)
44
45 if N < 3:
46   print('make_docs.py src_root destination_root')
47   sys.exit(1)
48
49 src_dir    = sys.argv[1] + '/docs/source'
50 build_root = sys.argv[2]
51 cache_dir  = build_root + '/doctrees'
52 html_dir   = build_root + '/html'
53
54 # Called from Command Line
55 if N == 3:
56   sphinx_exe = 'sphinx-build'
57
58 # Called from CMake (using the SPHINX_EXECUTABLE found)
59 elif N == 4:
60   sphinx_exe = sys.argv[3]
61
62 # Run Sphinx to build the documentation.
63 os.system('%s -b html -d %s %s %s' %(sphinx_exe, cache_dir, src_dir, html_dir))
64
65 replacements = [
66   # By default MathJax uses does not use TeX fonts. This simple search
67   # and replace fixes that.
68   ('''config=TeX-AMS-MML_HTMLorMML"></script>''',
69    '''config=TeX-AMS_HTML">
70       MathJax.Hub.Config({
71           "HTML-CSS": {
72             availableFonts: ["TeX"]
73           }
74         });
75       </script>'''),
76
77   # The title for the homepage is not ideal, so change it.
78   ('<title>Ceres Solver &mdash; Ceres Solver</title>',
79    '<title>Ceres Solver &mdash; A Large Scale Non-linear Optimization Library</title>')
80 ]
81
82 # This is a nasty hack to strip the breadcrumb navigation. A better strategy is
83 # to fork the upstream template, but that is no fun either. Whitespace matters!
84 # This doesn't use regular expressions since the escaping makes it untenable.
85 breadcrumb_start_other = \
86 '''<div role="navigation" aria-label="breadcrumbs navigation">
87   <ul class="wy-breadcrumbs">
88     <li><a href="index.html">Docs</a> &raquo;</li>
89
90     <li>'''
91
92 # The index page has a slightly different breadcrumb.
93 breadcrumb_start_index = breadcrumb_start_other.replace('index.html', '#')
94
95 breadcrumb_end = \
96 '''</li>
97       <li class="wy-breadcrumbs-aside">
98
99       </li>
100   </ul>
101   <hr/>
102 </div>'''
103
104 for name in glob.glob('%s/*.html' % html_dir):
105   print('Postprocessing: ', name)
106   with io.open(name, encoding="utf-8") as fptr:
107     out = fptr.read()
108
109   for input_pattern, output_pattern in replacements:
110     out = out.replace(input_pattern, output_pattern)
111
112   try:
113     breadcrumb_start = breadcrumb_start_index \
114                        if name.endswith('index.html') \
115                        else breadcrumb_start_other
116     pre_breadcrumb_start, post_breadcrumb_start = out.split(breadcrumb_start)
117     title, post_breadcrumb_end = post_breadcrumb_start.split(breadcrumb_end)
118     print('Stripping breadcrumb for -', title)
119     out = pre_breadcrumb_start + post_breadcrumb_end
120   except ValueError:
121     print('Skipping breadcrumb strip for', name)
122
123   with io.open(name, 'w', encoding="utf-8") as fptr:
124     fptr.write(out)