From cc5859fcbf086bcc6a25787dc3520e444f5b7282 Mon Sep 17 00:00:00 2001 From: tk0miya Date: Sat, 27 Sep 2014 10:11:47 +0900 Subject: [PATCH 1/3] Add imgpath property to all builders --- sphinx/builders/__init__.py | 4 ++++ sphinx/builders/epub.py | 6 +++--- sphinx/builders/html.py | 9 +++++---- sphinx/builders/qthelp.py | 2 +- sphinx/builders/websupport.py | 9 ++++++--- sphinx/ext/graphviz.py | 10 ++-------- sphinx/ext/pngmath.py | 2 +- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index d52a7983d..d2c41890d 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -44,6 +44,10 @@ class Builder(object): versioning_method = 'none' # allow parallel write_doc() calls allow_parallel = False + # basename of images directory + imagedir = "" + # relative path to image directory from current docname (used at writing docs) + imgpath = "" def __init__(self, app): self.env = app.env 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 c2c308937..4c7155fd7 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -73,6 +73,7 @@ class StandaloneHTMLBuilder(Builder): link_suffix = '.html' # defaults to matching out_suffix indexer_format = js_index indexer_dumps_unicode = True + imagedir = '_images' supported_image_types = ['image/svg+xml', 'image/png', 'image/gif', 'image/jpeg'] searchindex_filename = 'searchindex.js' @@ -424,7 +425,7 @@ class StandaloneHTMLBuilder(Builder): doctree.settings = self.docsettings self.secnumbers = self.env.toc_secnumbers.get(docname, {}) - self.imgpath = relative_uri(self.get_target_uri(docname), '_images') + self.imgpath = relative_uri(self.get_target_uri(docname), self.imagedir) self.dlpath = relative_uri(self.get_target_uri(docname), '_downloads') self.current_docname = docname self.docwriter.write(doctree, destination) @@ -436,7 +437,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 '' @@ -534,13 +535,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)) 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 7b0e6f724..0a99b5021 100644 --- a/sphinx/builders/websupport.py +++ b/sphinx/builders/websupport.py @@ -58,7 +58,8 @@ class WebSupportBuilder(PickleHTMLBuilder): doctree.settings = self.docsettings self.secnumbers = self.env.toc_secnumbers.get(docname, {}) - self.imgpath = '/' + posixpath.join(self.virtual_staticdir, '_images') + self.imgpath = '/' + posixpath.join(self.virtual_staticdir, self.imagedir) + print self.imgpath self.dlpath = '/' + posixpath.join(self.virtual_staticdir, '_downloads') self.current_docname = docname self.docwriter.write(doctree, destination) @@ -70,7 +71,8 @@ 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) + print self.imgpath self.post_process_images(doctree) title = self.env.longtitles.get(docname) title = title and self.render_partial(title)['title'] or '' @@ -148,7 +150,8 @@ class WebSupportBuilder(PickleHTMLBuilder): PickleHTMLBuilder.handle_finish(self) # move static stuff over to separate directory - directories = ['_images', '_static'] + directories = [self.imagedir, '_static'] + print directories 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 From f746a45dc93f27a91a238e0b0087b0f5d9d6df63 Mon Sep 17 00:00:00 2001 From: tk0miya Date: Sun, 28 Sep 2014 21:25:46 +0900 Subject: [PATCH 2/3] Remove debug print --- sphinx/builders/websupport.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/sphinx/builders/websupport.py b/sphinx/builders/websupport.py index 0a99b5021..a3d311bcc 100644 --- a/sphinx/builders/websupport.py +++ b/sphinx/builders/websupport.py @@ -59,7 +59,6 @@ class WebSupportBuilder(PickleHTMLBuilder): self.secnumbers = self.env.toc_secnumbers.get(docname, {}) self.imgpath = '/' + posixpath.join(self.virtual_staticdir, self.imagedir) - print self.imgpath self.dlpath = '/' + posixpath.join(self.virtual_staticdir, '_downloads') self.current_docname = docname self.docwriter.write(doctree, destination) @@ -72,7 +71,6 @@ class WebSupportBuilder(PickleHTMLBuilder): def write_doc_serialized(self, docname, doctree): self.imgpath = '/' + posixpath.join(self.virtual_staticdir, self.imagedir) - print self.imgpath self.post_process_images(doctree) title = self.env.longtitles.get(docname) title = title and self.render_partial(title)['title'] or '' @@ -151,7 +149,6 @@ class WebSupportBuilder(PickleHTMLBuilder): # move static stuff over to separate directory directories = [self.imagedir, '_static'] - print directories for directory in directories: src = path.join(self.outdir, directory) dst = path.join(self.staticdir, directory) From 8c38327f0e318defffb2dd6b921ca552d36d6ebe Mon Sep 17 00:00:00 2001 From: tk0miya Date: Mon, 29 Sep 2014 00:17:24 +0900 Subject: [PATCH 3/3] Refactored; Initialize imagedir at init() method --- sphinx/builders/__init__.py | 8 ++++---- sphinx/builders/html.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index d2c41890d..ee55830fc 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -44,10 +44,6 @@ class Builder(object): versioning_method = 'none' # allow parallel write_doc() calls allow_parallel = False - # basename of images directory - imagedir = "" - # relative path to image directory from current docname (used at writing docs) - imgpath = "" def __init__(self, app): self.env = app.env @@ -74,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/html.py b/sphinx/builders/html.py index 37d63be9a..d33d2eef4 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -73,7 +73,6 @@ class StandaloneHTMLBuilder(Builder): link_suffix = '.html' # defaults to matching out_suffix indexer_format = js_index indexer_dumps_unicode = True - imagedir = '_images' supported_image_types = ['image/svg+xml', 'image/png', 'image/gif', 'image/jpeg'] searchindex_filename = 'searchindex.js' @@ -96,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 @@ -1030,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()