示例#1
0
    def get_text(self, client, node, replaceEnt):
        # First see if the image file exists, or else,
        # use image-missing.png
        imgname = os.path.join(client.basedir, str(node.get("uri")))
        try:
            w, h, kind = MyImage.size_for_node(node, client=client)
        except ValueError:
            # Broken image, return arbitrary stuff
            imgname = missing
            w, h, kind = 100, 100, 'direct'

        alignment = node.get('align', 'CENTER').lower()
        if alignment in ('top', 'middle', 'bottom'):
            align = 'valign="%s"' % alignment
        else:
            align = ''
        # TODO: inline images don't support SVG, vectors and PDF,
        #       which may be surprising. So, work on converting them
        #       previous to passing to reportlab.
        # Try to rasterize using the backend
        w, h, kind = MyImage.size_for_node(node, client=client)
        img = MyImage(filename=imgname,
                      height=h,
                      width=w,
                      kind=kind,
                      client=client)
        # Last resort, try all rasterizers
        uri = MyImage.raster(imgname, client)
        return '<img src="%s" width="%f" height="%f" %s/>'%\
            (uri, w, h, align)
示例#2
0
 def gather_elements(self, client, node, style):
     # Based on the graphviz extension
     global graphviz_warn
     try:
         # Is vectorpdf enabled?
         if hasattr(VectorPdf, 'load_xobj'):
             # Yes, we have vectorpdf
             fname, outfn = sphinx.ext.graphviz.render_dot(
                 node['builder'], node['code'], node['options'], 'pdf')
         else:
             # Use bitmap
             if not graphviz_warn:
                 log.warning(
                     'Using graphviz with PNG output. You get much better results if you enable the vectorpdf extension.'
                 )
                 graphviz_warn = True
             fname, outfn = sphinx.ext.graphviz.render_dot(
                 node['builder'], node['code'], node['options'], 'png')
         if outfn:
             client.to_unlink.append(outfn)
             client.to_unlink.append(outfn + '.map')
         else:
             # Something went very wrong with graphviz, and
             # sphinx should have given an error already
             return []
     except sphinx.ext.graphviz.GraphvizError as exc:
         log.error('dot code %r: ' % node['code'] + str(exc))
         return [Paragraph(node['code'], client.styles['code'])]
     return [MyImage(filename=outfn, client=client)]
示例#3
0
 class HandleSphinxGraphviz(SphinxHandler, sphinx.ext.graphviz.graphviz):
     def gather_elements(self, client, node, style):
         # Based on the graphviz extension
         try:
             fname, outfn = sphinx.ext.graphviz.render_dot(
                 node['builder'], node['code'], node['options'], 'pdf')
             client.to_unlink.append(outfn)
             client.to_unlink.append(outfn + '.map')
         except sphinx.ext.graphviz.GraphvizError, exc:
             log.error('dot code %r: ' % node['code'] + str(exc))
             return [Paragraph(node['code'], client.styles['code'])]
         return [MyImage(filename=outfn, client=client)]
示例#4
0
    def gather_elements(self, client, node, style):
        # FIXME: handle alt

        target = None
        if isinstance(node.parent, docutils.nodes.reference):
            target = node.parent.get('refuri', None)
        st_name = 'image'
        if node.get('classes'):
            st_name = node.get('classes')[0]
        style = client.styles[st_name]
        uri = str(node.get("uri"))
        if uri.split("://")[0].lower() not in ('http', 'ftp', 'https'):
            imgname = os.path.join(client.basedir, uri)
        else:
            imgname = uri
        try:
            w, h, kind = MyImage.size_for_node(node, client=client)
        except ValueError:
            # Broken image, return arbitrary stuff
            imgname = missing
            w, h, kind = 100, 100, 'direct'
        node.elements = [
            MyImage(filename=imgname,
                    height=h,
                    width=w,
                    kind=kind,
                    client=client,
                    target=target)
        ]
        alignment = node.get('align', '').upper()
        if not alignment:
            # There is no JUSTIFY for flowables, of course, so 4:LEFT
            alignment = {
                0: 'LEFT',
                1: 'CENTER',
                2: 'RIGHT',
                4: 'LEFT'
            }[style.alignment]
        if not alignment:
            alignment = 'CENTER'
        node.elements[0].image.hAlign = alignment
        node.elements[0].spaceBefore = style.spaceBefore
        node.elements[0].spaceAfter = style.spaceAfter

        # Image flowables don't support valign (makes no sense for them?)
        # elif alignment in ('TOP','MIDDLE','BOTTOM'):
        #    i.vAlign = alignment
        return node.elements
示例#5
0
 def gather_elements(self, client, node, style):
     # FIXME: handle class,target,alt, check align
     imgname = os.path.join(client.basedir, str(node.get("uri")))
     try:
         w, h, kind = MyImage.size_for_node(node, client=client)
     except ValueError:
         # Broken image, return arbitrary stuff
         imgname = missing
         w, h, kind = 100, 100, 'direct'
     node.elements = [
         MyImage(filename=imgname,
                 height=h,
                 width=w,
                 kind=kind,
                 client=client)
     ]
     alignment = node.get('align', 'CENTER').upper()
     if alignment in ('LEFT', 'CENTER', 'RIGHT'):
         node.elements[0].image.hAlign = alignment
     # Image flowables don't support valign (makes no sense for them?)
     # elif alignment in ('TOP','MIDDLE','BOTTOM'):
     #    i.vAlign = alignment
     return node.elements