File: jekyll_fix.py

package info (click to toggle)
openslide-python 1.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,184 kB
  • ctags: 787
  • sloc: python: 1,044; makefile: 10
file content (67 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# openslide-python - Python bindings for the OpenSlide library
#
# Copyright (c) 2014 Carnegie Mellon University
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# Sphinx hardcodes that certain output directories have names starting with
# an underscore.
# Jekyll hardcodes that filenames starting with an underscore are not
# deployed to the website.
# Rename Sphinx output directories to drop the underscore.

DIRS = {
    '_static': 'static',
    '_sources': 'sources',
}
REWRITE_EXTENSIONS = set(['.html', '.js'])

import os
from sphinx.util.console import bold

def remove_directory_underscores(app, exception):
    if exception:
        return
    app.info(bold('fixing directory names... '), True)
    # Rewrite references in HTML/JS files
    for dirpath, _, filenames in os.walk(app.outdir):
        for filename in filenames:
            _, ext = os.path.splitext(filename)
            if ext in REWRITE_EXTENSIONS:
                path = os.path.join(dirpath, filename)
                with open(path) as fh:
                    contents = fh.read()
                for old, new in DIRS.items():
                    contents = contents.replace(old + '/', new + '/')
                with open(path, 'w') as fh:
                    fh.write(contents)
    # Move directory contents
    for old, new in DIRS.items():
        olddir = os.path.join(app.outdir, old)
        newdir = os.path.join(app.outdir, new)
        if not os.path.exists(newdir):
            os.mkdir(newdir)
        if os.path.isdir(olddir):
            for filename in os.listdir(olddir):
                oldfile = os.path.join(olddir, filename)
                newfile = os.path.join(newdir, filename)
                os.rename(oldfile, newfile)
            os.rmdir(olddir)
    app.info('done')


def setup(app):
    app.connect('build-finished', remove_directory_underscores)