def find_cell_end(self, lines):
        """Return position of end of cell marker, and position
        of first line after cell"""
        if self.in_region:
            self.cell_type = 'markdown'
            for i, line in enumerate(lines):
                if self.end_region_re.match(line):
                    return i, i + 1, True
        elif self.metadata is None:
            # default markdown: (last) two consecutive blank lines, except when in code blocks
            self.cell_type = 'markdown'
            prev_blank = 0
            in_explicit_code_block = False
            in_indented_code_block = False

            for i, line in enumerate(lines):
                if in_explicit_code_block and self.end_code_re.match(line):
                    in_explicit_code_block = False
                    continue

                if self.non_jupyter_code_re and self.non_jupyter_code_re.match(line):
                    in_explicit_code_block = True
                    prev_blank = 0
                    continue

                if prev_blank and line.startswith('    ') and not _BLANK_LINE.match(line):
                    in_indented_code_block = True
                    prev_blank = 0
                    continue

                if in_indented_code_block and not _BLANK_LINE.match(line) and not line.startswith('    '):
                    in_indented_code_block = False

                if in_indented_code_block or in_explicit_code_block:
                    continue

                if self.start_code_re.match(line) or self.start_region_re.match(line):
                    if i > 1 and prev_blank:
                        return i - 1, i, False
                    return i, i, False

                if self.split_at_heading and line.startswith('#') and prev_blank >= 1:
                    return i - 1, i, False

                if _BLANK_LINE.match(lines[i]):
                    prev_blank += 1
                elif i > 2 and prev_blank >= 2:
                    return i - 2, i, True
                else:
                    prev_blank = 0
        else:
            self.cell_type = 'code'
            for i, line in enumerate(lines):
                # skip cell header
                if i == 0:
                    continue
                if self.end_code_re.match(line):
                    return i, i + 1, True

        # End not found
        return len(lines), len(lines), False