def generate_gallery_rst(app):
try:<EOL><INDENT>plot_gallery = eval(app.builder.config.plot_gallery)<EOL><DEDENT>except TypeError:<EOL><INDENT>plot_gallery = bool(app.builder.config.plot_gallery)<EOL><DEDENT>gallery_conf.update(app.config.sphinx_gallery_conf)<EOL>gallery_conf.update(plot_gallery=plot_gallery)<EOL>gallery_conf.update(abort_on_example_error=app.builder.config.abort_on_example_error)<EOL>app.config.sphinx_gallery_conf = gallery_conf<EOL>app.config.html_static_path.append(glr_path_static())<EOL>clean_gallery_out(app.builder.outdir)<EOL>examples_dirs = gallery_conf['<STR_LIT>']<EOL>gallery_dirs = gallery_conf['<STR_LIT>']<EOL>if not isinstance(examples_dirs, list):<EOL><INDENT>examples_dirs = [examples_dirs]<EOL><DEDENT>if not isinstance(gallery_dirs, list):<EOL><INDENT>gallery_dirs = [gallery_dirs]<EOL><DEDENT>mod_examples_dir = os.path.relpath(gallery_conf['<STR_LIT>'],<EOL>app.builder.srcdir)<EOL>seen_backrefs = set()<EOL>for examples_dir, gallery_dir in zip(examples_dirs, gallery_dirs):<EOL><INDENT>examples_dir = os.path.relpath(examples_dir,<EOL>app.builder.srcdir)<EOL>gallery_dir = os.path.relpath(gallery_dir,<EOL>app.builder.srcdir)<EOL>for workdir in [examples_dir, gallery_dir, mod_examples_dir]:<EOL><INDENT>if not os.path.exists(workdir):<EOL><INDENT>os.makedirs(workdir)<EOL><DEDENT><DEDENT>fhindex = open(os.path.join(gallery_dir, '<STR_LIT>'), '<STR_LIT:w>')<EOL>fhindex.write(generate_dir_rst(examples_dir, gallery_dir, gallery_conf,<EOL>seen_backrefs))<EOL>for directory in sorted(os.listdir(examples_dir)):<EOL><INDENT>if os.path.isdir(os.path.join(examples_dir, directory)):<EOL><INDENT>src_dir = os.path.join(examples_dir, directory)<EOL>target_dir = os.path.join(gallery_dir, directory)<EOL>fhindex.write(generate_dir_rst(src_dir, target_dir,<EOL>gallery_conf,<EOL>seen_backrefs))<EOL><DEDENT><DEDENT>fhindex.flush()<EOL><DEDENT>
Generate the Main examples gallery reStructuredText

    Start the sphinx-gallery configuration and recursively scan the examples
    directories in order to populate the examples gallery