Skip to content

Commit b6e7d06

Browse files
committed
Drop underscores from Sphinx output dirs for Jekyll compatibility
1 parent 1998cc7 commit b6e7d06

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

doc/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# add these directories to sys.path here. If the directory is relative to the
1818
# documentation root, use os.path.abspath to make it absolute, like shown here.
1919
sys.path.insert(0, os.path.abspath('../openslide'))
20+
sys.path.insert(0, os.path.abspath('.'))
2021

2122
# -- General configuration -----------------------------------------------------
2223

@@ -28,6 +29,7 @@
2829
extensions = [
2930
'sphinx.ext.coverage',
3031
'sphinx.ext.intersphinx',
32+
'jekyll_fix',
3133
]
3234

3335
# Add any paths that contain templates here, relative to this directory.

doc/jekyll_fix.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)