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)
def split_markdown_cell( nb: notebooknode.NotebookNode) -> notebooknode.NotebookNode: """split a markdown cell if it contains tab block. a new property `class` is added to the metadata for a tab cell. """ # merge continous markdown cells grouped_cells = common.group_list( nb.cells, lambda cell, _: cell.cell_type == 'markdown') new_cells = [] for is_md, group in grouped_cells: if not is_md: new_cells.extend(group) else: src = '\n\n'.join(cell.source for cell in group) md_cells = markdown.split_markdown(src) is_tab_cell = lambda cell, _: cell['class'] if (cell[ 'type'] == 'markdown' and 'class' in cell) else 'not_tab_cell' grouped_md_cells = common.group_list(md_cells, is_tab_cell) for tab, md_group in grouped_md_cells: new_cell = nbformat.v4.new_markdown_cell( markdown.join_markdown_cells(md_group)) if tab != 'not_tab_cell': assert tab.startswith('`') and tab.endswith('`'), tab new_cell.metadata['tab'] = [ t.strip() for t in tab[1:-1].split(',') ] new_cells.append(new_cell) new_cells = [cell for cell in new_cells if cell.source] return create_new_notebook(nb, new_cells)
def test_split(self): cells = markdown.split_markdown(_markdown_src) self.assertEqual(len(cells), 7) self.assertEqual(cells[0]['type'], 'markdown') self.assertEqual(cells[1]['type'], 'markdown') self.assertEqual(cells[1]['class'], '`python2`') self.assertEqual(cells[3]['class'], '`python 3`') self.assertEqual(cells[5]['class'], 'bash')
def _activate_tab(filename, tab): with open(filename, 'r') as f: src = f.read() cells = markdown.split_markdown(src) for cell in cells: cell_tab = _get_cell_tab(cell) if not cell_tab: continue if tab == 'all' or cell_tab == tab: # activate cell['class'] = '{.python .input}' else: # disactivate cell['class'] = 'python' src = markdown.join_markdown_cells(cells) with open(filename, 'w') as f: f.write(src)
def _translate_markdown(self, text): cells = markdown.split_markdown(text) for cell in cells: if cell['type'] == 'markdown': if 'class' in cell and cell['class']: # it may have nested code blocks cell['source'] = self._translate_markdown(cell['source']) else: text_cells = markdown.split_text(cell['source']) for t_cell in text_cells: if t_cell['source'] and (t_cell['type'] in ['text', 'list', 'title']): text = t_cell['source'] markdown_text = MarkdownText() t_cell['source'] = markdown_text.decode( self.translate(markdown_text.encode(text))) if text.endswith('\n'): t_cell['source'] += '\n' cell['source'] = markdown.join_text(text_cells) return markdown.join_markdown_cells(cells)
def update_ipynb_toc(root): """Change the toc code block into a list of clickable links""" notebooks = find_files('**/*.ipynb', root) for fn in notebooks: with open(fn, 'r') as f: notebook = nbformat.read(f, as_version=4) for cell in notebook.cells: if (cell.cell_type == 'markdown' and '```toc' in cell.source): md_cells = markdown.split_markdown(cell.source) for c in md_cells: if c['type'] == 'code' and c['class'] == 'toc': toc = [] for l in c['source'].split('\n'): if l and not l.startswith(':'): toc.append(' - [%s](%s.ipynb)'%(l,l)) c['source'] = '\n'.join(toc) c['type'] = 'markdown' cell.source = markdown.join_markdown_cells(md_cells) with open(fn, 'w') as f: f.write(nbformat.writes(notebook))
def test_merge(self): cells = markdown.split_markdown(_markdown_src) src = markdown.join_markdown_cells(cells) self.assertEqual(_markdown_src, src)