# -*- coding: utf-8 -*-
# Copyright (c) 2009 by Leandro Lucarella, Roberto Alsina
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:

# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import aafigure
import aafigure.pdf
from docutils import nodes
from docutils.nodes import General, Inline, Element
from docutils.parsers.rst import directives
from docutils.parsers.rst import nodes
from reportlab.graphics import renderPDF
from docutils.parsers import rst

class Aanode(General, Inline, Element):
    children = ()

    def __init__(self, flowable, rawsource='', *children, **attributes):
        self.rawsource = rawsource
        self.flowable=flowable
        Element.__init__(self, rawsource, *children, **attributes)
    

class Aafig(rst.Directive):
    """
    Directive to insert an ASCII art figure to be rendered by aafigure.
    """
    has_content = True
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = False
    option_spec = dict(
        scale        = float,
        line_width   = float,
        background   = str,
        foreground   = str,
        fill         = str,
        aspect       = float,
        textual      = directives.flag,
        proportional = directives.flag,
    )

    def run(self):

        if 'textual' in self.options:
            self.options['textual'] = True
        if 'proportional' in self.options:
            self.options['proportional'] = True
        visitor = aafigure.process(
            '\n'.join(self.content),
            aafigure.pdf.PDFOutputVisitor,
            options=self.options
        )
        return [Aanode(renderPDF.GraphicsFlowable(visitor.drawing))]
        
directives.register_directive('aafig', Aafig)
directives.register_directive('aafigure', Aafig)

@@ -1377,16 +1377,25 @@
 AMAGE.%0A%0A
+try:%0A    
 import a
@@ -1402,16 +1402,20 @@
 afigure%0A
+    
 import a
@@ -1426,16 +1426,80 @@
 ure.pdf%0A
+    HAS_AAFIG=True%0Aexcept ImportError:%0A    HAS_AAFIG=False%0A    %0A
 from doc
@@ -1568,16 +1568,31 @@
  Element
+, literal_block
 %0Afrom do
@@ -2713,32 +2713,79 @@
 tional'%5D = True%0A
+        try:%0A            if HAS_AAFIG:%0A        
         visitor 
@@ -2816,16 +2816,24 @@
         
+        
 '%5Cn'.joi
@@ -2861,16 +2861,24 @@
         
+   
+     
 aafigure
@@ -2908,24 +2908,32 @@
             
+      
+  
 options=self
@@ -2949,18 +2949,34 @@
         
-)%0A
+        )%0A        
         
@@ -3036,16 +3036,110 @@
 wing))%5D%0A
+        except:%0A            pass%0A        return %5Bliteral_block(text='%5Cn'.join(self.content))%5D%0A
         

Make aafigs work nicely when aafigure is not installed