Add imgpath property to all builders

This commit is contained in:
tk0miya 2014-09-27 10:11:47 +09:00
parent 7984f81a5e
commit cc5859fcbf
7 changed files with 22 additions and 20 deletions

View File

@ -44,6 +44,10 @@ class Builder(object):
versioning_method = 'none' versioning_method = 'none'
# allow parallel write_doc() calls # allow parallel write_doc() calls
allow_parallel = False 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): def __init__(self, app):
self.env = app.env self.env = app.env

View File

@ -404,7 +404,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
The method tries to read and write the files with the PIL, The method tries to read and write the files with the PIL,
converting the format and resizing the image if necessary/possible. 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... ', for src in self.app.status_iterator(self.images, 'copying images... ',
brown, len(self.images)): brown, len(self.images)):
dest = self.images[src] dest = self.images[src]
@ -416,7 +416,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
(path.join(self.srcdir, src), )) (path.join(self.srcdir, src), ))
try: try:
copyfile(path.join(self.srcdir, src), copyfile(path.join(self.srcdir, src),
path.join(self.outdir, '_images', dest)) path.join(self.outdir, self.imagedir, dest))
except (IOError, OSError) as err: except (IOError, OSError) as err:
self.warn('cannot copy image file %r: %s' % self.warn('cannot copy image file %r: %s' %
(path.join(self.srcdir, src), err)) (path.join(self.srcdir, src), err))
@ -432,7 +432,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
nh = (height * nw) / width nh = (height * nw) / width
img = img.resize((nw, nh), Image.BICUBIC) img = img.resize((nw, nh), Image.BICUBIC)
try: try:
img.save(path.join(self.outdir, '_images', dest)) img.save(path.join(self.outdir, self.imagedir, dest))
except (IOError, OSError) as err: except (IOError, OSError) as err:
self.warn('cannot write image file %r: %s' % self.warn('cannot write image file %r: %s' %
(path.join(self.srcdir, src), err)) (path.join(self.srcdir, src), err))

View File

@ -73,6 +73,7 @@ class StandaloneHTMLBuilder(Builder):
link_suffix = '.html' # defaults to matching out_suffix link_suffix = '.html' # defaults to matching out_suffix
indexer_format = js_index indexer_format = js_index
indexer_dumps_unicode = True indexer_dumps_unicode = True
imagedir = '_images'
supported_image_types = ['image/svg+xml', 'image/png', supported_image_types = ['image/svg+xml', 'image/png',
'image/gif', 'image/jpeg'] 'image/gif', 'image/jpeg']
searchindex_filename = 'searchindex.js' searchindex_filename = 'searchindex.js'
@ -424,7 +425,7 @@ class StandaloneHTMLBuilder(Builder):
doctree.settings = self.docsettings doctree.settings = self.docsettings
self.secnumbers = self.env.toc_secnumbers.get(docname, {}) 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.dlpath = relative_uri(self.get_target_uri(docname), '_downloads')
self.current_docname = docname self.current_docname = docname
self.docwriter.write(doctree, destination) self.docwriter.write(doctree, destination)
@ -436,7 +437,7 @@ class StandaloneHTMLBuilder(Builder):
self.handle_page(docname, ctx, event_arg=doctree) self.handle_page(docname, ctx, event_arg=doctree)
def write_doc_serialized(self, docname, 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) self.post_process_images(doctree)
title = self.env.longtitles.get(docname) title = self.env.longtitles.get(docname)
title = title and self.render_partial(title)['title'] or '' title = title and self.render_partial(title)['title'] or ''
@ -534,13 +535,13 @@ class StandaloneHTMLBuilder(Builder):
def copy_image_files(self): def copy_image_files(self):
# copy image files # copy image files
if self.images: 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... ', for src in self.app.status_iterator(self.images, 'copying images... ',
brown, len(self.images)): brown, len(self.images)):
dest = self.images[src] dest = self.images[src]
try: try:
copyfile(path.join(self.srcdir, src), copyfile(path.join(self.srcdir, src),
path.join(self.outdir, '_images', dest)) path.join(self.outdir, self.imagedir, dest))
except Exception as err: except Exception as err:
self.warn('cannot copy image file %r: %s' % self.warn('cannot copy image file %r: %s' %
(path.join(self.srcdir, src), err)) (path.join(self.srcdir, src), err))

