code: def divine_format(text):
      """Guess the format of the notebook, based on its content #148"""
      try:
          nbformat.reads(text, as_version=4)
          return "ipynb"
      except nbformat.reader.NotJSONError:
          pass
  
      lines = text.splitlines()
      for comment in ["", "#"] + _COMMENT_CHARS:
          metadata, _, _, _ = header_to_metadata_and_cell(
              lines, comment, ""
          )
          ext = (
              metadata.get("jupytext", {})
              .get("text_representation", {})
              .get("extension")
          )
          if ext:
              return ext[1:] + ":" + guess_format(text, ext)[0]
  
      # No metadata, but ``` on at least one line => markdown
      for line in lines:
          if line == "```":
              return "md"
  
      return "py:" + guess_format(text, ".py")[0]
  
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 5 for calling a global function with example usage: divine_format(text) and returns: unknown, str, unknown, str