# 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
    expected = {
        'c':
        [{
            'name': 'c',
            'module': 'a.b',
            'module_short': 'a.b',
            'is_class': False,
            'is_explicit': False,
        }],
        'e.HelloWorld':
        [{
            'name': 'HelloWorld',
            'module': 'd',
            'module_short': 'd',
            'is_class': False,
            'is_explicit': False,
        }],
        'h.i.j':
        [{
            'name': 'j',
            'module': 'h.i',
            'module_short': 'h.i',
            'is_class': False,
            'is_explicit': False,
        }],
    }

    fname = tmpdir.join("identify_names.py")
    fname.write(code_str, 'wb')


# 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

    expected = {
        'c':
        [{
            'name': 'c',
            'module': 'a.b',
            'module_short': 'a.b',
            'is_class': False,
            'is_explicit': False,
        }],
        'e.HelloWorld':
        [{
            'name': 'HelloWorld',
            'module': 'd',
            'module_short': 'd',
            'is_class': False,
            'is_explicit': False,
        }],
        'h.i.j':
        [{
            'name': 'j',
            'module': 'h.i',
            'module_short': 'h.i',
            'is_class': False,
            'is_explicit': False,
        }],
    }

    fname = tmpdir.join("identify_names.py")
    fname.write(code_str, 'wb')


    _, script_blocks = split_code_and_text_blocks(fname.strpath)