def split_code_and_text_blocks(source_file):
    """Return list with source file separated into code and text blocks.

    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
    """
    docstring, rest_of_content, lineno = 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))

    return file_conf, blocks
def split_code_and_text_blocks ( source_file ) : docstring , rest_of_content , lineno = 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 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 ) ) return file_conf , blocks
Return list with source file separated into code and text blocks.

    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