|
| 1 | +# |
| 2 | +# openslide-python - Python bindings for the OpenSlide library |
| 3 | +# |
| 4 | +# Copyright (c) 2014 Carnegie Mellon University |
| 5 | +# |
| 6 | +# This library is free software; you can redistribute it and/or modify it |
| 7 | +# under the terms of version 2.1 of the GNU Lesser General Public License |
| 8 | +# as published by the Free Software Foundation. |
| 9 | +# |
| 10 | +# This library is distributed in the hope that it will be useful, but |
| 11 | +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 | +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 13 | +# License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with this library; if not, write to the Free Software Foundation, |
| 17 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +# |
| 19 | + |
| 20 | +# Sphinx hardcodes that certain output directories have names starting with |
| 21 | +# an underscore. |
| 22 | +# Jekyll hardcodes that filenames starting with an underscore are not |
| 23 | +# deployed to the website. |
| 24 | +# Rename Sphinx output directories to drop the underscore. |
| 25 | + |
| 26 | +DIRS = { |
| 27 | + '_static': 'static', |
| 28 | + '_sources': 'sources', |
| 29 | +} |
| 30 | +REWRITE_EXTENSIONS = set(['.html', '.js']) |
| 31 | + |
| 32 | +import os |
| 33 | +from sphinx.util.console import bold |
| 34 | + |
| 35 | +def remove_directory_underscores(app, exception): |
| 36 | + if exception: |
| 37 | + return |
| 38 | + app.info(bold('fixing directory names... '), True) |
| 39 | + # Rewrite references in HTML/JS files |
| 40 | + for dirpath, _, filenames in os.walk(app.outdir): |
| 41 | + for filename in filenames: |
| 42 | + _, ext = os.path.splitext(filename) |
| 43 | + if ext in REWRITE_EXTENSIONS: |
| 44 | + path = os.path.join(dirpath, filename) |
| 45 | + with open(path) as fh: |
| 46 | + contents = fh.read() |
| 47 | + for old, new in DIRS.items(): |
| 48 | + contents = contents.replace(old + '/', new + '/') |
| 49 | + with open(path, 'w') as fh: |
| 50 | + fh.write(contents) |
| 51 | + # Move directory contents |
| 52 | + for old, new in DIRS.items(): |
| 53 | + olddir = os.path.join(app.outdir, old) |
| 54 | + newdir = os.path.join(app.outdir, new) |
| 55 | + if not os.path.exists(newdir): |
| 56 | + os.mkdir(newdir) |
| 57 | + if os.path.isdir(olddir): |
| 58 | + for filename in os.listdir(olddir): |
| 59 | + oldfile = os.path.join(olddir, filename) |
| 60 | + newfile = os.path.join(newdir, filename) |
| 61 | + os.rename(oldfile, newfile) |
| 62 | + os.rmdir(olddir) |
| 63 | + app.info('done') |
| 64 | + |
| 65 | + |
| 66 | +def setup(app): |
| 67 | + app.connect('build-finished', remove_directory_underscores) |
0 commit comments