<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def guess_format(text, ext):
 """Guess the format and format options of the file, given its extension and content"""

    lines = text.splitlines()

    metadata = read_metadata(text, ext)

    if ('jupytext' in metadata and set(metadata['jupytext'])
            .difference(['encoding', 'executable', 'main_language'])) or \
            set(metadata).difference(['jupytext']):
        return format_name_for_ext(metadata, ext), {}

    # Is this a Hydrogen-like script?
    # Or a Sphinx-gallery script?
    if ext in _SCRIPT_EXTENSIONS:
        comment = _SCRIPT_EXTENSIONS[ext]['comment']
        twenty_hash = ''.join(['#'] * 20)
        magic_re = re.compile(r'^(%|%%|%%%)[a-zA-Z]')
        double_percent_re = re.compile(r'^{}( %%|%%)$'.format(comment))
        double_percent_and_space_re = re.compile(r'^{}( %%|%%)\s'.format(comment))
        nbconvert_script_re = re.compile(r'^{}( <codecell>| In\[[0-9 ]*\]:?)'.format(comment))
        vim_folding_markers_re = re.compile(r'^{}\s*'.format(comment) + '{{{')
        vscode_folding_markers_re = re.compile(r'^{}\s*region'.format(comment))

        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 magic_re.match(line):
                magic_command_count += 1

            if line.startswith(twenty_hash) 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 == '.md':
        for line in lines:
            if line.startswith(':::'):  # Pandoc div
                return 'pandoc', {}

    # Default format
    return get_format_implementation(ext).format_name, {}