diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index d52a7983d..ee55830fc 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -70,6 +70,10 @@ class Builder(object): # images that need to be copied over (source -> dest) self.images = {} + # basename of images directory + imagedir = "" + # relative path to image directory from current docname (used at writing docs) + imgpath = "" # these get set later self.parallel_ok = False diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 5f9f66431..848dfaa43 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -404,7 +404,7 @@ class EpubBuilder(StandaloneHTMLBuilder): The method tries to read and write the files with the PIL, converting the format and resizing the image if necessary/possible. """ - ensuredir(path.join(self.outdir, '_images')) + ensuredir(path.join(self.outdir, self.imagedir)) for src in self.app.status_iterator(self.images, 'copying images... ', brown, len(self.images)): dest = self.images[src] @@ -416,7 +416,7 @@ class EpubBuilder(StandaloneHTMLBuilder): (path.join(self.srcdir, src), )) try: copyfile(path.join(self.srcdir, src), - path.join(self.outdir, '_images', dest)) + path.join(self.outdir, self.imagedir, dest)) except (IOError, OSError) as err: self.warn('cannot copy image file %r: %s' % (path.join(self.srcdir, src), err)) @@ -432,7 +432,7 @@ class EpubBuilder(StandaloneHTMLBuilder): nh = (height * nw) / width img = img.resize((nw, nh), Image.BICUBIC) try: - img.save(path.join(self.outdir, '_images', dest)) + img.save(path.join(self.outdir, self.imagedir, dest)) except (IOError, OSError) as err: self.warn('cannot write image file %r: %s' % (path.join(self.srcdir, src), err)) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index d4bbefe49..d33d2eef4 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -95,6 +95,8 @@ class StandaloneHTMLBuilder(Builder): # a hash of all config values that, if changed, cause a full rebuild self.config_hash = '' self.tags_hash = '' + # basename of images directory + self.imagedir = '_images' # section numbers for headings in the currently visited document self.secnumbers = {} # currently written docname @@ -437,7 +439,7 @@ class StandaloneHTMLBuilder(Builder): self.handle_page(docname, ctx, event_arg=doctree) def write_doc_serialized(self, docname, doctree): - self.imgpath = relative_uri(self.get_target_uri(docname), '_images') + self.imgpath = relative_uri(self.get_target_uri(docname), self.imagedir) self.post_process_images(doctree) title = self.env.longtitles.get(docname) title = title and self.render_partial(title)['title'] or '' @@ -535,13 +537,13 @@ class StandaloneHTMLBuilder(Builder): def copy_image_files(self): # copy image files if self.images: - ensuredir(path.join(self.outdir, '_images')) + ensuredir(path.join(self.outdir, self.imagedir)) for src in self.app.status_iterator(self.images, 'copying images... ', brown, len(self.images)): dest = self.images[src] try: copyfile(path.join(self.srcdir, src), - path.join(self.outdir, '_images', dest)) + path.join(self.outdir, self.imagedir, dest)) except Exception as err: self.warn('cannot copy image file %r: %s' % (path.join(self.srcdir, src), err)) @@ -1029,6 +1031,7 @@ class SerializingHTMLBuilder(StandaloneHTMLBuilder): def init(self): self.config_hash = '' self.tags_hash = '' + self.imagedir = '_images' self.theme = None # no theme necessary self.templates = None # no template bridge necessary self.init_translator_class() diff --git a/sphinx/builders/qthelp.py b/sphinx/builders/qthelp.py index c0fff2a6a..8e2558e72 100644 --- a/sphinx/builders/qthelp.py +++ b/sphinx/builders/qthelp.py @@ -157,7 +157,7 @@ class QtHelpBuilder(StandaloneHTMLBuilder): olen = len(outdir) projectfiles = [] staticdir = path.join(outdir, '_static') - imagesdir = path.join(outdir, '_images') + imagesdir = path.join(outdir, self.imagedir) for root, dirs, files in os.walk(outdir): resourcedir = root.startswith(staticdir) or \ root.startswith(imagesdir) diff --git a/sphinx/builders/websupport.py b/sphinx/builders/websupport.py index ed913945c..619ef6fe0 100644 --- a/sphinx/builders/websupport.py +++ b/sphinx/builders/websupport.py @@ -59,7 +59,7 @@ class WebSupportBuilder(PickleHTMLBuilder): self.secnumbers = self.env.toc_secnumbers.get(docname, {}) self.fignumbers = self.env.toc_fignumbers.get(docname, {}) - self.imgpath = '/' + posixpath.join(self.virtual_staticdir, '_images') + self.imgpath = '/' + posixpath.join(self.virtual_staticdir, self.imagedir) self.dlpath = '/' + posixpath.join(self.virtual_staticdir, '_downloads') self.current_docname = docname self.docwriter.write(doctree, destination) @@ -71,7 +71,7 @@ class WebSupportBuilder(PickleHTMLBuilder): self.handle_page(docname, ctx, event_arg=doctree) def write_doc_serialized(self, docname, doctree): - self.imgpath = '/' + posixpath.join(self.virtual_staticdir, '_images') + self.imgpath = '/' + posixpath.join(self.virtual_staticdir, self.imagedir) self.post_process_images(doctree) title = self.env.longtitles.get(docname) title = title and self.render_partial(title)['title'] or '' @@ -149,7 +149,7 @@ class WebSupportBuilder(PickleHTMLBuilder): PickleHTMLBuilder.handle_finish(self) # move static stuff over to separate directory - directories = ['_images', '_static'] + directories = [self.imagedir, '_static'] for directory in directories: src = path.join(self.outdir, directory) dst = path.join(self.staticdir, directory) diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index 56831c648..71e7ba651 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -146,14 +146,8 @@ def render_dot(self, code, options, format, prefix='graphviz'): ).encode('utf-8') fname = '%s-%s.%s' % (prefix, sha1(hashkey).hexdigest(), format) - if hasattr(self.builder, 'imgpath'): - # HTML - relfn = posixpath.join(self.builder.imgpath, fname) - outfn = path.join(self.builder.outdir, '_images', fname) - else: - # LaTeX - relfn = fname - outfn = path.join(self.builder.outdir, fname) + relfn = posixpath.join(self.builder.imgpath, fname) + outfn = path.join(self.builder.outdir, self.builder.imagedir, fname) if path.isfile(outfn): return relfn, outfn diff --git a/sphinx/ext/pngmath.py b/sphinx/ext/pngmath.py index 27d7978fe..81a6f9d57 100644 --- a/sphinx/ext/pngmath.py +++ b/sphinx/ext/pngmath.py @@ -86,7 +86,7 @@ def render_math(self, math): shasum = "%s.png" % sha1(latex.encode('utf-8')).hexdigest() relfn = posixpath.join(self.builder.imgpath, 'math', shasum) - outfn = path.join(self.builder.outdir, '_images', 'math', shasum) + outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth