code: def main(app):
      static_dir = op.join(app.builder.srcdir, "_static")
      target_dir = op.join(app.builder.srcdir, "examples")
      image_dir = op.join(app.builder.srcdir, "examples/_images")
      thumb_dir = op.join(app.builder.srcdir, "example_thumbs")
      source_dir = op.abspath(
          op.join(app.builder.srcdir, "..", "examples")
      )
      if not op.exists(static_dir):
          os.makedirs(static_dir)
  
      if not op.exists(target_dir):
          os.makedirs(target_dir)
  
      if not op.exists(image_dir):
          os.makedirs(image_dir)
  
      if not op.exists(thumb_dir):
          os.makedirs(thumb_dir)
  
      if not op.exists(source_dir):
          os.makedirs(source_dir)
  
      banner_data = []
  
      toctree = "\n\n" ".. toctree::\n" "   :hidden:\n\n"
      contents = "\n\n"
  
      # Write individual example files
      for filename in sorted(glob.glob(op.join(source_dir, "*.py"))):
          ex = ExampleGenerator(filename, target_dir)
  
          banner_data.append(
              {
                  "title": ex.pagetitle,
                  "url": op.join("examples", ex.htmlfilename),
                  "thumb": op.join(ex.thumbfilename),
              }
          )
          shutil.copyfile(filename, op.join(target_dir, ex.pyfilename))
          output = RST_TEMPLATE.format(
              sphinx_tag=ex.sphinxtag,
              docstring=ex.docstring,
              end_line=ex.end_line,
              components=ex.components,
              fname=ex.pyfilename,
              img_file=ex.pngfilename,
          )
          with open(op.join(target_dir, ex.rstfilename), "w") as f:
              f.write(output)
  
          toctree += ex.toctree_entry()
          contents += ex.contents_entry()
  
      if len(banner_data) < 10:
          banner_data = (4 * banner_data)[:10]
  
      # write index file
      index_file = op.join(target_dir, "index.rst")
      with open(index_file, "w") as index:
          index.write(
              INDEX_TEMPLATE.format(
                  sphinx_tag="example_gallery",
                  toctree=toctree,
                  contents=contents,
              )
          )
  
imports: import os
  import os.path
  import re
  import glob
  import token
  import tokenize
  import shutil
  import warnings
  import matplotlib
  import matplotlib.pyplot
label_desc: Use image node_id 4 for calling a global function with example usage: main(app) without return types