View File

@ -157,7 +157,7 @@ class QtHelpBuilder(StandaloneHTMLBuilder):
olen = len(outdir) olen = len(outdir)
projectfiles = [] projectfiles = []
staticdir = path.join(outdir, '_static') staticdir = path.join(outdir, '_static')
imagesdir = path.join(outdir, '_images') imagesdir = path.join(outdir, self.imagedir)
for root, dirs, files in os.walk(outdir): for root, dirs, files in os.walk(outdir):
resourcedir = root.startswith(staticdir) or \ resourcedir = root.startswith(staticdir) or \
root.startswith(imagesdir) root.startswith(imagesdir)

View File

@ -58,7 +58,8 @@ class WebSupportBuilder(PickleHTMLBuilder):
doctree.settings = self.docsettings doctree.settings = self.docsettings
self.secnumbers = self.env.toc_secnumbers.get(docname, {}) 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.dlpath = '/' + posixpath.join(self.virtual_staticdir, '_downloads')
self.current_docname = docname self.current_docname = docname
self.docwriter.write(doctree, destination) self.docwriter.write(doctree, destination)
@ -70,7 +71,8 @@ class WebSupportBuilder(PickleHTMLBuilder):
self.handle_page(docname, ctx, event_arg=doctree) self.handle_page(docname, ctx, event_arg=doctree)
def write_doc_serialized(self, docname, 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) self.post_process_images(doctree)
title = self.env.longtitles.get(docname) title = self.env.longtitles.get(docname)
title = title and self.render_partial(title)['title'] or '' title = title and self.render_partial(title)['title'] or ''
@ -148,7 +150,8 @@ class WebSupportBuilder(PickleHTMLBuilder):
PickleHTMLBuilder.handle_finish(self) PickleHTMLBuilder.handle_finish(self)
# move static stuff over to separate directory # move static stuff over to separate directory
directories = ['_images', '_static'] directories = [self.imagedir, '_static']
print directories
for directory in directories: for directory in directories:
src = path.join(self.outdir, directory) src = path.join(self.outdir, directory)
dst = path.join(self.staticdir, directory) dst = path.join(self.staticdir, directory)

View File

@ -146,14 +146,8 @@ def render_dot(self, code, options, format, prefix='graphviz'):
).encode('utf-8') ).encode('utf-8')
fname = '%s-%s.%s' % (prefix, sha1(hashkey).hexdigest(), format) fname = '%s-%s.%s' % (prefix, sha1(hashkey).hexdigest(), format)
if hasattr(self.builder, 'imgpath'):
# HTML
relfn = posixpath.join(self.builder.imgpath, fname) relfn = posixpath.join(self.builder.imgpath, fname)
outfn = path.join(self.builder.outdir, '_images', fname) outfn = path.join(self.builder.outdir, self.builder.imagedir, fname)
else:
# LaTeX
relfn = fname
outfn = path.join(self.builder.outdir, fname)
if path.isfile(outfn): if path.isfile(outfn):
return relfn, outfn return relfn, outfn

View File

@ -86,7 +86,7 @@ def render_math(self, math):
shasum = "%s.png" % sha1(latex.encode('utf-8')).hexdigest() shasum = "%s.png" % sha1(latex.encode('utf-8')).hexdigest()
relfn = posixpath.join(self.builder.imgpath, 'math', shasum) 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): if path.isfile(outfn):
depth = read_png_depth(outfn) depth = read_png_depth(outfn)
return relfn, depth return relfn, depth