def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
src_file = os.path.join(src_dir, fname)<EOL>example_file = os.path.join(target_dir, fname)<EOL>shutil.copyfile(src_file, example_file)<EOL>image_dir = os.path.join(target_dir, '<STR_LIT>')<EOL>if not os.path.exists(image_dir):<EOL><INDENT>os.makedirs(image_dir)<EOL><DEDENT>base_image_name = os.path.splitext(fname)[<NUM_LIT:0>]<EOL>image_fname = '<STR_LIT>' + base_image_name + '<STR_LIT>'<EOL>image_path = os.path.join(image_dir, image_fname)<EOL>script_blocks = split_code_and_text_blocks(example_file)<EOL>amount_of_code = sum([len(bcontent)<EOL>for blabel, bcontent in script_blocks<EOL>if blabel == '<STR_LIT:code>'])<EOL>if _plots_are_current(example_file, image_path):<EOL><INDENT>return amount_of_code<EOL><DEDENT>time_elapsed = <NUM_LIT:0><EOL>ref_fname = example_file.replace(os.path.sep, '<STR_LIT:_>')<EOL>example_rst = """<STR_LIT>""".format(ref_fname)<EOL>example_nb = Notebook(fname, target_dir)<EOL>filename_pattern = gallery_conf.get('<STR_LIT>')<EOL>if re.search(filename_pattern, src_file) and gallery_conf['<STR_LIT>']:<EOL><INDENT>example_globals = {'<STR_LIT>': '<STR_LIT>'}<EOL>fig_count = <NUM_LIT:0><EOL>is_example_notebook_like = len(script_blocks) > <NUM_LIT:2><EOL>for blabel, bcontent in script_blocks:<EOL><INDENT>if blabel == '<STR_LIT:code>':<EOL><INDENT>code_output, rtime, fig_count = execute_script(bcontent,<EOL>example_globals,<EOL>image_path,<EOL>fig_count,<EOL>src_file,<EOL>gallery_conf)<EOL>time_elapsed += rtime<EOL>example_nb.add_code_cell(bcontent)<EOL>if is_example_notebook_like:<EOL><INDENT>example_rst += codestr2rst(bcontent) + '<STR_LIT:\n>'<EOL>example_rst += code_output<EOL><DEDENT>else:<EOL><INDENT>example_rst += code_output<EOL>example_rst += codestr2rst(bcontent) + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>example_rst += text2string(bcontent) + '<STR_LIT:\n>'<EOL>example_nb.add_markdown_cell(text2string(bcontent))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for blabel, bcontent in script_blocks:<EOL><INDENT>if blabel == '<STR_LIT:code>':<EOL><INDENT>example_rst += codestr2rst(bcontent) + '<STR_LIT:\n>'<EOL>example_nb.add_code_cell(bcontent)<EOL><DEDENT>else:<EOL><INDENT>example_rst += bcontent + '<STR_LIT:\n>'<EOL>example_nb.add_markdown_cell(text2string(bcontent))<EOL><DEDENT><DEDENT><DEDENT>save_thumbnail(image_path, base_image_name, gallery_conf)<EOL>time_m, time_s = divmod(time_elapsed, <NUM_LIT>)<EOL>example_nb.save_file()<EOL>with open(os.path.join(target_dir, base_image_name + '<STR_LIT>'), '<STR_LIT:w>') as f:<EOL><INDENT>example_rst += CODE_DOWNLOAD.format(time_m, time_s, fname,<EOL>example_nb.file_name)<EOL>f.write(example_rst)<EOL><DEDENT>return amount_of_code<EOL>
Generate the rst file for a given example.

        Returns the amout of code (in characters) of the corresponding
        files.