Sunday 23 November 2008 21:29:05 Georg Brandl
> Gael Varoquaux schrieb:
> > Hey guys,
> >
> > Sorry to be lazy, but I can't find a quick way to do this: I want a
> > scaled down image, using:
> >
> > .. image:: foobar.jpg
> >
> >     :scale: 50
> >
> > but I want it also to be a link to the image itself. The following does
> > work, because the image is copied in the _images folder:
> >
> > .. image:: foobar.jpg
> >
> >     :target: foobar.jpg
> >     :scale: 50
> >
> > I think it would be nice, if by default, with the HTML writer, the scaled
> > down images had links to the full size image (that would be off course
> > overridden by an explicit 'target').
> >
> > What'ya think?
>
> +1 -- I think Sebastian even had a patch for that.

I wrote something up, the mercurial changeset is attached.

-- 
Freedom is always the freedom of dissenters.
                                      (Rosa Luxemburg)

# HG changeset patch
# User Sebastian Wiesner <[EMAIL PROTECTED]>
# Date 1227644959 -3600
# Node ID 5d35960e9a9f5a2a6045f8c0fe3056ac7b59c6e3
# Parent  7c3a95e3c76080b0fce906ab246fe4630466c482
Scaled images are now linked to their high resultion version

diff -r 7c3a95e3c760 -r 5d35960e9a9f sphinx/builder.py
--- a/sphinx/builder.py Mon Nov 24 20:43:22 2008 +0100
+++ b/sphinx/builder.py Tue Nov 25 21:29:19 2008 +0100
@@ -15,6 +15,7 @@
 import shutil
 import gettext
 import cPickle as pickle
+import posixpath
 from os import path
 from cgi import escape
 
@@ -512,11 +513,11 @@
         )
 
     def write_doc(self, docname, doctree):
-        self.post_process_images(doctree)
         destination = StringOutput(encoding='utf-8')
         doctree.settings = self.docsettings
 
         self.imgpath = relative_uri(self.get_target_uri(docname), '_images')
+        self.post_process_images(doctree)
         self.docwriter.write(doctree, destination)
         self.docwriter.assemble_parts()
         body = self.docwriter.parts['fragment']
@@ -686,6 +687,31 @@
         # dump the search index
         self.handle_finish()
 
+    def post_process_images(self, doctree):
+        """
+        Pick the best candiate for an image and link down-scaled images to
+        their high res version.
+        """
+        Builder.post_process_images(self, doctree)
+        def is_downscaled_with_no_target(node):
+            return (isinstance(node, nodes.image) and
+                    node.has_key('scale') and
+                    # docutils does unfortunately not preserve the
+                    # ``target`` attribute on images, so we need to check
+                    # the parent node here.
+                    not isinstance(node.parent, nodes.reference)
+                    )
+        for node in doctree.traverse(is_downscaled_with_no_target):
+            uri = node['uri']
+            reference = nodes.reference()
+            if uri in self.images:
+                reference['refuri'] = posixpath.join(self.imgpath,
+                                                     self.images[uri])
+            else:
+                reference['refuri'] = uri
+            node.replace_self(reference)
+            reference.append(node)
+
     def get_outdated_docs(self):
         if self.templates:
             template_mtime = self.templates.newest_template_mtime()

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to