blob: c1e711f2025e9ca9aeff02a0ff338c6dbe06b825 [file] [log] [blame]
Sameer Agarwalfa21df82013-02-18 08:48:52 -08001#!/usr/bin/python
Keir Mierle556027a2014-05-22 23:33:34 +00002# encoding: utf-8
Sameer Agarwalfa21df82013-02-18 08:48:52 -08003#
4# Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07005# Copyright 2015 Google Inc. All rights reserved.
6# http://ceres-solver.org/
Sameer Agarwalfa21df82013-02-18 08:48:52 -08007#
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
Mark Moll1c3d85b2015-04-10 22:28:06 -050036from __future__ import print_function
Sameer Agarwalfa21df82013-02-18 08:48:52 -080037import glob
Niels Ole Salscheider2ad14b72016-03-02 21:55:37 +010038import io
Sameer Agarwal931c3092013-02-25 09:46:21 -080039import os
40import sys
Sameer Agarwalfa21df82013-02-18 08:48:52 -080041
Pablo Specialec51b11c2013-03-12 00:56:56 -070042# Number of arguments
43N = len(sys.argv)
44
45if N < 3:
Mark Moll1c3d85b2015-04-10 22:28:06 -050046 print('make_docs.py src_root destination_root')
Sameer Agarwalbeb45052013-02-22 13:37:01 -080047 sys.exit(1)
Sameer Agarwalfa21df82013-02-18 08:48:52 -080048
Keir Mierle556027a2014-05-22 23:33:34 +000049src_dir = sys.argv[1] + '/docs/source'
Sameer Agarwalfa21df82013-02-18 08:48:52 -080050build_root = sys.argv[2]
Keir Mierle556027a2014-05-22 23:33:34 +000051cache_dir = build_root + '/doctrees'
52html_dir = build_root + '/html'
Pablo Specialec51b11c2013-03-12 00:56:56 -070053
54# Called from Command Line
55if N == 3:
Keir Mierle556027a2014-05-22 23:33:34 +000056 sphinx_exe = 'sphinx-build'
Pablo Specialec51b11c2013-03-12 00:56:56 -070057
58# Called from CMake (using the SPHINX_EXECUTABLE found)
59elif N == 4:
60 sphinx_exe = sys.argv[3]
Sameer Agarwalfa21df82013-02-18 08:48:52 -080061
62# Run Sphinx to build the documentation.
Keir Mierle556027a2014-05-22 23:33:34 +000063os.system('%s -b html -d %s %s %s' %(sphinx_exe, cache_dir, src_dir, html_dir))
Sameer Agarwalfa21df82013-02-18 08:48:52 -080064
Keir Mierle556027a2014-05-22 23:33:34 +000065replacements = [
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>'''),
Sameer Agarwalfa21df82013-02-18 08:48:52 -080076
Keir Mierle556027a2014-05-22 23:33:34 +000077 # The title for the homepage is not ideal, so change it.
78 ('<title>Ceres Solver &mdash; Ceres Solver</title>',
Sameer Agarwald1e954d2016-11-21 22:37:46 -080079 '<title>Ceres Solver &mdash; A Large Scale Non-linear Optimization Library</title>')
Keir Mierle556027a2014-05-22 23:33:34 +000080]
Sameer Agarwal931c3092013-02-25 09:46:21 -080081
Keir Mierle556027a2014-05-22 23:33:34 +000082# 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.
Keir Mierle38787f42014-05-23 00:37:22 +000085breadcrumb_start_other = \
Keir Mierle556027a2014-05-22 23:33:34 +000086'''<div role="navigation" aria-label="breadcrumbs navigation">
87 <ul class="wy-breadcrumbs">
88 <li><a href="index.html">Docs</a> &raquo;</li>
Sameer Agarwald1e954d2016-11-21 22:37:46 -080089
Keir Mierle556027a2014-05-22 23:33:34 +000090 <li>'''
91
Keir Mierle38787f42014-05-23 00:37:22 +000092# The index page has a slightly different breadcrumb.
93breadcrumb_start_index = breadcrumb_start_other.replace('index.html', '#')
94
Keir Mierle556027a2014-05-22 23:33:34 +000095breadcrumb_end = \
96'''</li>
97 <li class="wy-breadcrumbs-aside">
Sameer Agarwald1e954d2016-11-21 22:37:46 -080098
Keir Mierle556027a2014-05-22 23:33:34 +000099 </li>
100 </ul>
101 <hr/>
102</div>'''
103
104for name in glob.glob('%s/*.html' % html_dir):
Mark Moll1c3d85b2015-04-10 22:28:06 -0500105 print('Postprocessing: ', name)
Niels Ole Salscheider2ad14b72016-03-02 21:55:37 +0100106 with io.open(name, encoding="utf-8") as fptr:
Keir Mierle556027a2014-05-22 23:33:34 +0000107 out = fptr.read()
108
109 for input_pattern, output_pattern in replacements:
110 out = out.replace(input_pattern, output_pattern)
111
112 try:
Keir Mierle38787f42014-05-23 00:37:22 +0000113 breadcrumb_start = breadcrumb_start_index \
114 if name.endswith('index.html') \
115 else breadcrumb_start_other
Keir Mierle556027a2014-05-22 23:33:34 +0000116 pre_breadcrumb_start, post_breadcrumb_start = out.split(breadcrumb_start)
117 title, post_breadcrumb_end = post_breadcrumb_start.split(breadcrumb_end)
Mark Moll1c3d85b2015-04-10 22:28:06 -0500118 print('Stripping breadcrumb for -', title)
Keir Mierle556027a2014-05-22 23:33:34 +0000119 out = pre_breadcrumb_start + post_breadcrumb_end
120 except ValueError:
Mark Moll1c3d85b2015-04-10 22:28:06 -0500121 print('Skipping breadcrumb strip for', name)
Keir Mierle556027a2014-05-22 23:33:34 +0000122
Niels Ole Salscheider2ad14b72016-03-02 21:55:37 +0100123 with io.open(name, 'w', encoding="utf-8") as fptr:
Keir Mierle556027a2014-05-22 23:33:34 +0000124 fptr.write(out)