code: def guess_format(text, ext):
      """Guess the format and format options of the file, given its extension and content"""
      metadata = read_metadata(text, ext)
  
      if "text_representation" in metadata.get("jupytext", {}):
          return format_name_for_ext(metadata, ext), {}
  
      if (
          is_myst_available()
          and ext in myst_extensions()
          and matches_mystnb(text, ext)
      ):
          return MYST_FORMAT_NAME, {}
  
      lines = text.splitlines()
  
      # Is this a Hydrogen-like script?
      # Or a Sphinx-gallery script?
      if ext in _SCRIPT_EXTENSIONS:
          unescaped_comment = _SCRIPT_EXTENSIONS[ext]["comment"]
          comment = re.escape(unescaped_comment)
          language = _SCRIPT_EXTENSIONS[ext]["language"]
          twenty_hash_re = re.compile(r"^#( |)#{19,}\s*$")
          double_percent_re = re.compile(rf"^{comment}( %%|%%)$")
          double_percent_and_space_re = re.compile(
              rf"^{comment}( %%|%%)\s"
          )
          nbconvert_script_re = re.compile(
              rf"^{comment}( <codecell>| In\[[0-9 ]*\]:?)"
          )
          vim_folding_markers_re = re.compile(rf"^{comment}\s*" + "{{{")
          vscode_folding_markers_re = re.compile(
              rf"^{comment}\s*region"
          )
  
          twenty_hash_count = 0
          double_percent_count = 0
          magic_command_count = 0
          rspin_comment_count = 0
          vim_folding_markers_count = 0
          vscode_folding_markers_count = 0
  
          parser = StringParser(
              language="R" if ext in [".r", ".R"] else "python"
          )
          for line in lines:
              parser.read_line(line)
              if parser.is_quoted():
                  continue
  
              # Don't count escaped Jupyter magics (no space between %% and command) as cells
              if (
                  double_percent_re.match(line)
                  or double_percent_and_space_re.match(line)
                  or nbconvert_script_re.match(line)
              ):
                  double_percent_count += 1
  
              if not line.startswith(unescaped_comment) and is_magic(
                  line, language
              ):
                  magic_command_count += 1
  
              if twenty_hash_re.match(line) and ext == ".py":
                  twenty_hash_count += 1
  
              if line.startswith("#'") and ext in [".R", ".r"]:
                  rspin_comment_count += 1
  
              if vim_folding_markers_re.match(line):
                  vim_folding_markers_count += 1
  
              if vscode_folding_markers_re.match(line):
                  vscode_folding_markers_count += 1
  
          if double_percent_count >= 1:
              if magic_command_count:
                  return "hydrogen", {}
              return "percent", {}
  
          if vim_folding_markers_count:
              return "light", {"cell_markers": "{{{,}}}"}
  
          if vscode_folding_markers_count:
              return "light", {"cell_markers": "region,endregion"}
  
          if twenty_hash_count >= 2:
              return "sphinx", {}
  
          if rspin_comment_count >= 1:
              return "spin", {}
  
      if ext in [".md", ".markdown"]:
          for line in lines:
              if line.startswith(":::"):  # Pandoc div
                  return "pandoc", {}
  
      # Default format
      return get_format_implementation(ext).format_name, {}
  
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 4 for calling a global function with example usage: guess_format(text, ext) and returns: get_format_implementation,  dict, format_name_for_ext,  dict, MYST_FORMAT_NAME,  dict, str,  dict, str,  dict, str,  dict, str,  dict, str,  dict, str,  dict, str,  dict