On Sun, Oct 12, 2008 at 09:28:32PM +0200, Gael Varoquaux wrote:

> On Sun, Oct 12, 2008 at 08:41:08PM +0200, Gael Varoquaux wrote:

> > Hi Sphinx!

> > I have the impression that the scale directive is not honored in the html
> > writer. This puzzles me a bit, as I have had a look at the source code,
> > and the writer calls the base docutils writer to do most of the work.
> > However I have been unable to scale my images using the scale directive.

> I should point out that I have PIL and docutils 0.5 installed. If I run
> rst2html on the relevant file, I get a bunch of errors due to the
> sphinx-specific directives, but the html code I am interested in looks
> OK:

This problem is bugging me, so I am investigating it :).
I had a look at the relevant docutils code, and I would say that the
reason this is not working is probably because during the compilation
docutils cannot open the image file, probably because it has not copied
to the path where it will be found.

Well, I noticed that this problem had been reported more than once
before, and that itch must have very scratchy (or maybe I didn't want to
work on the other things I need to work on), so I wrote a patch to
sphinx. It is not ideal as it duplicates partly some docutils code (but
not much). The patch is attached. It does fix the problem on my example.

Gaƫl

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sphinx-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Index: htmlwriter.py
===================================================================
--- htmlwriter.py       (revision 66880)
+++ htmlwriter.py       (working copy)
@@ -11,6 +11,7 @@
 
 import sys
 import posixpath
+import os
 
 from docutils import nodes
 from docutils.writers.html4css1 import Writer, HTMLTranslator as BaseTranslator
@@ -19,6 +20,10 @@
 from sphinx.highlighting import PygmentsBridge
 from sphinx.util.smartypants import sphinx_smarty_pants
 
+try:
+    import Image                        # check for the Python Imaging Library
+except ImportError:
+    Image = None
 
 class HTMLWriter(Writer):
     def __init__(self, builder):
@@ -253,6 +258,23 @@
         if olduri in self.builder.images:
             node['uri'] = posixpath.join(self.builder.imgpath,
                                          self.builder.images[olduri])
+        
+        if node.has_key('scale'):
+            if Image and not (node.has_key('width')
+                              and node.has_key('height')):
+                try:
+                    im = Image.open(os.path.join(self.builder.srcdir, 
+                                                    olduri))
+                except (IOError, # Source image can't be found or opened
+                        UnicodeError):  # PIL doesn't like Unicode paths.
+                    print olduri
+                    pass
+                else:
+                    if not node.has_key('width'):
+                        node['width'] = str(im.size[0])
+                    if not node.has_key('height'):
+                        node['height'] = str(im.size[1])
+                    del im
         BaseTranslator.visit_image(self, node)
 
     def visit_toctree(self, node):

Reply via email to