make_docs.py revision 79397c21138f54fcff6ec067b44b847f1f7e0e98
1#!/usr/bin/python
2# encoding: utf-8
3#
4# Ceres Solver - A fast non-linear least squares minimizer
5# Copyright 2014 Google Inc. All rights reserved.
6# http://code.google.com/p/ceres-solver/
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
36import glob
37import os
38import sys
39
40# Number of arguments
41N = len(sys.argv)
42
43if N < 3:
44  print 'make_docs.py src_root destination_root'
45  sys.exit(1)
46
47src_dir    = sys.argv[1] + '/docs/source'
48build_root = sys.argv[2]
49cache_dir  = build_root + '/doctrees'
50html_dir   = build_root + '/html'
51
52# Called from Command Line
53if N == 3:
54  sphinx_exe = 'sphinx-build'
55
56# Called from CMake (using the SPHINX_EXECUTABLE found)
57elif N == 4:
58  sphinx_exe = sys.argv[3]
59
60# Run Sphinx to build the documentation.
61os.system('%s -b html -d %s %s %s' %(sphinx_exe, cache_dir, src_dir, html_dir))
62
63replacements = [
64  # By default MathJax uses does not use TeX fonts. This simple search
65  # and replace fixes that.
66  ('''config=TeX-AMS-MML_HTMLorMML"></script>''',
67   '''config=TeX-AMS_HTML">
68      MathJax.Hub.Config({
69          "HTML-CSS": {
70            availableFonts: ["TeX"]
71          }
72        });
73      </script>'''),
74
75  # The title for the homepage is not ideal, so change it.
76  ('<title>Ceres Solver &mdash; Ceres Solver</title>',
77   '<title>Ceres Solver &mdash; A Nonlinear Least Squares Minimizer</title>')
78]
79
80# This is a nasty hack to strip the breadcrumb navigation. A better strategy is
81# to fork the upstream template, but that is no fun either. Whitespace matters!
82# This doesn't use regular expressions since the escaping makes it untenable.
83breadcrumb_start_other = \
84'''<div role="navigation" aria-label="breadcrumbs navigation">
85  <ul class="wy-breadcrumbs">
86    <li><a href="index.html">Docs</a> &raquo;</li>
87
88    <li>'''
89
90# The index page has a slightly different breadcrumb.
91breadcrumb_start_index = breadcrumb_start_other.replace('index.html', '#')
92
93breadcrumb_end = \
94'''</li>
95      <li class="wy-breadcrumbs-aside">
96
97      </li>
98  </ul>
99  <hr/>
100</div>'''
101
102for name in glob.glob('%s/*.html' % html_dir):
103  print 'Postprocessing: ', name
104  with open(name) as fptr:
105    out = fptr.read()
106
107  for input_pattern, output_pattern in replacements:
108    out = out.replace(input_pattern, output_pattern)
109
110  try:
111    breadcrumb_start = breadcrumb_start_index \
112                       if name.endswith('index.html') \
113                       else breadcrumb_start_other
114    pre_breadcrumb_start, post_breadcrumb_start = out.split(breadcrumb_start)
115    title, post_breadcrumb_end = post_breadcrumb_start.split(breadcrumb_end)
116    print 'Stripping breadcrumb for -', title
117    out = pre_breadcrumb_start + post_breadcrumb_end
118  except ValueError:
119    print 'Skipping breadcrumb strip for', name
120
121  with open(name, 'w') as fptr:
122    fptr.write(out)
123