(name, arguments, options, content, lineno,
                   content_offset, block_text, state, state_machine)
return []
Handle the plot directive.
Handle the plot directive.
Handle the plot directive .
def plot_directive(name, arguments, options, content, lineno,
                   content_offset, block_text, state, state_machine):
    """
    Handle the plot directive.
    """
    formats = setup.config.plot_formats
    if type(formats) == str:
        formats = eval(formats)

    # The user may provide a filename *or* Python code content, but not both
    if len(arguments) == 1:
        reference = directives.uri(arguments[0])
        basedir, fname = os.path.split(reference)
        basename, ext = os.path.splitext(fname)
        basedir = relpath(basedir, setup.app.builder.srcdir)
        if len(content):
            raise ValueError("plot directive may not specify both a filename and inline content")
        content = None
    else:
        basedir = "inline"
        content = '\n'.join(content)
        # Since we don't have a filename, use a hash based on the content
        reference = basename = md5(content).hexdigest()[-10:]
        fname = None

    # Get the directory of the rst file, and determine the relative
    # path from the resulting html file to the plot_directive links
    # (linkdir).  This relative path is used for html links *only*,
    # and not the embedded image.  That is given an absolute path to
    # the temporary directory, and then sphinx moves the file to
    # build/html/_images for us later.
    rstdir, rstfile = os.path.split(state_machine.document.attributes['source'])
    outdir = os.path.join('plot_directive', basedir)
    reldir = relpath(setup.confdir, rstdir)
    linkdir = os.path.join(reldir, outdir)

    # tmpdir is where we build all the output files.  This way the
    # plots won't have to be redone when generating latex after html.

    # Prior to Sphinx 0.6, absolute image paths were treated as
    # relative to the root of the filesystem.  0.6 and after, they are
    # treated as relative to the root of the documentation tree.  We need
    # to support both methods here.
    tmpdir = os.path.join('build', outdir)
    tmpdir = os.path.abspath(tmpdir)
    if sphinx_version < (0, 6):
        prefix = ''
    else:
        prefix = '/'
    if not os.path.exists(tmpdir):
        cbook.mkdirs(tmpdir)

    # destdir is the directory within the output to store files
    # that we'll be linking to -- not the embedded images.
    destdir = os.path.abspath(os.path.join(setup.app.builder.outdir, outdir))
    if not os.path.exists(destdir):
        cbook.mkdirs(destdir)

    # Generate the figures, and return the number of them
    num_figs = makefig(reference, content, tmpdir)

    if options.has_key('include-source'):
        if content is None:
            lines = [
                '.. include:: %s' % os.path.join(setup.app.builder.srcdir, reference),
                '    :literal:']
            if options.has_key('encoding'):
                lines.append('    :encoding: %s' % options['encoding'])
                del options['encoding']
        else:
            lines = ['::', ''] + ['    %s'%row.rstrip() for row in content.split('\n')]
        lines.append('')
        del options['include-source']
    else:
        lines = []

    if num_figs > 0:
        options = ['      :%s: %s' % (key, val) for key, val in
                   options.items()]
        options = "\n".join(options)
        if fname is not None:
            shutil.copyfile(reference, os.path.join(destdir, fname))

        for i in range(num_figs):
            if num_figs == 1:
                outname = basename
            else:
                outname = "%s_%02d" % (basename, i)

            # Copy the linked-to files to the destination within the build tree,
            # and add a link for them
            links = []
            if fname is not None:
                links.append('`source code <%(linkdir)s/%(basename)s.py>`__')
            for format in formats[1:]:
                shutil.copyfile(os.path.join(tmpdir, outname + "." + format),
                                os.path.join(destdir, outname + "." + format))
                links.append('`%s <%s/%s.%s>`__' % (format, linkdir, outname, format))
            links = ', '.join(links) % locals()

            # Output the resulting reST
            lines.extend((template % locals()).split('\n'))
    else:
        lines.extend((exception_template % locals()).split('\n'))

    if len(lines):
        state_machine.insert_input(
            lines, state_machine.input_lines.source(0))

    return []
