Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 2 | # encoding: utf-8 |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 3 | # |
| 4 | # Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 5 | # Copyright 2015 Google Inc. All rights reserved. |
| 6 | # http://ceres-solver.org/ |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 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 | |
Mark Moll | 1c3d85b | 2015-04-10 22:28:06 -0500 | [diff] [blame] | 36 | from __future__ import print_function |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 37 | import glob |
Niels Ole Salscheider | 2ad14b7 | 2016-03-02 21:55:37 +0100 | [diff] [blame] | 38 | import io |
Sameer Agarwal | 931c309 | 2013-02-25 09:46:21 -0800 | [diff] [blame] | 39 | import os |
| 40 | import sys |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 41 | |
Pablo Speciale | c51b11c | 2013-03-12 00:56:56 -0700 | [diff] [blame] | 42 | # Number of arguments |
| 43 | N = len(sys.argv) |
| 44 | |
| 45 | if N < 3: |
Mark Moll | 1c3d85b | 2015-04-10 22:28:06 -0500 | [diff] [blame] | 46 | print('make_docs.py src_root destination_root') |
Sameer Agarwal | beb4505 | 2013-02-22 13:37:01 -0800 | [diff] [blame] | 47 | sys.exit(1) |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 48 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 49 | src_dir = sys.argv[1] + '/docs/source' |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 50 | build_root = sys.argv[2] |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 51 | cache_dir = build_root + '/doctrees' |
| 52 | html_dir = build_root + '/html' |
Pablo Speciale | c51b11c | 2013-03-12 00:56:56 -0700 | [diff] [blame] | 53 | |
| 54 | # Called from Command Line |
| 55 | if N == 3: |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 56 | sphinx_exe = 'sphinx-build' |
Pablo Speciale | c51b11c | 2013-03-12 00:56:56 -0700 | [diff] [blame] | 57 | |
| 58 | # Called from CMake (using the SPHINX_EXECUTABLE found) |
| 59 | elif N == 4: |
| 60 | sphinx_exe = sys.argv[3] |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 61 | |
| 62 | # Run Sphinx to build the documentation. |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 63 | os.system('%s -b html -d %s %s %s' %(sphinx_exe, cache_dir, src_dir, html_dir)) |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 64 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 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>'''), |
Sameer Agarwal | fa21df8 | 2013-02-18 08:48:52 -0800 | [diff] [blame] | 76 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 77 | # The title for the homepage is not ideal, so change it. |
| 78 | ('<title>Ceres Solver — Ceres Solver</title>', |
Sameer Agarwal | d1e954d | 2016-11-21 22:37:46 -0800 | [diff] [blame] | 79 | '<title>Ceres Solver — A Large Scale Non-linear Optimization Library</title>') |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 80 | ] |
Sameer Agarwal | 931c309 | 2013-02-25 09:46:21 -0800 | [diff] [blame] | 81 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 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. |
Keir Mierle | 38787f4 | 2014-05-23 00:37:22 +0000 | [diff] [blame] | 85 | breadcrumb_start_other = \ |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 86 | '''<div role="navigation" aria-label="breadcrumbs navigation"> |
| 87 | <ul class="wy-breadcrumbs"> |
| 88 | <li><a href="index.html">Docs</a> »</li> |
Sameer Agarwal | d1e954d | 2016-11-21 22:37:46 -0800 | [diff] [blame] | 89 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 90 | <li>''' |
| 91 | |
Keir Mierle | 38787f4 | 2014-05-23 00:37:22 +0000 | [diff] [blame] | 92 | # The index page has a slightly different breadcrumb. |
| 93 | breadcrumb_start_index = breadcrumb_start_other.replace('index.html', '#') |
| 94 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 95 | breadcrumb_end = \ |
| 96 | '''</li> |
| 97 | <li class="wy-breadcrumbs-aside"> |
Sameer Agarwal | d1e954d | 2016-11-21 22:37:46 -0800 | [diff] [blame] | 98 | |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 99 | </li> |
| 100 | </ul> |
| 101 | <hr/> |
| 102 | </div>''' |
| 103 | |
| 104 | for name in glob.glob('%s/*.html' % html_dir): |
Mark Moll | 1c3d85b | 2015-04-10 22:28:06 -0500 | [diff] [blame] | 105 | print('Postprocessing: ', name) |
Niels Ole Salscheider | 2ad14b7 | 2016-03-02 21:55:37 +0100 | [diff] [blame] | 106 | with io.open(name, encoding="utf-8") as fptr: |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 107 | out = fptr.read() |
| 108 | |
| 109 | for input_pattern, output_pattern in replacements: |
| 110 | out = out.replace(input_pattern, output_pattern) |
| 111 | |
| 112 | try: |
Keir Mierle | 38787f4 | 2014-05-23 00:37:22 +0000 | [diff] [blame] | 113 | breadcrumb_start = breadcrumb_start_index \ |
| 114 | if name.endswith('index.html') \ |
| 115 | else breadcrumb_start_other |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 116 | pre_breadcrumb_start, post_breadcrumb_start = out.split(breadcrumb_start) |
| 117 | title, post_breadcrumb_end = post_breadcrumb_start.split(breadcrumb_end) |
Mark Moll | 1c3d85b | 2015-04-10 22:28:06 -0500 | [diff] [blame] | 118 | print('Stripping breadcrumb for -', title) |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 119 | out = pre_breadcrumb_start + post_breadcrumb_end |
| 120 | except ValueError: |
Mark Moll | 1c3d85b | 2015-04-10 22:28:06 -0500 | [diff] [blame] | 121 | print('Skipping breadcrumb strip for', name) |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 122 | |
Niels Ole Salscheider | 2ad14b7 | 2016-03-02 21:55:37 +0100 | [diff] [blame] | 123 | with io.open(name, 'w', encoding="utf-8") as fptr: |
Keir Mierle | 556027a | 2014-05-22 23:33:34 +0000 | [diff] [blame] | 124 | fptr.write(out) |