code: def read_metadata(text, ext):
      """Return the header metadata"""
      ext = "." + ext.split(".")[-1]
      lines = text.splitlines()
  
      if ext in [".md", ".markdown", ".Rmd"]:
          comment = comment_suffix = ""
      else:
          comment = _SCRIPT_EXTENSIONS.get(ext, {}).get("comment", "#")
          comment_suffix = _SCRIPT_EXTENSIONS.get(ext, {}).get(
              "comment_suffix", ""
          )
  
      metadata, _, _, _ = header_to_metadata_and_cell(
          lines, comment, comment_suffix, ext
      )
      if ext in [".r", ".R"] and not metadata:
          metadata, _, _, _ = header_to_metadata_and_cell(
              lines, "#'", "", ext
          )
  
      # MyST has the metadata at the root level
      if (
          not metadata
          and ext in myst_extensions()
          and text.startswith("---")
      ):
          for header in yaml.safe_load_all(text):
              if not isinstance(header, dict):
                  continue
              if (
                  header.get("jupytext", {})
                  .get("text_representation", {})
                  .get("format_name")
                  == "myst"
              ):
                  return header
              return metadata
  
      return metadata
  
imports: import os
  import re
  import warnings
  import nbformat
  import yaml
  from .cell_reader import DoublePercentScriptCellReader, HydrogenCellReader, LightScriptCellReader, MarkdownCellReader, RMarkdownCellReader, RScriptCellReader, SphinxGalleryScriptCellReader
  from .cell_to_text import BareScriptCellExporter, DoublePercentCellExporter, HydrogenCellExporter, LightScriptCellExporter, MarkdownCellExporter, RMarkdownCellExporter, RScriptCellExporter, SphinxGalleryCellExporter
  from .header import header_to_metadata_and_cell, insert_or_test_version_number
  from .languages import _COMMENT_CHARS, _SCRIPT_EXTENSIONS, same_language
  from .magics import is_magic
  from .metadata_filter import metadata_filter_as_string
  from .myst import MYST_FORMAT_NAME, is_myst_available, matches_mystnb, myst_extensions, myst_version
  from .pandoc import is_pandoc_available, pandoc_version
  from .stringparser import StringParser
  from .version import __version__
label_desc: Use image node_id 2 for calling a global function with example usage: read_metadata(text, ext) and returns: metadata, metadata, header