def plot_directive ( name , arguments , options , content , lineno , content_offset , block_text , state , state_machine ) : formats = setup . config . plot_formats if type ( formats ) == str : formats = eval ( formats ) # The user may provide a filename *or* Python code content, but not both if len ( arguments ) == 1 : reference = directives . uri ( arguments [ 0 ] ) basedir , fname = os . path . split ( reference ) basename , ext = os . path . splitext ( fname ) basedir = relpath ( basedir , setup . app . builder . srcdir ) if len ( content ) : raise ValueError ( "plot directive may not specify both a filename and inline content" ) content = None else : basedir = "inline" content = '\n' . join ( content ) # Since we don't have a filename, use a hash based on the content reference = basename = md5 ( content ) . hexdigest ( ) [ - 10 : ] fname = None # Get the directory of the rst file, and determine the relative # path from the resulting html file to the plot_directive links # (linkdir).  This relative path is used for html links *only*, # and not the embedded image.  That is given an absolute path to # the temporary directory, and then sphinx moves the file to # build/html/_images for us later. rstdir , rstfile = os . path . split ( state_machine . document . attributes [ 'source' ] ) outdir = os . path . join ( 'plot_directive' , basedir ) reldir = relpath ( setup . confdir , rstdir ) linkdir = os . path . join ( reldir , outdir ) # tmpdir is where we build all the output files.  This way the # plots won't have to be redone when generating latex after html. # Prior to Sphinx 0.6, absolute image paths were treated as # relative to the root of the filesystem.  0.6 and after, they are # treated as relative to the root of the documentation tree.  We need # to support both methods here. tmpdir = os . path . join ( 'build' , outdir ) tmpdir = os . path . abspath ( tmpdir ) if sphinx_version < ( 0 , 6 ) : prefix = '' else : prefix = '/' if not os . path . exists ( tmpdir ) : cbook . mkdirs ( tmpdir ) # destdir is the directory within the output to store files # that we'll be linking to -- not the embedded images. destdir = os . path . abspath ( os . path . join ( setup . app . builder . outdir , outdir ) ) if not os . path . exists ( destdir ) : cbook . mkdirs ( destdir ) # Generate the figures, and return the number of them num_figs = makefig ( reference , content , tmpdir ) if options . has_key ( 'include-source' ) : if content is None : lines = [ '.. include:: %s' % os . path . join ( setup . app . builder . srcdir , reference ) , '    :literal:' ] if options . has_key ( 'encoding' ) : lines . append ( '    :encoding: %s' % options [ 'encoding' ] ) del options [ 'encoding' ] else : lines = [ '::' , '' ] + [ '    %s' % row . rstrip ( ) for row in content . split ( '\n' ) ] lines . append ( '' ) del options [ 'include-source' ] else : lines = [ ] if num_figs > 0 : options = [ '      :%s: %s' % ( key , val ) for key , val in options . items ( ) ] options = "\n" . join ( options ) if fname is not None : shutil . copyfile ( reference , os . path . join ( destdir , fname ) ) for i in range ( num_figs ) : if num_figs == 1 : outname = basename else : outname = "%s_%02d" % ( basename , i ) # Copy the linked-to files to the destination within the build tree, # and add a link for them links = [ ] if fname is not None : links . append ( '`source code <%(linkdir)s/%(basename)s.py>`__' ) for format in formats [ 1 : ] : shutil . copyfile ( os . path . join ( tmpdir , outname + "." + format ) , os . path . join ( destdir , outname + "." + format ) ) links . append ( '`%s <%s/%s.%s>`__' % ( format , linkdir , outname , format ) ) links = ', ' . join ( links ) % locals ( ) # Output the resulting reST lines . extend ( ( template % locals ( ) ) . split ( '\n' ) ) else : lines . extend ( ( exception_template % locals ( ) ) . split ( '\n' ) ) if len ( lines ) : state_machine . insert_input ( lines , state_machine . input_lines . source ( 0 ) ) return [ ]