code: def _embed_code_links(app, gallery_conf, gallery_dir):
      # Add resolvers for the packages for which we want to show links
      doc_resolvers = {}
  
      src_gallery_dir = os.path.join(app.builder.srcdir, gallery_dir)
      for this_module, url in gallery_conf["reference_url"].items():
          try:
              if url is None:
                  doc_resolvers[this_module] = SphinxDocLinkResolver(
                      app.builder.outdir, src_gallery_dir, relative=True
                  )
              else:
                  doc_resolvers[this_module] = SphinxDocLinkResolver(
                      url, src_gallery_dir
                  )
  
          except HTTPError as e:
              logger.warning(
                  "The following HTTP Error has occurred: %d", e.code
              )
          except URLError as e:
              logger.warning(
                  "Embedding the documentation hyperlinks requires Internet "
                  "access.\nPlease check your network connection.\nUnable to "
                  "continue embedding `%s` links due to a URL Error:\n%s",
                  this_module,
                  str(e.args),
              )
  
      html_gallery_dir = os.path.abspath(
          os.path.join(app.builder.outdir, gallery_dir)
      )
  
      # patterns for replacement
      link_pattern = (
          '<a href="%s" title="View documentation for %s">%s</a>'
      )
      orig_pattern = '<span class="n">%s</span>'
      period = '<span class="o">.</span>'
  
      # This could be turned into a generator if necessary, but should be okay
      flat = [
          [dirpath, filename]
          for dirpath, _, filenames in os.walk(html_gallery_dir)
          for filename in filenames
      ]
      iterator = sphinx_compatibility.status_iterator(
          flat,
          gallery_dir,
          color="fuchsia",
          length=len(flat),
          stringify_func=lambda x: os.path.basename(x[1]),
      )
      for dirpath, fname in iterator:
          full_fname = os.path.join(html_gallery_dir, dirpath, fname)
          subpath = dirpath[len(html_gallery_dir) + 1 :]
          pickle_fname = os.path.join(
              src_gallery_dir, subpath, fname[:-5] + "_codeobj.pickle"
          )
  
          if os.path.exists(pickle_fname):
              # we have a pickle file with the objects to embed links for
              with open(pickle_fname, "rb") as fid:
                  example_code_obj = pickle.load(fid)
              fid.close()
              str_repl = {}
              # generate replacement strings with the links
              for name, cobj in example_code_obj.items():
                  this_module = cobj["module"].split(".")[0]
  
                  if this_module not in doc_resolvers:
                      continue
  
                  try:
                      link = doc_resolvers[this_module].resolve(
                          cobj, full_fname
                      )
                  except (HTTPError, URLError) as e:
                      if isinstance(e, HTTPError):
                          extra = e.code
                      else:
                          extra = e.reason
                      logger.warning(
                          "Error resolving %s.%s: %r (%s)",
                          cobj["module"],
                          cobj["name"],
                          e,
                          extra,
                      )
                      continue
  
                  if link is not None:
                      parts = name.split(".")
                      name_html = period.join(
                          orig_pattern % part for part in parts
                      )
                      full_function_name = "%s.%s" % (
                          cobj["module"],
                          cobj["name"],
                      )
                      str_repl[name_html] = link_pattern % (
                          link,
                          full_function_name,
                          name_html,
                      )
              # do the replacement in the html file
  
              # ensure greediness
              names = sorted(str_repl, key=len, reverse=True)
              regex_str = "|".join(re.escape(name) for name in names)
              regex = re.compile(regex_str)
  
              def substitute_link(match):
                  return str_repl[match.group()]
  
              if len(str_repl) > 0:
                  with open(full_fname, "rb") as fid:
                      lines_in = fid.readlines()
                  with open(full_fname, "wb") as fid:
                      for line in lines_in:
                          line = line.decode("utf-8")
                          line = regex.sub(substitute_link, line)
                          fid.write(line.encode("utf-8"))
  
imports: from __future__ import print_function
  import codecs
  import gzip
  import os
  import posixpath
  import re
  import shelve
  import sys
  from io import StringIO
  from .None import sphinx_compatibility
label_desc: Use image node_id 6 for calling a global function with example usage: _embed_code_links(app, gallery_conf, gallery_dir) and returns: str_repl