Beispiel #1
0
def _process_nb(nb):
    # add empty lines before and after a mark/fence
    new_cells = []
    for cell in nb.cells:
        if cell.cell_type == 'markdown':
            md_cells = markdown.split_markdown(cell.source)
            for i, md_cell in enumerate(md_cells):
                if i < len(md_cells) - 1 and md_cells[i + 1]['type'] == 'code':
                    md_cells[i]['source'] += '\n'
                if md_cell['type'] == 'markdown':
                    lines = md_cells[i]['source'].split('\n')
                    for j, line in enumerate(lines):
                        m = common.md_mark_pattern.match(line)
                        if (m is not None
                                and m[1] not in ('ref', 'numref', 'eqref')
                                and m.end() == len(line)):
                            lines[j] = '\n' + line + '\n'
                    md_cells[i]['source'] = '\n'.join(lines)
            new_cells.append(
                nbformat.v4.new_markdown_cell(
                    markdown.join_markdown_cells(md_cells)))
        else:
            new_cells.append(cell)
    # hide/show
    for cell in new_cells:
        if cell.cell_type == 'code':
            if '# hide outputs' in cell.source.lower():
                cell.outputs = []
            if '# hide code' in cell.source.lower():
                cell.source = ''
    return notebook.create_new_notebook(nb, new_cells)
Beispiel #2
0
def remove_slide_marks(
        nb: notebooknode.NotebookNode) -> notebooknode.NotebookNode:
    """Remove all slide blocks and return."""
    new_cells = []
    for cell in nb.cells:
        if cell.cell_type != 'markdown':
            new_cells.append(cell)
        else:
            src = cell.source
            matches = _match_slide_marks(cell.source)
            for pair, text in matches:
                old = pair[0] + text + pair[1]
                new = '' if pair[0].endswith('~~') else text
                src = src.replace(old, new)
            new_cells.append(nbformat.v4.new_markdown_cell(src))
    return notebook.create_new_notebook(nb, new_cells)
Beispiel #3
0
def _generate_slides(
        nb: notebooknode.NotebookNode) -> Optional[notebooknode.NotebookNode]:
    new_cells = []
    has_slides = False
    for cell in nb.cells:
        if cell.cell_type != 'markdown':
            # remove comments
            lines = cell.source.splitlines()
            new_lines = []
            for l in lines:
                new_l = re.sub(r'\#\ .*', '', l)
                if new_l != l and not new_l.rstrip():
                    continue
                new_lines.append(new_l.rstrip())
            cell.source = '\n'.join(new_lines)
            new_cells.append(cell)
        else:
            slide_type = '-'
            src = []
            matches = _match_slide_marks(cell.source)
            if matches:
                has_slides = True
            for pair, text in matches:
                if pair[0].startswith('['):
                    slide_type = 'slide'
                src.append(text)
            src = '\n'.join(src)
            if src:
                # cannot simply use . as it could be in code such as `a.text()`
                for m in ('.\n', '. '):
                    sentences = [s.strip() for s in src.split(m)]
                    src = m.join([s[0].upper() + s[1:] for s in sentences])
                src = src.replace('.$$', '$$').replace(',$$', '$$')
                src = src.rstrip(',. \n:,。:')
            # find level-1 head
            for l in cell.source.splitlines():
                if l.strip().startswith('# '):
                    src = l + '\n\n' + src
                    break
            if not src: continue
            new_cells.append(
                nbformat.v4.new_markdown_cell(
                    src, metadata={"slideshow": {
                        "slide_type": slide_type
                    }}))
    if not has_slides:
        return None

    # merge code cell in the same slide if they don't have output
    md_code_group = common.group_list(new_cells,
                                      lambda cell, _: cell.cell_type == 'code')
    merged_code_cell = []
    for is_code, group in md_code_group:
        if not is_code:
            merged_code_cell.extend(group)
        else:
            src = []
            for i, cell in enumerate(group):
                src.append(cell.source)
                if i == len(group) - 1 or 'outputs' in cell and len(
                        cell['outputs']):
                    cell.source = '\n\n'.join(src)
                    src = []
                    merged_code_cell.append(cell)
    # clean #@save
    for cell in merged_code_cell:
        if cell.cell_type == 'code':
            cell.source = cell.source.replace( \
                '\n#@save\n', '\n').replace('#@save', '').strip()
    return notebook.create_new_notebook(nb, merged_code_cell)