# Path: sphinx_gallery/py_source_parser.py
# def split_code_and_text_blocks(source_file, return_node=False):
#     """Return list with source file separated into code and text blocks.
# 
#     Parameters
#     ----------
#     source_file : str
#         Path to the source file.
#     return_node : bool
#         If True, return the ast node.
# 
#     Returns
#     -------
#     file_conf : dict
#         File-specific settings given in source file comments as:
#         ``# sphinx_gallery_<name> = <value>``
#     blocks : list
#         (label, content, line_number)
#         List where each element is a tuple with the label ('text' or 'code'),
#         the corresponding content string of block and the leading line number
#     node : ast Node
#         The parsed node.
#     """
#     docstring, rest_of_content, lineno, node = _get_docstring_and_rest(
#         source_file)
#     blocks = [('text', docstring, 1)]
# 
#     file_conf = extract_file_config(rest_of_content)
# 
#     pattern = re.compile(
#         r'(?P<header_line>^#{20,}.*|^# ?%%.*)\s(?P<text_content>(?:^#.*\s?)*)',
#         flags=re.M)
#     sub_pat = re.compile('^#', flags=re.M)
# 
#     pos_so_far = 0
#     for match in re.finditer(pattern, rest_of_content):
#         code_block_content = rest_of_content[pos_so_far:match.start()]
#         if code_block_content.strip():
#             blocks.append(('code', code_block_content, lineno))
#         lineno += code_block_content.count('\n')
# 
#         lineno += 1  # Ignored header line of hashes.
#         text_content = match.group('text_content')
#         text_block_content = dedent(re.sub(sub_pat, '', text_content)).lstrip()
#         if text_block_content.strip():
#             blocks.append(('text', text_block_content, lineno))
#         lineno += text_content.count('\n')
# 
#         pos_so_far = match.end()
# 
#     remaining_content = rest_of_content[pos_so_far:]
#     if remaining_content.strip():
#         blocks.append(('code', remaining_content, lineno))
# 
#     out = (file_conf, blocks)
#     if return_node:
#         out += (node,)
#     return out
# 
# Path: sphinx_gallery/gen_rst.py
# def _sanitize_rst(string):
#     """Use regex to remove at least some sphinx directives."""
#     # :class:`a.b.c <thing here>`, :ref:`abc <thing here>` --> thing here
#     p, e = r'(\s|^):[^:\s]+:`', r'`(\W|$)'
#     string = re.sub(p + r'\S+\s*<([^>`]+)>' + e, r'\1\2\3', string)
#     # :class:`~a.b.c` --> c
#     string = re.sub(p + r'~([^`]+)' + e, _regroup, string)
#     # :class:`a.b.c` --> a.b.c
#     string = re.sub(p + r'([^`]+)' + e, r'\1\2\3', string)
# 
#     # ``whatever thing`` --> whatever thing
#     p = r'(\s|^)`'
#     string = re.sub(p + r'`([^`]+)`' + e, r'\1\2\3', string)
#     # `whatever thing` --> whatever thing
#     string = re.sub(p + r'([^`]+)' + e, r'\1\2\3', string)
#     return string


import pytest
import sphinx_gallery.backreferences as sg
from sphinx.errors import ExtensionError
from sphinx_gallery.py_source_parser import split_code_and_text_blocks
from sphinx_gallery.gen_rst import _sanitize_rst
    </div>

{1}"""


@pytest.mark.parametrize('content, tooltip, is_backref', [
    # HTML sanitizing
    ('<"test">', '&lt;&quot;test&quot;&gt;', False),
    # backref support
    ('test formating', 'test formating', True),
    # RST sanitizing
    ('1 :class:`~a.b`. 2 :class:`a.b` 3 :ref:`whatever <better name>`',
     '1 b. 2 a.b 3 better name', False),
    ('use :meth:`mne.io.Raw.plot_psd` to',
     'use mne.io.Raw.plot_psd to', False),
    ('`this` and ``that``; and `these things` and ``those things``',
     'this and that; and these things and those things', False),
])
def test_thumbnail_div(content, tooltip, is_backref):
    """Test if the thumbnail div generates the correct string."""
    with pytest.raises(ExtensionError, match='internal Sphinx-Gallery thumb'):
        html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py',
                                     '<"test">', '<"title">')
    content = _sanitize_rst(content)
    title = 'test title'
    html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py',
                                 content, title, is_backref=is_backref,
                                 check=False)
    if is_backref:
        extra = """

# Path: sphinx_gallery/py_source_parser.py
# def split_code_and_text_blocks(source_file, return_node=False):
#     """Return list with source file separated into code and text blocks.
# 
#     Parameters
#     ----------
#     source_file : str
#         Path to the source file.
#     return_node : bool
#         If True, return the ast node.
# 
#     Returns
#     -------
#     file_conf : dict
#         File-specific settings given in source file comments as:
#         ``# sphinx_gallery_<name> = <value>``
#     blocks : list
#         (label, content, line_number)
#         List where each element is a tuple with the label ('text' or 'code'),
#         the corresponding content string of block and the leading line number
#     node : ast Node
#         The parsed node.
#     """
#     docstring, rest_of_content, lineno, node = _get_docstring_and_rest(
#         source_file)
#     blocks = [('text', docstring, 1)]
# 
#     file_conf = extract_file_config(rest_of_content)
# 
#     pattern = re.compile(
#         r'(?P<header_line>^#{20,}.*|^# ?%%.*)\s(?P<text_content>(?:^#.*\s?)*)',
#         flags=re.M)
#     sub_pat = re.compile('^#', flags=re.M)
# 
#     pos_so_far = 0
#     for match in re.finditer(pattern, rest_of_content):
#         code_block_content = rest_of_content[pos_so_far:match.start()]
#         if code_block_content.strip():
#             blocks.append(('code', code_block_content, lineno))
#         lineno += code_block_content.count('\n')
# 
#         lineno += 1  # Ignored header line of hashes.
#         text_content = match.group('text_content')
#         text_block_content = dedent(re.sub(sub_pat, '', text_content)).lstrip()
#         if text_block_content.strip():
#             blocks.append(('text', text_block_content, lineno))
#         lineno += text_content.count('\n')
# 
#         pos_so_far = match.end()
# 
#     remaining_content = rest_of_content[pos_so_far:]
#     if remaining_content.strip():
#         blocks.append(('code', remaining_content, lineno))
# 
#     out = (file_conf, blocks)
#     if return_node:
#         out += (node,)
#     return out
# 
# Path: sphinx_gallery/gen_rst.py
# def _sanitize_rst(string):
#     """Use regex to remove at least some sphinx directives."""
#     # :class:`a.b.c <thing here>`, :ref:`abc <thing here>` --> thing here
#     p, e = r'(\s|^):[^:\s]+:`', r'`(\W|$)'
#     string = re.sub(p + r'\S+\s*<([^>`]+)>' + e, r'\1\2\3', string)
#     # :class:`~a.b.c` --> c
#     string = re.sub(p + r'~([^`]+)' + e, _regroup, string)
#     # :class:`a.b.c` --> a.b.c
#     string = re.sub(p + r'([^`]+)' + e, r'\1\2\3', string)
# 
#     # ``whatever thing`` --> whatever thing
#     p = r'(\s|^)`'
#     string = re.sub(p + r'`([^`]+)`' + e, r'\1\2\3', string)
#     # `whatever thing` --> whatever thing
#     string = re.sub(p + r'([^`]+)' + e, r'\1\2\3', string)
#     return string


# Path: sphinx_gallery/tests/test_backreferences.py
import pytest
import sphinx_gallery.backreferences as sg
from sphinx.errors import ExtensionError
from sphinx_gallery.py_source_parser import split_code_and_text_blocks
from sphinx_gallery.gen_rst import _sanitize_rst

    </div>

{1}"""


@pytest.mark.parametrize('content, tooltip, is_backref', [
    # HTML sanitizing
    ('<"test">', '&lt;&quot;test&quot;&gt;', False),
    # backref support
    ('test formating', 'test formating', True),
    # RST sanitizing
    ('1 :class:`~a.b`. 2 :class:`a.b` 3 :ref:`whatever <better name>`',
     '1 b. 2 a.b 3 better name', False),
    ('use :meth:`mne.io.Raw.plot_psd` to',
     'use mne.io.Raw.plot_psd to', False),
    ('`this` and ``that``; and `these things` and ``those things``',
     'this and that; and these things and those things', False),
])
def test_thumbnail_div(content, tooltip, is_backref):
    """Test if the thumbnail div generates the correct string."""
    with pytest.raises(ExtensionError, match='internal Sphinx-Gallery thumb'):
        html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py',
                                     '<"test">', '<"title">')
    content = _sanitize_rst(content)
    title = 'test title'
    html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py',
                                 content, title, is_backref=is_backref,
                                 check=False)
    if is_backref:
        extra = """

.. only:: not html